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
When responding to a request on a "relationships" endpoint with an include query parameter, the included objects in the response have their attributes inside an extraneous "data" key. This violates spec.
For example, a request to /jsonapi/node/article/4f342419-e668-4b76-9f87-7ce20c436169/relationships/uid?_format=api_json&include=uid
returns the following response:
{
"data": {
"type": "user--user",
"id": "ba316f6a-143a-4263-9841-b66628564e93"
},
"links": {
"self": "http://drupal-8x.dd:8080/jsonapi/node/article/4f342419-e668-4b76-9f87-7ce20c436169/relationships/uid?_format=api_json",
"related": "http://drupal-8x.dd:8080/jsonapi/node/article/4f342419-e668-4b76-9f87-7ce20c436169/uid?_format=api_json"
},
"included": [
{
"data": {
"type": "user--user",
"id": "ba316f6a-143a-4263-9841-b66628564e93",
"attributes": {
"uid": "1",
"uuid": "ba316f6a-143a-4263-9841-b66628564e93",
"langcode": "en",
"name": "admin",
"created": "1483459313",
"changed": "1483459313",
"default_langcode": "1"
},
"relationships": {
"user_picture": {
"data": null
}
},
"links": {
"self": "http://drupal-8x.dd:8080/jsonapi/user/user/ba316f6a-143a-4263-9841-b66628564e93?_format=api_json"
}
},
"links": {
"self": "http://drupal-8x.dd:8080/jsonapi/node/article/4f342419-e668-4b76-9f87-7ce20c436169/relationships/uid?_format=api_json&include=uid&XDEBUG_SESSION_START=PHPSTORM"
}
}
]
}
Proposed resolution
Don't generate the "data" level when serializing these objects.
So, this instead:
{
"data": {
"type": "user--user",
"id": "ba316f6a-143a-4263-9841-b66628564e93"
},
"links": {
"self": "http://drupal-8x.dd:8080/jsonapi/node/article/4f342419-e668-4b76-9f87-7ce20c436169/relationships/uid?_format=api_json",
"related": "http://drupal-8x.dd:8080/jsonapi/node/article/4f342419-e668-4b76-9f87-7ce20c436169/uid?_format=api_json"
},
"included": [
{
"type": "user--user",
"id": "ba316f6a-143a-4263-9841-b66628564e93",
"attributes": {
"uid": "1",
"uuid": "ba316f6a-143a-4263-9841-b66628564e93",
"langcode": "en",
"name": "admin",
"created": "1483459313",
"changed": "1483459313",
"default_langcode": "1"
},
"relationships": {
"user_picture": {
"data": null
}
},
"links": {
"self": "http://drupal-8x.dd:8080/jsonapi/user/user/ba316f6a-143a-4263-9841-b66628564e93?_format=api_json"
}
},
"links": {
"self": "http://drupal-8x.dd:8080/jsonapi/node/article/4f342419-e668-4b76-9f87-7ce20c436169/relationships/uid?_format=api_json&include=uid&XDEBUG_SESSION_START=PHPSTORM"
}
]
}
Remaining tasks
Comment | File | Size | Author |
---|---|---|---|
#16 | included_objects-2841096-16.patch | 12.29 KB | hampercm |
|
Comments
Comment #2
hampercm CreditAttribution: hampercm at Acquia commentedComment #3
e0ipsoComment #4
hampercm CreditAttribution: hampercm at Acquia commentedComment #6
hampercm CreditAttribution: hampercm at Acquia commentedComment #7
hampercm CreditAttribution: hampercm at Acquia commentedComment #9
e0ipsoI believe there is something going on with the testbot when applying patches.
I know this was not introduced in this issue, but it seems a relevant place to fix it. Can you rename
DocumentRootNormalizerValue
toJsonApiDocumentTopLevelNormalizerValue
for consistency?Comment #10
hampercm CreditAttribution: hampercm at Acquia commentedRenamed DocumentRootNormalizerValue and DocumentRootNormalizerValueInterface
Comment #11
Wim LeersI've updated the IS to show the before/after.
This proves that that change is indeed made by this patch.
Looks ready to me!
+1 to this. Although I would argue it's out of scope. The patch in #6 fixes the actual bug. It's 2.5 K. Doing this too makes the patch 11 K.
Of course, e0ips0's call :)
Also, this is going to clash quite epically with #2842378: Remove all remaining interfaces in the JSON API module that have only one implementation. This one is much smaller and will be easier to reroll, so I'd prefer that one to go in first. But, if this goes in first, that's fine too — it's not that big of a deal.
Comment #13
hampercm CreditAttribution: hampercm at Acquia commentedI'm fine with postponing and re-rolling after #2842378: Remove all remaining interfaces in the JSON API module that have only one implementation goes through.
Comment #14
e0ipso#2842378: Remove all remaining interfaces in the JSON API module that have only one implementation is merged and this should be unblocked now.
Comment #15
hampercm CreditAttribution: hampercm at Acquia commentedRerolling the patch for this...
Comment #16
hampercm CreditAttribution: hampercm at Acquia commentedRerolled the patch. No interdiff due to rebase.
Comment #18
e0ipso