cdimascio / express-openapi-validator

🦋 Auto-validates api requests, responses, and securities using ExpressJS and an OpenAPI 3.x specification
MIT License
920 stars 211 forks source link

Express 5 req.query immutable #969

Open MarcelHoogesteger opened 2 months ago

MarcelHoogesteger commented 2 months ago

The request parameter mutator is broken when express 5 is used

When you have a query parameter definition for array values, the req.query[key] could not be updated because it is immutable in express 5.

The req.query key values are not parsed by its query parameter definitions in the OpenApi 3 specs

The req.query parameters should be parsed like defined in the OpenApi 3 schema for the query parameter

Example sort query parameter definition: name: sort in: query description: Sort parameter required: false style: pipeDelimited explode: false schema: type: array items: type: string

req.query = { sort: "name|date"} should be having an array value after parsing: req.query = { sort: ["name", "date"]}, but it remains the same value req.query = { sort: "name|date"}

jacobshirley commented 2 months ago

+1.

I've implemented an ugly workaround for it by making query mutable:

app.use((req, res, next) => {
        if (req.query)
            Object.defineProperty(req, 'query', {
                writable: true,
                value: { ...req.query },
            })

        next()
 })