PayU / openapi-validator-middleware

Input validation using Swagger (Open API) and ajv
Apache License 2.0
144 stars 50 forks source link

simple parent child spec fails #151

Closed thebookyouread closed 3 years ago

thebookyouread commented 3 years ago

Using the spec below I get this error: Cannot convert undefined or null to object

openapi: '3.0.0'
info:
  version: 1.0.0
  title: my spec
paths:
  /Persons:
    post:
      requestBody:
        content:
          application/json:
            schema:
             $ref: '#/components/schemas/Person'
      responses:
        '200':
          description: response

components:
  schemas:
    Person:
      type: object
      required:
        - id
      properties:
        id:
          type: number
        name:
          type: string
        parent: 
          $ref: '#/components/schemas/Person'
        children:
          type: array
          items:
            $ref: '#/components/schemas/Person'
kobik commented 3 years ago

Hi @thebookyouread , thanks for reporting the issue.

Can you please share a stack trace of the error?

saputkin commented 3 years ago

Hi, I think this error is a result of "Circular references", i've tested other circular references definitions and got the same error. The following is the error:

`/Users/tests/test_validator/node_modules/api-schema-builder/src/index.js:42
    Object.keys(dereferenced.paths).forEach(currentPath => {
           ^

TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at buildValidations (/Users/tests/test_validator/node_modules/api-schema-builder/src/index.js:42:12)
    at Object.buildSchemaSync (/Users/tests/test_validator/node_modules/api-schema-builder/src/index.js:31:12)
    at Middleware.init (/Users/tests/test_validator/node_modules/openapi-validator-middleware/src/middleware.js:33:41)
    at Object.<anonymous> (/Users/tests/test_validator/index.js:4:19)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)`
ilanKushnir commented 3 years ago

Hi @thebookyouread , we checked the issue in both api-schema-builder and json-schema-deref-sync packages, and the circular references are not supported due to performance and complexity issues. We checked what we can do in our package to solve it and concluded that it will be too complicated and will take us into more issues.

For now, we decided to not support it and we recommend avoiding this pattern as stated in the following issue