cdimascio / express-openapi-validator

🦋 Auto-validates api requests, responses, and securities using ExpressJS and an OpenAPI 3.x specification
MIT License
919 stars 211 forks source link

Path alises using `$ref` not working #758

Open jfoclpf opened 2 years ago

jfoclpf commented 2 years ago

Describe the bug Path alises using $ref not working

To Reproduce

File openapi.yaml has this portion:

  /municipios/{municipio}:
    summary: Detalhes sobre Município  
    get:
      parameters:
        - in: path
          name: municipio
          required: true
          schema:
            type: string
            example: évora
          allowReserved: true
          description: Município
        - $ref: '#/components/parameters/json'
      responses:
        '200':
          description: Detalhes do Município
          content:
            application/json:
              schema:
                type: string

  /municipio/{municipio}:
    $ref: '#/paths/~1municipios~1{municipio}'

Path /municipio/{municipio} is an alias of /municipios/{municipio}

Actual behavior

TypeError: Cannot convert undefined or null to object
  at Function.keys (<anonymous>)
  at SchemaPreprocessor.gatherSchemaNodesFromPaths (/home/joao/dev/geoapi.pt/node_modules/express-openapi-validator/dist/middlewares/parsers/schema.preprocessor.js:79:41)
  at SchemaPreprocessor.preProcess (/home/joao/dev/geoapi.pt/node_modules/express-openapi-validator/dist/middlewares/parsers/schema.preprocessor.js:46:24)
  at /home/joao/dev/geoapi.pt/node_modules/express-openapi-validator/dist/openapi.validator.js:86:95 +0ms
  geoapipt:server html +0ms

Expected behavior No error upon GET /municipio/{municipio} or GET /municipios/{municipio}

Context

My openapi.yaml file was validated OK using ibm-openapi-validator

jfoclpf commented 2 years ago

Considering that $ref: '#/components/parameters/json' was already working, my guess is that this validator does not consider ~1 as valid escaper for /

Nonetheless documentation refers:

Escape Characters

/ and ~ are special characters in JSON Pointers, and need to be escaped when used literally (for example, in path names). Character | Escape With -- | -- ~ | ~0 / | ~1