express-openapi-validator
express-openapi-validator copied to clipboard
bug: multiple specs are not working
Describe the bug
applying multiple specs to middleware does not seem to be supported. only the very first applied spec is working.
I found this issue, but it applied to a previous version of this project, and indicated it should have been fixed: https://github.com/cdimascio/express-openapi-validator/issues/281
To Reproduce
[spec1, spec2].forEach((spec) =>
app.use(
middleware({
spec,
operationHandlers: {
...
},
}),
)
)
Actual behavior only first (spec1) spec is known and maps operation handlers.
Expected behavior both specs should map operation handlers.
Examples and context
after some debugging I got it working removing this block: https://github.com/cdimascio/express-openapi-validator/blob/3cbcba92ce54a3422e9e63d0bc70cea078ef65de/src/middlewares/openapi.metadata.ts#L47 throwing an exception.
it seems every schema enters lookupRoute: https://github.com/cdimascio/express-openapi-validator/blob/3cbcba92ce54a3422e9e63d0bc70cea078ef65de/src/middlewares/openapi.metadata.ts#L26 , and if not found in the very first schema, it throws an exception.
@dnalborczyk i added some new multi-spec tests. the tests essentially replicate the example 2 multi-spec. the tests succeed. one key difference is that the tests do not use operation handlers.
i wonder if this issue is specific to operation handlers?
would you mind modifying example 2 with a route or two from your spec. if it works, then I'll know the issue is related to operation handlers.
@cdimascio thank you for looking into this!
would you mind modifying example 2 with a route or two from your spec.
yes, absolutely! I'll have a look tomorrow and report back. 👍
@cdimascio I added a (failing) multi spec test: https://github.com/cdimascio/express-openapi-validator/pull/552 based on your other multi spec test.
notice that this test is not using any server url: https://github.com/cdimascio/express-openapi-validator/blob/4c1354f39dddc924d8fca4ae790a3efc153eba3f/test/api.v1.yaml#L11 and therefore not any base paths: https://github.com/cdimascio/express-openapi-validator/blob/4c1354f39dddc924d8fca4ae790a3efc153eba3f/test/multi.spec.spec.ts#L69
I'm currently facing the same issue. Is there any update? Thanks in advance!
It fails also with v5, and it's not related to the usage of operationHandlers.
I fixed it simply by using one middleware per route, changing this in the docs example:
const versions = [1, 2];
for (const v of versions) {
const apiSpec = path.join(__dirname, `api.v${v}.yaml`);
app.use(`api_v${v}`,
OpenApiValidator.middleware({
apiSpec,
}),
);
routes(app, v);
}
I'm facing the same issue when applying multiple specs. When I'm using 2 swaggers, endpoints from first swagger work properly but there are problems with second swagger.
I had the same as well, loading it the way @juanda99 did, also did not fix it for me.
It works for the first set of routers that I pass with app.use(someRouterInstance)
What is interesting is that when I did
app.use("/api", OpenApiValidator.middleware({ apiSpec: C:/projects/skills/api-definitions/${definition}.yaml, validateResponses: true, }))
It actually works more or less. The requests get validated but the responses do not.
What I also think is very interesting is that when I add "/api", I can seem to use a relative path like this ./../../api-definitions ...
However when I do not use "/api", it is not able to find it this way