express-openapi-validator
express-openapi-validator copied to clipboard
path params are not coerced when declared under the path
Describe the bug
Path params are coerced when declared under a method, but not when declared under the path for all the methods.
To Reproduce
With the following openapi.yaml
, req.params.id
is a string, whereas it should be a number:
paths:
/my-path/{id}:
parameters:
- name: id
in: path
schema:
type: number
post:
With the following openapi.yaml
, req.params.id
is a number:
paths:
/my-path/{id}:
post:
parameters:
- name: id
in: path
schema:
type: number
Actual behavior
The path parameters are not coerced (req.params.id
is a string).
Expected behavior
The path parameters are coerced (req.params.id
is a number).
Examples and context
See the example above.
I also noticed this. Seems like a huge issue but maybe I am doing something wrong?
I somehow assumed also that if you specify a path param to be a number
, you would in fact get a number back not a string...
https://github.com/cdimascio/express-openapi-validator/issues/372#issuecomment-693058131
It appears that not only do query parameters that use $ref for the schema not get coerced (req.params in runtime code is still a string), they also don't get typechecked at all. So if the ref says it should be a number, a string passes through validation without a warning.
I have experienced this issue as well. I might look at trying to do a PR for it, cause I don't like repeating simple path parameters like {id} for a CRUD collection of endpoints