Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
JSON:API error code should be string as stated in specification and json schema.
https://jsonapi.org/format/#errors
code
: An application-specific error code, expressed as a string value.
Here is the provided example, which shows the code as a string and not an integer in the JSON response
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/vnd.api+json
{
"jsonapi": { "version": "1.0" },
"errors": [
{
"code": "123",
"source": { "pointer": "/data/attributes/firstName" },
"title": "Value is too short",
"detail": "First name must contain at least three characters."
},
{
"code": "225",
"source": { "pointer": "/data/attributes/password" },
"title": "Passwords must contain a letter, number, and punctuation character.",
"detail": "The password provided is missing a punctuation character."
},
{
"code": "226",
"source": { "pointer": "/data/attributes/password" },
"title": "Password and password confirmation do not match."
}
]
}
Proposed resolution
Cast the error code as string
Remaining tasks
Patch
User interface changes
N/A
API changes
N/A
Data model changes
Release notes snippet
JSON:API now provides HTTP error codes as strings instead of integers in order to comply with the specification. If your client application relies on an integer data type for an HTTP error code, it may need to be updated.
Comment | File | Size | Author |
---|---|---|---|
#9 | 3086408-7.patch | 1.71 KB | Krzysztof Domański |
#9 | 3086408-test-only-fail.patch | 906 bytes | Krzysztof Domański |
#7 | 3086408-7.patch | 1.71 KB | mglaman |
#2 | drupal-error-code-as-string-3086408-2-D8.patch | 818 bytes | el7cosmos |
Comments
Comment #2
el7cosmosComment #4
mglamanPatch did not apply.
Comment #5
mglamanComment #6
mglamanComment #7
mglamanHere is an updated patch. I updated
HttpExceptionNormalizerTest
to use assertSame to verify we're getting strings. Otherwise, the test would consider(int) 13
eqaual to(string) 13
.Comment #8
Krzysztof DomańskiLooks good. Can we get a change record for the behaviour change to JSON:API Error Objects?
Comment #9
Krzysztof Domański1. I add test only to make sure the result is integer.
2. Re-uploud previous patch, because RTBC patch should be last. I hope it will be RTBC soon.
3. The problem of this change is Backward Compatibility...
Comment #11
mglaman#9, there isn't much to consider for backwards compatibility if we open a change record to announce the change. The only bug I could see is if someone tried to perform an integer-based operation on the code without running parseInt (JavaScript) or something similar in other frameworks.
Comment #12
mglamanGave a shot at a change record: https://www.drupal.org/node/3087598
Comment #13
Krzysztof DomańskiThanks!
Comment #15
gabesulliceLooks good to me! Thank you!
Comment #19
catchCommitted/pushed to 9.0.x and cherry-picked to 8.9.x and 8.8.x, thanks!
Comment #20
xjmAh, this is another one of those minor changes to the exposed API for spec compliance. We'll add it to the release notes along with the other.
Comment #21
xjmUpdating the release note after checking with @gabesullice.