Closed (fixed)
Project:
JSON:API
Version:
8.x-1.x-dev
Component:
Code
Priority:
Major
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
3 Jan 2017 at 14:58 UTC
Updated:
13 Feb 2017 at 08:54 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
hampercm commentedComment #3
wim leersWoot! Thanks Chris :) This is possibly the single most important issue in the JSON API issue queue right now!
Comment #4
hampercm commentedHere's a preliminary patch for performing validation against the official JSON API schema. I'd like to test it a bit more before it is committed, but it's already discovered a couple issues with responses that didn't meet spec, so that's a ++
Bumping Priority to Major based on @Wim Leers's comment. Automated tests will fail until child issues are resolved.
Comment #6
hampercm commentedAdd some elements to the composer.json file that are required for Drupal modules.
Comment #7
hampercm commentedComment #9
e0ipsoComment #10
e0ipsoWe have the ability to create JSON Schemas for the resources themselves based on the Typed Data (using https://github.com/e0ipso/data_model). That means that we could test the output against the specialized schema per resource, instead of the generic schema. This would have the benefit of asserting required values, cardinalities, etc. However the generated schema may have bugs and cause false alerts.
Thoughts?
Comment #11
e0ipsoComment #12
hampercm commentedThis patch fixes a couple of issues with #4
- Don't flag empty responses as invalid (DELETE responses for example)
- Use JSON::encode() to ensure web-safe characters are logged
It also adds a debugging "printout" to the first functional test, to help figure out why it's failing. This will need to be removed later, as it will cause the test to fail.
Comment #14
hampercm commentedHmmm, looks like the 500 response has no content, so no help there. Checking to see if the validator class exists...
Comment #16
wim leersIt looks like DrupalCI is not installing the PHP lib. Described the problem at #2597778-78: Install composer dependencies for contrib projects before running tests, and pinged Mixologic on IRC.
Comment #17
wim leers@e0ipso: I think that can be a next step. Regardless of that, I think it's critical that we comply with the generic/basic/barebones schema for JSON API responses. If we don't comply with that, it's pointless to generate our own more specific schemas.
In other words: we must ALWAYS comply with the "basic JSON API JSON schema". As a next step, we should have a JSON schema for any JSON API extensions we support. And as a final step, we can have JSON schemas for our specific resources.
This is looking great, and wonderfully simple :)
This is currently running 100% of the time on PHP 5.
On PHP 5, you must wrap the assertion logic in quotes, i.e. make it a string. PHP 5
eval()s these statements.Should be
static, because this doesn't use any data in theRequestHandlerclass (and shouldn't).s/$responseData/$response_data/
Or $response_body.
Is this really necessary?
It's okay for this all to be put on a single line.
I'd like to see an additional test here where this newly added assertion is explicitly failing.
Because that would allow us to ensure that the DX of failed JSON API JSON schema validation is solid.
Comment #18
hampercm commentedThanks for the review!
1) Oops. I had originally done this as a string, but changed it to debug some weirdness. Forgot to change it back.
2) Done
3) Done
4) Not really necessary, I suppose. Removed.
5) Done
6) Test added.
@e0ipso: Regarding creating custom schemas, I agree with @Wim Leers. That does seem like a nice thing to explore, but I'd say create a separate issue for it.
This issue is blocked on child issues being resolved, and on a bug in the Drupal CI infrastructure, though the latter can be worked around.
Comment #19
e0ipsoYup, that's what I meant. I was sharing a random thought in there.
Comment #20
MixologicI fixed the drupalci issue, and jsonschema is being installed now. Looks like its still failing #14 has different errors now.
Comment #21
wim leersThanks, Mixologic!
Comment #22
hampercm commentedYes, there will be test failures until the child issues are resolved. Glad to hear about the Drupal CI fix. @Mixologic++
Comment #23
wim leersSo this is blocked on #2841096: Included objects returned for a relationships endpoint have attributes in a "data" key and #2841109: Included entities that have access denied must comply with the spec, and both of those have been discovered thanks to this issue?
Comment #24
hampercm commented@Wim Leers yes, that's correct. Tests won't pass until those are resolved.
Comment #25
wim leersAlright, updating issue title to reflect that then. :)
Comment #26
wim leersWhen the blockers are in and we resume work on this issue/patch, we should change this to
require-dev.Comment #27
hampercm commentedIf that's done, we should also add a check for the existence of the validator class, in case the dependency isn't installed.
Comment #28
e0ipsoI think that's acceptable.
Comment #29
wim leersLooking at #26 again, I think it could be argued either way actually.
Comment #30
hampercm commentedI had originally considered making it a
require-dev, but it seemed like there would be an advantage to having validation on all installs. The performance impacts from validation aren't too bad:PHP 7.0: 19.8 req/sec vs. 22.0
PHP 5.6: 15.9 req/sec vs. 20.2
and are statistically insignificant with assertions turned off (<1% performance hit). The code size of the dependency is about 128k.
My preference would be to leave as a
require'd dependency. Perhaps we could plan to move it to arequire-devwhen the module stabilizes?Comment #31
e0ipsoI don't really care as long as we have the same outcome, which is there is a conscious decision to turn this on (regardless of the composer command needed).
Comment #32
wim leersSounds good to me. Let's get it stable first.
Comment #33
e0ipsoThis is unblocked now.
Comment #35
wim leersWOOT!
Comment #36
e0ipsoComment #37
hampercm commented:) Thanks for your help with that, e0ipso!
working on this...
Comment #38
hampercm commentedPatch rerolled.
I also changed the validation library to a
require-devdependency in the composer file. Verifying that automated tests still pass...This isn't quite ready to commit yet, as I'll need to add a check to the validation code for the case where the validation library isn't installed.
Comment #39
hampercm commentedCheck added. This should be good to go!
Comment #40
wim leersWouldn't it be better to show the validation errors here, instead of this generic "please report"? Only developers will see this, and they'll be smart enough to report it?
Looks sensible :)
Comment #41
hampercm commentedThe validation errors do get watchdog logged. I'll add a bit of text to the assert message explaining that.
Comment #42
hampercm commentedRefer to recent log messages in assert() message
Comment #43
e0ipsoSorry I'm late to this, but I'd rather remove references to https://www.drupal.org/project/issues/jsonapi since we're proposing this for inclusion into core.
Maybe: A JSON API response failed validation (see /admin/reports/dblog for details). Please report this at the issue queue.'
Comment #44
hampercm commentedGood point! Fixed.
Comment #45
dawehnerThis is a nice trick!
Couldn't this be a kernel test?
Is there a reason we cannot use
assertFalsehere?Comment #46
hampercm commentedAddressed #45:
1. :)
2. Yes, it can be a kernel test. I had originally thought to use a more complex scenario that utilized a full functional test environment, but went a different direction. Changed.
3. Changed.
Thanks!
Comment #47
e0ipsoI'm cool to commit this, but it seems that the patch in #46 got some extra unintended stuff attached to it,
Comment #49
hampercm commentedThe patch in #46 looks correct to me (except for the missing newline at EOF).Ugh, guess it did get some changes from the core patch review mixed in.
Rerolling...
Comment #50
hampercm commentedFixed. Sorry for the lack of an interdiff.
Comment #51
hampercm commentedComment #52
wim leersI have only nits:
I was going to say "this description is wrong", but it's wrong in the
.info.ymltoo. So, no fault of this patch.Does this file have a particular version? How will we keep this up to date?
Unused
use.This sentence is unnecessary, and misleading. This function is simply called, it has no knowledge whatsoever about which responses are going through the system. It simply validates the response it is given.
Comment #53
hampercm commentedComment #54
hampercm commentedAddresses #52:
1. Yes, that description is out-of-date. Taking the liberty of making it more accurate.
2. There isn't really any version on the schema file, it's part of the JSON API standard's GitHub repo at https://raw.githubusercontent.com/json-api/json-api/gh-pages/schema . Not sure how to plan keeping it up-to-date. Any ideas?
3. This class IS used.
4. Removed. This was a holdover from when I was actually checking the format, or at least planning to.
Comment #55
wim leers#54.2: let's at least document in this issue which commit (hash) it uses.
#54.3: oops, sorry!
Comment #56
hampercm commentedThe schema file was last updated September 19, 2016 in commit https://github.com/json-api/json-api/commit/ff222faee41260d80cb8054e75fb...
Comment #57
wim leersThere we go :)
Comment #58
e0ipsoI'm working on resolving conflicts after #2846857: Automated tests broken got merged and some minor follow ups.
Comment #59
e0ipsoNo intediff due to reroll. However these are the most important changes I made:
(see /admin/reports/dblog for details) to (see the logs for details)
Added:
<?
// Do not use Json::decode here since it coerces the response into an
// associative array, which creates validation errors.
Added:
Changed to:
(This applies to all the asserts in this class).
Merging if green.
Comment #61
e0ipsoComment #62
hampercm commented@e0ipso ++
Comment #63
wim leersWoooooot! This is a huge milestone!