express-openapi-validator
express-openapi-validator copied to clipboard
Cannot read property errors
Describe the bug
(NOTE: Sorry, this is more of a question I think as I am trying to figure out an error rather than a bug report)
Getting Cannot read property parameters of undefined and other cannot read errors like that.
To Reproduce I have a YAML schema like
summary: Update a Leap User by adding attributes
description: Updates an entity in `users` table
operationId: user-update
tags:
- user
parameters:
- $ref: ../queryParams.yaml
- $ref: ../cookieParams.yaml
requestBody:
required: true
content:
application/json:
schema:
$ref: ./postRequestBody.yaml
responses:
$ref: ./postResponses.yaml
That is for a POST request which gets $ref included elsewhere.
I do not have a simple set up to reproduce this, but I can work on one if this is not a simple stupid mistake on my part.
Actual behavior
When I do not have parameters in the spec, I get an error that there is a query parameter in the POST and it is not included in the spec. That is good.
Once I include the parameters section, I get the Cannot read property... error.
spectral lint does not show any error. Neither does express-openapi-validator report any errors on start up.
I am using the middleware with the following settings
OpenApiValidator.middleware({
apiSpec: './spec/openapi.yaml',
validateRequests: true,
validateResponses: {
onError: (error, body, req) => {
console.log(`Response body fails validation: `, error);
console.log(`Emitted from:`, req.originalUrl);
console.debug(body);
},
},
});
Expected behavior
Including parameters validates the query and cookie parameters.
Examples and context See spec above
@boks1971, thanks for this issue. An example will be helpful. Feel free to create a repo that demonstrates the bug. I'll have a look.
Thank you so much @cdimascio . I will try to create a repro case and share.
But, wanted to provide more information in case it provides a clue.
It works if I use $refParser.mode = 'dereference'.
I then looked at the output of $RefParser.bundle and $RefParser.dereference. I had used that parameter references in my example (the query and cookie) in another path earlier in the file (a bunch of paths in my code use the same parameters). In the dereference case, it is all dereferenced. In the bundle case, the first instance was dereferenced and the place where I saw the error above is using a JSON pointer to the first instance. Here is what it looks like.
My paths are like
/user/create where parameters looks like
"parameters": [
{
"in": "query",
"name": "session_id",
"schema": {
"type": "string"
},
"required": true,
"description": "session id"
},
{
"in": "cookie",
"name": "session_token",
"schema": {
"type": "string"
},
"required": true,
"description": "session token"
}
],
The next path is /user/update where parameters looks like
"parameters": [
{
"$ref": "#/paths/~1user~1create/post/parameters/0"
},
{
"$ref": "#/paths/~1user~1create/post/parameters/1"
}
],