Unless I am mistaken, the "relationships" section in a POST request needs to go inside the "data" object. The documentation had "relationships" outside of the "data" object, and I couldn't get any relationships to create unless I changed it.
I've updated the documentation, but I'm also including a patch here that will throw a 400 HTTP exception if the POST request contains "relationships" outside of the "data" object.
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | 2888132-18-tests-only.patch | 3.84 KB | gabesullice |
| #16 | 2888132-16.patch | 5.61 KB | gabesullice |
| #16 | interdiff-14-16.txt | 2.57 KB | gabesullice |
Comments
Comment #2
kostajh commentedComment #3
kostajh commentedComment #4
kostajh commentedNot sure why the PHP 7 test failed, as I wrote the patch and tested locally with PHP 7.1.
Comment #5
e0ipso@kostajh it's probably due to D8.4.x-dev, not PHP7. Can you test with Drupal core on 8.4 locally?
Comment #6
wim leersComment #7
gabesulliceComment #8
gabesulliceThis patch includes a fix to the prior work, but it also solves #2888889: JSON API spec compliance: Throw error if the "type" attribute is missing in write operations, cleans up some tests and fixes invalid request payloads... in our own tests :S
Comment #9
gabesulliceDerp, fixed some copy pasta.
Comment #10
e0ipsoI see this in many places. What's the motivation?
😮
Comment #11
e0ipso@gabesullice one clarification about this issue. These compliance issues were created before we had the schema validation built in. This validation here is redundant with the schema validation (which is run in "dev" mode and during functional tests).
What are these (limited) manual tests adding?
Comment #12
gabesulliceJust taste. PHP let's you make a mess with characters like this
->__. Elsewhere in core, I've seen type conversion used more often than the __toString magic method.As far as I can tell, that validation is only for our responses. That is, we're not validating the incoming request payloads. This adds a bit of validation to those as a DX improvement. I'd love to hear your thoughts about validating incoming payloads with Schemata (my assumption was that we were not doing it because of performance concerns).
Comment #13
wim leersIndeed.
Validating request bodies with Schemata too would be very interesting. Why haven't we thought about that before? :)
Because this logic is moved, we also had to move the test coverage? (i.e. because its' validated slightly later)?
Also, why isn't that
$this->validateDocument()call in the place that the original if-test was?This looks like it could apply to the normalized result. But it's really only intended for incoming data: "request body document".
So I'd rename this to something like
validateRequestBody()orvalidateReceivedDocument()or something like that.Interesting. So we had some validation for this already?
I'd +1 these changes, but they feel out of scope. Let's move them to a separate issue, so we can get that consistent everywhere first.
(And of course, you'd need to convince @e0ipso.)
Woah!
Comment #14
gabesulliceIt's an interesting thought. I'm not sure how I feel about it. Would it only be useful for internal application development when
assertcould be turned on? I don't think a public, production API would want to have schemata validating every incoming request, but I'm not sure.The test coverage didn't really "move". I think that's an artifact from #2 being 6 months old. I didn't reroll the patch before creating the interdiff.
Because it makes more sense to do the validation before extracting values from the document. I went even further with this and moved it to the first line of the method.
Done.
Nope, you're just looking at the interdiff. I reworded this to more closely align to the language of the spec.
Removed (while pinching my nose).
Comment #15
wim leersHah, oops!
Should be
protectedand typehinted toarray.Also: should be
staticIMHO (see http://www.drupal4hu.com/node/416.html).Why not
assertSame()?Comment #16
gabesullice1. Good catch.
2. Because I'm a 🐑 and copied the rest of the tests (except for __toString apparently).
Comment #17
wim leersCan you also upload a test-only patch? That one should fail.
Comment #18
gabesulliceSure, but note that this is a weird change where the tests were broken and the code change caught it.
Also, because the first assertion fails for the "relationship" case, the "type" case is never hit.
Comment #20
wim leersHm, right.
Alright, then this is ready for final review & commit by @e0ipso :)
Comment #21
e0ipsoI'm a progress junkie! This feels right. I am now wondering if we should validate input documents against the JSON API schema instead of doing manual validations. If you feel that way, please open a new issue becase this one goes in!
Comment #23
wim leersI think we should at least try that. It could save us a lot of trouble in the future!
Comment #24
kostajh commentedThanks to all of you for working on and merging this!
Comment #25
e0ipsoI'm sorry it took so long @kostajh, but we made it there!
Comment #26
gabesulliceI've rewritten this reply three different times, each with a different conclusion :P
I'm gonna settle on: "we should give this a shot, but not make it a high priority."
If we do put this in, it should be implemented to prove the validity of our implementation-not as a development tool.
More technically, this might mean we run this validation in our exception handler instead of the request handler. We can then decorate the error log with any validation failures (but not change the error from a 5xx to a 4xx!).
This means we should probably revert some of the validation that we did here.
I'm really wary of doing anything that encourages people to be making all their requests by hand, rather than using a jsonapi compliant client-side library (what's the point of a spec if you are?).
Comment #27
gabesulliceTo clarify, I'm okay with leaving these two "manual" validations in because:
Comment #28
wim leersAlright, so let's open a follow-up issue for that? :)