express-openapi-validator icon indicating copy to clipboard operation
express-openapi-validator copied to clipboard

path params are not coerced when declared under the path

Open jazzzz opened this issue 3 years ago • 4 comments

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.

jazzzz avatar Jun 07 '21 17:06 jazzzz

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...

vomc avatar Jul 19 '21 15:07 vomc

https://github.com/cdimascio/express-openapi-validator/issues/372#issuecomment-693058131

vomc avatar Jul 19 '21 15:07 vomc

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.

alexkrolick avatar Jun 16 '22 17:06 alexkrolick

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

nklisch avatar Jan 08 '23 20:01 nklisch