According to http://jsonapi.org/format/#fetching-includes:
Furthermore, related resources can be requested from a relationship endpoint:
GET /articles/1/relationships/comments?include=comments.author HTTP/1.1
Accept: application/vnd.api+jsonIn this case, the primary data would be a collection of resource identifier objects that represent linkage to comments for an article, while the full comments and comment authors would be returned as included data.
I have just tested this but it doesn't seem to work on the following:
http://test.local/jsonapi/node/patient/08fa6eae-9943-4718-bb8d-cd7e4f09f...
The response I get does not seem to contain the included data as described:
{
"data": [
{
"type": "node--tube",
"id": "e7085e31-2b3a-4634-98a6-cd1e51da4c62"
},
{
"type": "node--tube",
"id": "83b69c84-7f36-41bb-b31c-cd72031a8055"
},
{
"type": "node--tube",
"id": "68ea705b-1463-4ecc-b394-c27421de8524"
}
],
"links": {
"self": "http://sgh.local/jsonapi/node/patient/08fa6eae-9943-4718-bb8d-cd7e4f09f63d/relationships/field_tubes",
"related": "http://sgh.local/jsonapi/node/patient/08fa6eae-9943-4718-bb8d-cd7e4f09f63d/field_tubes"
}
}Sorry if this is a known bug already posted on another issue, but I couldn't find anything similar, so decided to post an issue for this.
| Comment | File | Size | Author |
|---|---|---|---|
| #46 | 2965056-46.patch | 6.69 KB | wim leers |
| #46 | interdiff.txt | 819 bytes | wim leers |
| #41 | 2965056-41.patch | 5.92 KB | wim leers |
| #41 | interdiff.txt | 1.01 KB | wim leers |
| #38 | 2965056-38.patch | 5.88 KB | wim leers |
Comments
Comment #2
gabesulliceHi @zhangyb! Thanks for the concise report. You even referenced the spec 👍👍👍(w/ an anchor link no less!)
It's funny that you posted this issue today. I was thinking about some JSON API stuff last night and realized that this might be happening (completely different code paths for
relatedandcollectionresources). You've just confirmed my hunch for me, so thank you!It's worth noting two things:
400which is required.Personally, I feel we should just add support for it, but it may be a short-term solution to just return a 400.
@Wim Leers, do you have any thoughts?
Comment #3
zhangyb commentedHi @gabesullice, thanks for the reference! I must have missed that note.
To add - It is actually returning a 400 status, so it is actually compliant with the spec:
Would love to see the relationship endpoint support includes!
Comment #4
gabesulliceThanks for the clarification. I agree that this would be a nice feature to support.
Comment #5
wim leers+1 to supporting this in the 8.x-2.x branch.
Comment #6
gabesulliceI don't know if it was a mistake on my part or if we didn't previously support included on related resources, but I've confirmed that we do support them on related routes currently.
This is only about relationship routes then.
Comment #7
wim leers👏
Comment #8
wim leersThis gets us halfway there. For
http://d8/jsonapi/node/article/90e5de48-341f-486c-964b-b58ccca22365/relationships/uid?include=roles, this results in:i.e. it still includes the values of the relationship too; those should be omitted.
Comment #10
wim leersFixed.
Comment #12
wim leersTurns out that instead of all these changes, I can just generalize some of the logic in
\Drupal\jsonapi\Controller\EntityResource::getRelated().Comment #13
gabesulliceRTBC if tests pass.
Comment #14
wim leersTests in #8, #10 and (probably) #12 are failing on
which returns 200 in HEAD, but 400 now. I think the test is wrong. If you're already looking at the resource identifiers for
uidhow does it make sense to also include them?Comment #16
wim leersComment #18
gabesulliceThis is not how includes work.
At the URL you gave, you're viewing resource identifier objects. By including them, you're saying "also give me the resource objects which are identified". RIOs !== ROs.
The spec has this example:
Notice that both the path and the include have the
commentsfield name in them.FWIW, I also found this confusing, but it is the proper behavior.
Comment #19
wim leersAlright. It definitely is confusing. My next step was to consult the spec, but I'm glad that you already experienced the same confusion in the past and are hence able to bring clarity based on past experience :)
So then that means I'm breaking existing spec-compliant behavior in the above patches.
Comment #20
wim leersAs of #3026030: [regression] Includes are no longer respected when POSTing/PATCHing, includes are again supported on every operation. That issue brought it back to
createIndividual()andpatchIndividual(). It already was supported ingetIndividual(),getCollection(),getRelated(),getRelationship(),addToRelationshipData()/replaceRelationshipData()/removeFromRelationshipData()(which all callgetRelationship()). That's everything.The only bug here is that if you first request
/jsonapi/node/article/a2c1bfb4-b715-46b3-b196-e56b79f14119/relationships/uidfollowed by/jsonapi/node/article/a2c1bfb4-b715-46b3-b196-e56b79f14119/relationships/uid?include=uid, then because theurl.query_args:includecache context is absent, the original cached response will be served and hence it appears to not work. But this is simply incorrect cacheability metadata!Even more complex cases like
/jsonapi/node/article/a2c1bfb4-b715-46b3-b196-e56b79f14119/relationships/field_tags?include=field_tags.vidwork fine.Attached patch is purely expanding the existing test coverage and proves this is just a bit of missing cacheability. It will fail due to this.
Comment #21
wim leersAnd this fixes it.
Comment #24
wim leersDrupalCI is apparently having problems… let's give it some time to recover. 😴
Comment #25
wim leersTODO: remove this:
Comment #28
wim leersComment #29
wim leersAddress #25.
Comment #31
wim leersComment #32
wim leers#29:
I think that this
@todoreally was trying to say that we should get rid of bothurl.query_args:fieldsandurl.query_args:include. Right now it's only removing the latter.#31:
This is removing one of these thanks to the fix above, but in this case there is no doubt: we want both of these
url.query_args:*cache contexts to disappear. This is fixing one but not the other. Fixing my remark about #29 would also fix this properly.So let's do that too.
Comment #33
wim leersFor #32.
Comment #34
wim leersWell, actually, since this never even was a feature, it can't be broken and thus it can't be a bug, can it?
Comment #35
wim leersRebased #31 (it no longer applied without conflict).
Comment #36
wim leersWorked to finish this because it removes two
@todos under our control, and per #3028970: Audit all @todos, we need to keep only relevant@todos for JSON:API to go into core.Comment #38
wim leers#3028970: Audit all @todos landed!
Let's land this next.
Comment #39
wim leersMore accurate title.
Comment #41
wim leersFix CS violation.
Comment #43
wim leersAhhh, just one more fail…
Comment #44
gabesullice❤️
Uber-technicality: we could not add the
fieldscontext to relationship requests that don't have anincludequery parameter.However, I think that for simplicity's sake, we don't need to worry about that.
The last failure is from a bad test expectation. RTBC when tests pass :)
Comment #45
wim leersYep, exactly. That's a far-future optimization if the need ever arises.
Thanks for the review! 🙏
Comment #46
wim leersThis fixes the one fail we still have, but I think it might break something else.
Comment #47
wim leers🎉 🚢
Comment #49
wim leers