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

ignorePaths not taken in account when a request validation is perform

Open SBKila opened this issue 10 months ago • 0 comments

Describe the bug A clear and concise description of what the bug is.

I have to manage 2 openapi files for the same route openapi file1 define path /items/{id} openapi file2 define path /items

in file2 path /items has a header mandatory not defined in file1

To do that I create 2 middleware and using ignorePaths to prevent mismatch between /items and /items/{id}

const openApiValidator = require('express-openapi-validator');
const openapiValidatorMiddleware1 = openApiValidator.middleware({
    apiSpec: "./api/file1.yaml",
    validateRequests: true, // (default)
    validateResponses: true, // false by default
    ignoreUndocumented: true,
    ignorePaths: (path) => path.endsWith('/items')
});
const openapiValidatorMiddleware2 = openApiValidator.middleware({
    apiSpec: "./api/file2.yaml",
    validateRequests: true, // (default)
    validateResponses: true, // false by default
    ignoreUndocumented: true,
    ignorePaths: (path) => !path.endsWith('/items')
});

router.use(openapiValidatorMiddleware2);
router.use(openapiValidatorMiddleware1);

To Reproduce when a request targeting /items with the specific header is received,

Actual behavior validate method of openapiValidatorMiddleware2 is call with openapi.expressRoute equals to /items , and because it is the first call, the method buildMiddleware is call with success then validate method of openapiValidatorMiddleware1 is call is call with openapi.expressRoute equals to /items , and because it is the first call, the method buildMiddleware is call but fails. it fails because reqSchema (openapi.schema) do not match at all the open api document (file1)

the line 56 of openapi.request.validator.js const schemaParser = new schema_parse_1.ParametersSchemaParser(this.ajv, apiDoc); is not able to find the parameter because it is define in file2 and not in file1

so reqSchema.parameters is undefined and for sure an exception is raised when schemaParser.parse is call when trying to get parameters.in with a TypeError: Cannot read properties of undefined (reading 'in') error

Expected behavior As far as the path is matching ignorePaths method this validation should not be performed

I'm quite sure the same will happen for response validation

Thanks for your help

SBKila avatar Feb 25 '25 10:02 SBKila