You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
A clear and concise description of what the bug is.
A pre-existing API using v1.yaml and express-openapi-validator successfully
original routes were unversioned e.g. /hello-world/:name
new swagger introduced for breaking change so introduced v2.yaml with /v2/ path defined
new route now /v2/hello-world/:name/:surname
/v2/hello-world/:name/:surname is returning 404
You'll see a linked issue and comment in the code snippet description around uncertainty as well around how OpenApiValidator.middleware has to be called incrementally
To Reproduce
Steps to reproduce the behavior.
./swagger/v1.yaml
openapi: 3.0.3info:
title: Hello World Apidescription: API for greeting the worldcontact:
name: Hello World Apiversion: 1.0.0servers:
- url: /paths:
/hello-world/{name}:
get:
summary: Say hitags:
- Hello Worldparameters:
- name: namedescription: Name of who to greetexample: "Dan"in: pathrequired: trueschema:
type: stringminLength: 1responses:
200:
description: (OK) Success.content:
application/json:
schema:
$ref: "#/components/schemas/get-hello-world-request-response-body"components:
securitySchemes:
basicAuth:
type: httpscheme: basicschemas:
get-hello-world-request-response-body:
type: objectproperties:
greet:
type: stringdescription: Greets the user.headers:
authorization:
description: Identifier (Basic Auth) used to authenticate with the proxied api.schema:
type: stringrequired: truesecurity:
- basicAuth: []
./swagger/v2.yaml
openapi: 3.0.3info:
title: Hello World Apidescription: API for greeting the worldcontact:
name: Hello World Apiversion: 2.0.0servers:
- url: /v2/paths:
/hello-world/{name}/{surname}:
get:
summary: Say hitags:
- Hello Worldparameters:
- name: namedescription: Name of who to greetexample: "Dan"in: pathrequired: trueschema:
type: stringminLength: 1
- name: surnamedescription: Surname of who to greetexample: "Crouch"in: pathrequired: trueschema:
type: stringminLength: 1responses:
200:
description: (OK) Success.content:
application/json:
schema:
$ref: "#/components/schemas/get-hello-world-request-response-body"components:
securitySchemes:
basicAuth:
type: httpscheme: basicschemas:
get-hello-world-request-response-body:
type: objectproperties:
greet:
type: stringdescription: Greets the user.headers:
authorization:
description: Identifier (Basic Auth) used to authenticate with the proxied api.schema:
type: stringrequired: truesecurity:
- basicAuth: []
useRoutes.js
import{Router}from"express";import*asOpenApiValidatorfrom"express-openapi-validator";importpathfrom"path";import{defaultRoutes,v1Routes,v2Routes}from"./routes";constversionRoutesMap=[{version: "v1",routes: v1Routes},{version: "v2",routes: v2Routes},];constuseRoutes=({ router, versionRoutes })=>{for(constrouteofversionRoutes){route(router);}};// I can't figure out why we can't use a loop on versions to register the swagger versions in OpenApiValidator.middleware call and// then simply call useRoutes to register all versioned routes together.// If we do take that approach then for some reason it says "/changerequest" not found when we try to hit the route.// I think it is because the OpenApiValidator.middleware needs to be called after each route has been registered as to avoid conflicts on similar endpoints.// I have raised a question here on this.// https://github.com/cdimascio/express-openapi-validator/issues/179#issuecomment-1816781096exportdefault({ app })=>{constrouter=Router();for(constrouteofdefaultRoutes){route(router);}for(constversionRoutesofversionRoutesMap){constapiSpec=path.join("./docs/swagger",`${versionRoutes.version}.yaml`,);app.use(OpenApiValidator.middleware({
apiSpec,ignorePaths: /.*\/api-docs\/.*/,}),);useRoutes({ router,versionRoutes: versionRoutes.routes});}app.use(router);};
Describe the bug
A clear and concise description of what the bug is.
/hello-world/:name
/v2/hello-world/:name/:surname
/v2/hello-world/:name/:surname
is returning 404OpenApiValidator.middleware
has to be called incrementallyTo Reproduce
Steps to reproduce the behavior.
./swagger/v1.yaml
./swagger/v2.yaml
useRoutes.js
The routes are typical express route definitions:
Actual behavior
A clear and concise description of what happens.
GET http://localhost:3000/hello-world/Dan ✅ works as it did before
GET http://localhost:3000/v2/hello-world/Dan ❌ 404 response
If I don't register the unversioned route (
/hello-world/:name
), then the v2 route (/v2/hello-world/:name/:surname
) does work.Expected behavior
A clear and concise description of what you expected to happen.
I am expecting the unversioned route and the versioned route to both work
Examples and context
An example or relevant context e.g. an OpenAPI snippet, an Express handler function snippet
The text was updated successfully, but these errors were encountered: