Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Valid 3.0.0 spec with polymorphism cannot be loaded by validator when validateResponses is true #511

Closed
jrosmithMG opened this issue Jan 5, 2021 · 4 comments · Fixed by #515

Comments

@jrosmithMG
Copy link

OS: macOS Catalina (10.15.7)
nodejs version: 12.14.1
npm version: 6.13.4
express-openapi-validator version: 4.0.0

Observed Behavior
An openapi spec validated by editor.swaggerhub.io cannot be loaded by express-openapi-validator when using polymorphism and setting validateResponses to true.

Expected Behavior
Polymorphic specs should be appropriately loaded by express-openapi-validator and validate responses.

Steps To Reproduce
Using the following example api spec:

openapi: 3.0.0
info:
  title: Example API
  version: 0.1.0
servers:
  - url: https://localhost/
paths:
  /example:
    post:
      description: Request
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PolyObject'
      responses:
        "201":
          description: Response
          content:
            application/json:
              schema:
                type: object
components:
  schemas:
    PolyObject:
      type: object
      discriminator:
        propertyName: object_type
        mapping:
          PolyObject1: '#/components/schemas/PolyObject1'
          PolyObject2: '#/components/schemas/PolyObject2'
      oneOf:
        - $ref: '#/components/schemas/PolyObject1'
        - $ref: '#/components/schemas/PolyObject2'

    PolyObjectBase:
      type: object
      required:
        - object_type
      properties:
        object_type:
          type: string
          enum:
            - PolyObject1
            - PolyObject2
        shared_prop1:
          type: string
        shared_prop2:
          type: string
    
    PolyObject1:
      allOf:
      - $ref: '#/components/schemas/PolyObjectBase'
      - type: object
        properties:
          polyObject1SpecificProp1:
            type: string
          
    PolyObject2:
      allOf:
      - $ref: '#/components/schemas/PolyObjectBase'
      - type: object
        properties:
          polyObject2SpecificProp1:
            type: string

and a basic express setup:

const OpenApiValidator = require('express-openapi-validator');
const express = require('express');
const app = express()

app.use(
    OpenApiValidator.middleware({
        apiSpec: './spec/example.yaml',
        // validateResponses: true // uncommenting validateResponses causes error to be thrown
    })
)

app.listen(3000, () => { console.log("Sever started") })

When validateResponses: true is uncommented, the following exception is thrown:

(node:43565) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'type' of undefined
    at SchemaPreprocessor.handleSerDes (/Users/josh/Projects/example-project/node_modules/express-openapi-validator/dist/middlewares/parsers/schema.preprocessor.js:245:24)
    at SchemaPreprocessor.schemaVisitor (/Users/josh/Projects/example-project/node_modules/express-openapi-validator/dist/middlewares/parsers/schema.preprocessor.js:184:18)
    at /Users/josh/Projects/example-project/node_modules/express-openapi-validator/dist/middlewares/parsers/schema.preprocessor.js:65:74
    at recurse (/Users/josh/Projects/example-project/node_modules/express-openapi-validator/dist/middlewares/parsers/schema.preprocessor.js:127:13)
    at /Users/josh/Projects/example-project/node_modules/express-openapi-validator/dist/middlewares/parsers/schema.preprocessor.js:131:21
    at Array.forEach (<anonymous>)
    at recurse (/Users/josh/Projects/example-project/node_modules/express-openapi-validator/dist/middlewares/parsers/schema.preprocessor.js:129:30)
    at /Users/josh/Projects/example-project/node_modules/express-openapi-validator/dist/middlewares/parsers/schema.preprocessor.js:137:21
    at Array.forEach (<anonymous>)
    at recurse (/Users/josh/Projects/example-project/node_modules/express-openapi-validator/dist/middlewares/parsers/schema.preprocessor.js:135:30)

Notes
This is the smallest snippet I could create to reproduce the error - the actual spec implementing this contains much more complex polymorphic logic. I'm unsure why validateResponses seemingly triggers the issue given that the example spec declares the response to be an object and not any of the polymorphic objects.

@cdimascio
Copy link
Owner

@jrosmithMG this looks like a dupe of #509

@cdimascio
Copy link
Owner

@jrosmithMG thanks for the spec. i can reproduce it. i'll have a look at fixing this (hopefully tomorrow night)

@cdimascio
Copy link
Owner

@jrosmithMG fixed in v4.10.3

@cdimascio
Copy link
Owner

keeping this open as there is additional work to fix this properly

cdimascio added a commit that referenced this issue Jan 11, 2021
cdimascio added a commit that referenced this issue Jan 11, 2021
* feat: skip visited nodes

* fix: remove uneeded clones

* fix: cleanup

* fix: remove yaml parse

* fix: remove merge

* fix: remove uneeded deps

* chore: beta

* fix: skip schemas

* fix: update path to $refs

* fix: visit ref and update deref path

* chore: comment

* test: add polymorphism test for #511
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants