While trying to create an Order through JSON:API I got an error pushing me to provide the order_number
as part of the request.
After some discussion with mikelutz and mglaman on Slack, the suggested solution was to make the order_number
not required for draft orders and then add a custom validation constraint which marks it required for the other states.
The expectation would be to have the same behavior as if an Order is placed through the site where the order_number
is set by the EventSubscriber to be the same as the order_id
.
Extract of the body of JSON:API POST request:
{
"data": {
"type": "commerce_order--default",
"attributes": {
"mail": "dscl@example.com",
"state": "draft"
},
(...)
}
Extract of the JSON:API error:
(...)
"title": "Unprocessable Entity",
"status": "422",
"detail": "order_number: This value should not be null.",
"source": {
"file": "/var/www/devbrains/pubfactory-d9/web/core/modules/jsonapi/src/Entity/EntityValidationTrait.php",
"line": 59,
"pointer": "/data/attributes/order_number"
},
(...)