Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routes with glob caused error starting from 5.3.7 #1019

Closed
imsamurai opened this issue Dec 16, 2024 · 4 comments
Closed

Routes with glob caused error starting from 5.3.7 #1019

imsamurai opened this issue Dec 16, 2024 · 4 comments

Comments

@imsamurai
Copy link

Describe the bug
Route like /some/(.*) caused

TypeError: Unexpected ( at 9, expected END: https://git.new/pathToRegexpError
    at Iter.consume (/project/node_modules/express-openapi-validator/node_modules/path-to-regexp/dist/index.js:123:15)
    at consume (/project/node_modules/express-openapi-validator/node_modules/path-to-regexp/dist/index.js:179:16)
    at parse (/project/node_modules/express-openapi-validator/node_modules/path-to-regexp/dist/index.js:183:20)
    at /project/node_modules/express-openapi-validator/node_modules/path-to-regexp/dist/index.js:294:74
    at Array.map (<anonymous>)
    at pathToRegexp (/project/node_modules/express-openapi-validator/node_modules/path-to-regexp/dist/index.js:294:25)
    at lookupRoute (/project/node_modules/express-openapi-validator/dist/middlewares/openapi.metadata.js:73:65)
    at /project/node_modules/express-openapi-validator/dist/middlewares/openapi.metadata.js:17:25
    at /project/node_modules/express-openapi-validator/dist/openapi.validator.js:126:24
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) 

starting from version 5.3.7

To Reproduce

add route to spec and go /some

  /some/(.*):
    get:
      responses:
        200:
          description: OK

Actual behavior
Error

Expected behavior
Validation success

@cdimascio
Copy link
Owner

cdimascio commented Dec 27, 2024

This issue arises from a change in path-to-regex. You can find more details in the README under the Express 4.x section, specifically bullet point 3, which directly affects your case. However, you can address your scenario using the alternative approach mentioned in bullet point 1.

@imsamurai can you describe what you are trying to match with the glob? is it arbitrary paths? some examples will be helpful

@cdimascio
Copy link
Owner

cdimascio commented Dec 27, 2024

note that there are mechanism to match wild card paths. see this test and accompanying spec

another example...

you can match paths using the following:

        {
           // ...
            paths: {
                '/some/{wildcard}*': {
                    parameters: [
                        {
                            name: 'wildcard',
                            in: 'path',
                            required: true,
                            schema: {
                                type: 'string',
                            },
                        },
                    ],
                    get: {
                        responses: {
                            '200': {
                                description: 'OK',
                            },
                        },
                    },
                },
            }
           // ...
        }
   .get('/api/some/:wildcard(*)', (req, res) => {
        const wildcard = req.params.wildcard;
        console.log(`Wildcard: ${wildcard}`);
        res.status(200).send(`Matched wildcard: ${wildcard}`);
    }),

then GET .get(/some/test/stuff) will match and arbitrary path, like /test/stuff

@cdimascio
Copy link
Owner

cdimascio commented Dec 27, 2024

im closing this out given there are options to handle your case. please re-open if i've not captured your scenario properly

additionally, the openapi spec doesn't support wildcard paths, see here.

that said, such paths are common practice when developing rest apis. as a result, express-openapi-validator intends to support them

@imsamurai
Copy link
Author

Sorry for delay, you ask about use case.
I have the route /doc where swagger web ui are hosted.
Web ui needs to load js, css, etc: /doc/somescript.js.
Because i don't control this lib and some files may be changed i need to allow them all: /doc/(.*).

Maybe you can do some backward compatibility in validator? Because this is major change, not path version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants