Search code examples
node.jsswaggeropenapiexpress-openapi-validator

Priority of paths in Open API 3


I'm using an openapi validator (express-openapi-validator) in my Node.js project and can't figure out how to control the order of the paths matched.

If I have 2 paths such as,

/foo/{type}
   parameters:
     - name: type
       schema:
          type: string
          enum: ['bar', 'bam']

and

/foo/bar

For a request to /foo/bar, the second path is always matched.

How do I control the precedence of this match?


Solution

  • For a request to /foo/bar, the second path is always matched.

    This is the correct and expected behavior. OpenAPI Specification states that specific paths must be matched before similar templated paths - see Path Templating Matching. This is not supposed to be configurable, otherwise the behavior would contradict the specification.

    To have requests to /foo/bar handled by /foo/{type}, you'll need to remove the /foo/bar path from the API definition.