Hey there!
My error output has asked me to file an issue with my failing subrequests, so I am here to do so, though I am sure that I am doing something wrong.
Here is the error output:
AssertionError: A Subrequests blueprint failed validation (see the logs for details).
Validation message:
Response failed validation: [{"requestId":"router","action":"view","uri":"\/router\/translate-path?path=some\/path"},{"requestId":"Page","action":"view","headers":{"Accept":"application\/json","X-CONSUMER-ID":"REALLYCOOLUUID"},"uri":"{{router.body@$.jsonapi.individual}}?include=field_content_sections.image,highlighted_content.field_image,field_content_sections.resource_file","waitFor":["router"]}]
And the failure reported:
Validation errors: [{"property":"[0]","pointer":"\/0","message":"Array value found, but an object is required","constraint":"type","context":1},{"property":"[1]","pointer":"\/1","message":"Array value found, but an object is required","constraint":"type","context":1},{"property":"[1].headers","pointer":"\/1\/headers","message":"Array value found, but an object is required","constraint":"type","context":1}]
It sounds like the issue is with my headers definition...but from what I can see, they are properly getting translated as an object.
This issue only seems to come up when I am running on my mac using Drush as the server, on my linux box running Lando, and on our Platform.sh instance, the same page request works fine. Here is the client code that is calling the route via axios:
const routerRequest = {
requestId: "router",
action: "view",
uri: `/router/translate-path?path=${params.vertical}/${params.slug}`
};
const pageRequest = {
requestId: "Page",
action: "view",
headers: {
Accept: "application/json",
"X-CONSUMER-ID": process.env.CONSUMER_ID
},
uri: `{{router.body@$.jsonapi.individual}}?include=field_content_sections.image,highlighted_content.field_image,field_content_sections.resource_file`,
waitFor: ["router"]
};
return app.$axios
.post("/subrequests?_format=json", [routerRequest, pageRequest], {
headers: {
"Content-Type": "application/json",
Accept: "application/json"
}
}).then(...
I am confident I am doing something funky here, but just wanted to report as requested.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 3029570-subrequest-failed-validation.patch | 3.86 KB | weemondo |
| #4 | 3029570-array-not-object.patch | 658 bytes | weemondo |
Comments
Comment #2
AdamWells commentedHi, I was having the exact same problem and have figured out the issue with help from co-workers. I hope it's the same for you. In docroot/modules/contrib/subrequests/schema.json -- verify that the type property for your subrequest(s) and header(s) is an array instead of object. That fixed the issue for me.
Comment #3
AdamWells commentedHi, I was having the exact same problem and have figured out the issue with help from co-workers. I hope it's the same for you. In docroot/modules/contrib/subrequests/schema.json -- verify that the type property for your subrequest(s) and header(s) is an array instead of object. That fixed the issue for me.
Comment #4
weemondo commentedSame problem. In my case the schema validation is failing because it is expecting subrequests and headers to be objects but symfony serializer is by default returning associative arrays. I've attached a patch to change the schema which is the simple option if perhaps not the "right" one.
The Subrequest class expects headers to be an array, refactoring this is a little trickier than I have time for today, I will try to come back to this.
Comment #5
weemondo commentedAdding a patch which decodes the json passing
['json_decode_associative'=> false]. This works locally with the given schema, but have not done extensive testing. This will decode the list of headers as an stdclass also, not sure if this will cause issues.Comment #6
e0ipsoOh, it seems that the validator has changed the implementation under the hood.
Comment #7
dennis_meuwissen commentedI am running into the same issue with JSON:API and subrequests. Neither patches seem to work (anymore), so instead I've opted to disable validation for JSON:API responses until this gets resolved.
For those using a composer-based workflow, adding the following to your composer file will not install the Validator dependency used:
Since JSON:API checks to see if the Validator class supplied by the dependency exists before setting it (ResourceResponseValidator::setValidator), not installing the dependency simply ends up disabling the validation altogether.
Comment #8
e0ipsoKicking the testbot awake.
Comment #9
weemondo commentedJust about worked through the failed tests for the 2nd patch by casting arrays into objects. However, if there are any modules using subrequest data directly this could break the other modules (given that $request->headers is now an object).
I feel like I'm jumping through hoops so we can validate our php data according to json, which makes me wonder if there's a better way?
Comment #10
dustinleblanc@weemondo I am not sure the best place to attack the issue, it seems @e0ipso mentioned the validator itself having a change. It's been long enough since I reported the issue that my mind is fuzzy about the issue and we ended up solving the problem for the project in a different way.
Comment #11
brian-c commentedJust wanted to chime in here that a page cache issue caused both of the errors described in this ticket. Patch #2 at https://www.drupal.org/project/drupal/issues/3050383 resolved it for me.
Comment #12
e0ipsoComment #13
Dhammika commentedI can confirm if you skip the patches #4 and #5 AND patch with 3050383 (mentioned by Brian C above) AND bypass the validator as per comment #7, I was able to get a JSON-API request to be executed!
Comment #14
mattew commentedStill encountered the issue today. For me, disabling the JSON validator is not a real solution either an option, and I can't see how the page cache issue could be related to this JSON validation issue.
I can't see how today Subrequests could work out-of-the-box with this broken validator.
Comment #15
mattew commentedThe current simpler and safer way to fix this issue for the moment is using the patch #4. Works for me.
Comment #16
quadrexdevThe patch from #4 work for me. Thanks weemondo
Comment #18
e0ipsoThanks for the contribution! Sorry it took this long.
Comment #19
e0ipso