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

Express 5 req.query immutable

Open MarcelHoogesteger opened this issue 1 year ago • 4 comments

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"}

MarcelHoogesteger avatar Sep 11 '24 14:09 MarcelHoogesteger

+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()
 })

jacobshirley avatar Sep 12 '24 12:09 jacobshirley

+1

I am facing the same problem. I have remarked that req.query does not inherit from Object.prototype anymore ([Object: null prototype]), I dunno whether or not it can be useful to fix this problem.

Is it planned to fix this bug...?

Thank you !

SophieDel avatar Dec 10 '24 16:12 SophieDel

thanks all. i've started a new branch express-5-support to tackle support for express 5. Without any changes 63 of 414 unit tests fail. with @jacobshirley workaround in place, there are 29 remaining tests to resolve.

seeking community to help resolve the remaining issues

cdimascio avatar Dec 28 '24 01:12 cdimascio

Hello

With https://github.com/cdimascio/express-openapi-validator/pull/1036 the number goes down to 15, due to a fix I've made in some tests.

With the routing syntax changes, I don't think there's an easy way to have tests that guarantee both express 4 and express 5 compatibility, but we can think of that after all the tests route have its matching syntax fixed. Right now, I still haven't found a way to have the same wildcard tests for express 5

SF97 avatar Dec 30 '24 11:12 SF97