NIKHIL C M
NIKHIL C M

Reputation: 4236

express-openapi-validator: ERROR: TypeError: Cannot read property 'schema' of undefined

I have a openapi schema definition like this:

openapi: 3.0.0
    info:
      title: Manual Handling
      description: API documentation for manual handling.
      version: 0.1.9
    servers:
      - url: /
        description: Self
      - url: http://localhost:3010
        description: local
      - url: https://zz.zzz.in
        description: Development server
    paths:
     /api/v1/meeting/{meetingId}:
        get:
          description: Get meeting details by meeting id
          summary: Get meeting details by meeting id
          tags:
            - Meeting
          parameters:
            - name: meetingId
              in: path
              required: true
              description: Meeting id
              schema:
                $ref: '#/components/parameters/MeetingId'
          responses:
            '200':
              description: Meeting token obtained successfully
              content:
                application/json:
                  schema:
                    $ref: '#/components/parameters/MeetingId'
          security:
            - bearerAuth: []
    components:
      securitySchemes:
        bearerAuth:
          type: http
          scheme: bearer
          bearerFormat: JWT
      parameters:
        MeetingId:
          name: meetingId
          description: Meeting id of the session
          required: true
          in: path
          example: 01701deb-34cb-46c2-972d-6eeea3850342
          schema:
            type: string

and enabled validator for request

new OpenApiValidator({
    apiSpec: spec,
    validateRequests: true,
}).installSync(app);

and called /api/v1/meeting/{meetingId} api. Instead of it doing a validation it throws an error

ERROR:  TypeError: Cannot read property 'schema' of undefined
    at dereferenceSchema (/Users/nikhilcm/nikhil/projects/manual_handling_backend/node_modules/express-openapi-validator/dist/middlewares/parsers/util.js:47:24)
    at Object.normalizeParameter (/Users/nikhilcm/nikhil/projects/manual_handling_backend/node_modules/express-openapi-validator/dist/middlewares/parsers/util.js:23:18)
    at /Users/nikhilcm/nikhil/projects/manual_handling_backend/node_modules/express-openapi-validator/dist/middlewares/parsers/schema.parse.js:32:45
    at Array.forEach (<anonymous>)
    at ParametersSchemaParser.parse (/Users/nikhilcm/nikhil/projects/manual_handling_backend/node_modules/express-openapi-validator/dist/middlewares/parsers/schema.parse.js:28:20)
    at RequestValidator.buildMiddleware (/Users/nikhilcm/nikhil/projects/manual_handling_backend/node_modules/express-openapi-validator/dist/middlewares/openapi.request.validator.js:57:41)
    at RequestValidator.validate (/Users/nikhilcm/nikhil/projects/manual_handling_backend/node_modules/express-openapi-validator/dist/middlewares/openapi.request.validator.js:48:37)
    at requestValidationHandler (/Users/nikhilcm/nikhil/projects/manual_handling_backend/node_modules/express-openapi-validator/dist/index.js:158:79)
    at Layer.handle [as handle_request] (/Users/nikhilcm/nikhil/projects/manual_handling_backend/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/nikhilcm/nikhil/projects/manual_handling_backend/node_modules/express/lib/router/index.js:317:13)
    at /Users/nikhilcm/nikhil/projects/manual_handling_backend/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/nikhilcm/nikhil/projects/manual_handling_backend/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/nikhilcm/nikhil/projects/manual_handling_backend/node_modules/express/lib/router/index.js:275:10)
    at /Users/nikhilcm/nikhil/projects/manual_handling_backend/node_modules/express-openapi-validator/dist/middlewares/openapi.security.js:59:17

The documentation is verified in swagger editor, no errors were shown. Raised the issue in their github page as well.

Upvotes: 4

Views: 5860

Answers (1)

Helen
Helen

Reputation: 98022

The problem is here:

      parameters:
        - name: meetingId
          in: path
          required: true
          description: Meeting id
          schema:
            $ref: '#/components/parameters/MeetingId'   # <-------

schema can only reference schema components (i.e. #/components/schemas/...) and cannot reference parameters (i.e. #/components/parameters/...). Parameter components are supposed to be referenced from the parameter list.

Replace this part with either

1)

      parameters:
        - $ref: '#/components/parameters/MeetingId'

or


      parameters:
        - name: meetingId
          in: path
          required: true
          description: Meeting id of the session
          example: 01701deb-34cb-46c2-972d-6eeea3850342
          schema:
            type: string

In this case also remove the (unused) parameter definition from the components/parameters section.

Upvotes: 2

Related Questions