Discovered as part of #2972808: Comprehensive JSON API integration test coverage phase 6: POST/PATCH/DELETE of relationships.
The acceptable status code for a successful relationship for POST requests is 204 when there are not already relationships at that resource (i.e. the client is creating all of the relationships) or if the data array is empty and the existing relationships are also empty.
A server MUST return a 204 No Content status code if an update is successful and the representation of the resource in the request matches the result. — Updating relationship responses (204)
If there are existing resource identifier objects at the resource not given in the POST request, then the appropriate response is 200:
If a server accepts an update but also changes the targeted relationship(s) in other ways than those specified by the request, it MUST return a 200 OK response. The response document MUST include a representation of the updated relationship(s).
A server MUST return a 200 OK status code if an update is successful, the client’s current data remain up to date, and the server responds only with top-level meta data. In this case the server MUST NOT include a representation of the updated relationship(s). — Updating relationship responses (200)
One might ask why 201 Created is not acceptable given that the spec also says "a server MAY respond with other HTTP status codes."
The reason is the MUST if X, Y Z language given for the 204 and 200 responses. When all conditions are true, the server has no choice but to follow that guidance. IOW, the MAY language is only for all other cases not already covered by the MUSTs.
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | 2977653-14.patch | 16.3 KB | wim leers |
| #14 | interdiff.txt | 3.09 KB | wim leers |
| #11 | 2977653-11.patch | 14.73 KB | wim leers |
| #11 | interdiff.txt | 4.29 KB | wim leers |
| #9 | 2977653-9.patch | 11.04 KB | wim leers |
Comments
Comment #2
wim leersNice find!
Can't we fix this in 8.x-1.x?
Comment #3
gabesulliceYou're right. This can happen in 8.1.x. I was muddling this up in my mind w/ #2977659: Spec Compliance: POST|PATCH|DELETE on relationships should respect arity rules which has a small potential for breakage.
Comment #4
gabesulliceComment #5
wim leersFollowing the
@todoinstructions in the existing test coverage in HEAD. This will cause failures; in theory I should not need to make any additional changes to test coverage; only to controller/normalizer logic.Comment #7
wim leersI missed a few in #5.
Comment #9
wim leersComment #11
wim leersI managed to fix most test failures by updating expectations. But I'm stuck on
in
JsonApiFunctionalTest::testWrite().For some reason the
EntityResource::patchRelationship()→$entity->validate()→\Drupal\Core\Entity\Plugin\Validation\Constraint\EntityChangedConstraintValidator::validate()→::loadUnchanged()call chain is resulting in an exception because a the appropriatenodeDB table does not exist 😵😱 This is the exact exception:I'm clueless. I've spent more than an hour and a half on this already, still nothing. So just posting it here, hoping somebody else figures this out!
Comment #13
wim leersThis is the only change I can see could possibly be causing this problem, in some insanely roundabout/complex way.
Another round of debugging didn't help. Tomorrow with a fresh set of eyes, this should be solvable.
s/udp/upd/
Comment #14
wim leersI can no longer reproduce the bizarreness of #11…
In fact, it actually was pretty easy now to make the entire patch green! Both on 8.5 and 8.6.
Maybe something else in JSON API changed that solved the root cause. Maybe it was my machine. Maybe it was me. 😳
Comment #15
wim leersSelf-review:
All changes in this class are like this: removing
@todos and uncommenting associated intended test coverage, replacing the old test coverage.These were added by @gabesullice in #2972808: Comprehensive JSON API integration test coverage phase 6: POST/PATCH/DELETE of relationships. While working on this patch, I vetted every one. I consulted the JSON API spec while doing so.
These are the necessary logic changes to make the test coverage in point 1 pass.
The changes in these test classes are necessary to make them pass; they follow the example set by the test coverage in point 1. I also verified each of these changes.
I can't find any problems. I mostly just executed on the stubs that @gabesullice had already added, and also ensured this actually matches the JSON API spec. Still, I'd like @gabesullice to do the final sign-off (and hopefully commit).
Comment #17
gabesullice+1
\O/
Comment #18
gabesullice