My nodejs app has an open-api.yaml file and express-openapi-validate validator. I'm doing a POST request which is working and the api validator doesn't return any errors:
curl --request POST 'http://localhost:33004/my-app/settings' --data-raw '{"serials":["1234","2355"]}' -H 'Content-Type: application/json'
In my open-api.yaml I have:
openapi: '3.0.0'
servers:
- url: 'http://{host}/my-app'
variables:
host:
enum:
- 'localhost'
....
...
paths:
/settings:
...
post:
tags:
- 'settings'
operationId: 'postSettings'
requestBody:
content:
application/json:
schema:
type: object
properties:
serials:
type: array
items:
type: string
...
Then I tried dockerizing my app - created a docker container, and ran it inside with the pm2-runtime. However, when I send the same request to the docker container when the app is running in it, I get error while validating request: request should have required property '.headers' . I have no property '.headers' mentioned in the open-api.yaml file.
I tried removing the validator middleware, and the request went through just fine. Can you help me understand what is the validator complaining about?
EDIT:
I managed to find the error object:
{
"data": [
{
"dataPath": "",
"keyword": "required",
"message": "should have required property '.headers'",
"params": {
"missingProperty": ".headers"
},
"schemaPath": "#/required"
}
],
"name": "ValidationError",
"statusCode": 400
}
needless to say I have no required headers property...
I had the same for all node versions <= 14 and for express-openapi-validator version <= 3.
Impossible to have the validator working with node version 16 and we were stuck to an old dev version because of it.
The solution was to migrate to express-openapi-validator v4.
The migration is explained here https://github.com/cdimascio/express-openapi-validator#upgrading-from-3x
This (v3):
const validator = new OpenApiValidator({
apiSpec: openApiYaml,
operationHandlers: __dirname
})
app.use(validator)
becomes (v4):
const validator = OpenApiValidator.middleware({
apiSpec: 'src/openapi.yaml'
})
app.use(validator)
The code was originally generated by https://hub.docker.com/r/openapitools/openapi-generator-cli
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With