codekie / openapi-examples-validator

Validates embedded examples in OpenAPI-files
MIT License
57 stars 11 forks source link

UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded #118

Open mohamedelhabib opened 3 years ago

mohamedelhabib commented 3 years ago

Hello

i'am using the openapi-examples-validator (4.2.0) to validate a openapi spec yaml that contains a recursive object. i got this error below a example openapi document

(node:19) UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded
    at Function.keys (<anonymous>)
    at crawl (/usr/lib/node_modules/openapi-examples-validator/node_modules/@apidevtools/json-schema-ref-parser/lib/resolve-external.js:62:30)
    at crawl (/usr/lib/node_modules/openapi-examples-validator/node_modules/@apidevtools/json-schema-ref-parser/lib/resolve-external.js:70:38)
    at crawl (/usr/lib/node_modules/openapi-examples-validator/node_modules/@apidevtools/json-schema-ref-parser/lib/resolve-external.js:70:38)
    at crawl (/usr/lib/node_modules/openapi-examples-validator/node_modules/@apidevtools/json-schema-ref-parser/lib/resolve-external.js:70:38)
    at crawl (/usr/lib/node_modules/openapi-examples-validator/node_modules/@apidevtools/json-schema-ref-parser/lib/resolve-external.js:70:38)
    at crawl (/usr/lib/node_modules/openapi-examples-validator/node_modules/@apidevtools/json-schema-ref-parser/lib/resolve-external.js:70:38)
    at crawl (/usr/lib/node_modules/openapi-examples-validator/node_modules/@apidevtools/json-schema-ref-parser/lib/resolve-external.js:70:38)
    at crawl (/usr/lib/node_modules/openapi-examples-validator/node_modules/@apidevtools/json-schema-ref-parser/lib/resolve-external.js:70:38)
    at crawl (/usr/lib/node_modules/openapi-examples-validator/node_modules/@apidevtools/json-schema-ref-parser/lib/resolve-external.js:70:38)
(node:19) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 4)
(node:19) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
openapi: 3.0.3
info:
  version: 1.0.0
  title: recurive-api
paths:
  /categories:
    get:
      tags:
        - categories
      operationId: listCategories
      responses:
        200:
          description: List of categories retrieved Successfully
          content:
            'application/json':
              schema:
                $ref: '#/components/schemas/Node'
components:
  schemas:
    Node:
      title: Node
      description: This is a recursive element and can hold categories, sub-categories and products
      type: object
      properties:
        nodes:
          type: array
          items:
            $ref: '#/components/schemas/Node'
codekie commented 3 years ago

Yes. In the current implementation that's not going to work because before the validation starts the spec is dereferenced and in this case it ends up in an infinite recursion. I have to check if there's an easy way to solve this.

codekie commented 3 years ago

I just had a look at the JSON-schema validator that I use to validate the OpenAPI-description against the examples (which is ajv) and they don't support circular references (see: https://github.com/ajv-validator/ajv/blob/HEAD/docs/security.md#circular-references-in-javascript-objects). So I'll have to put this on hold until they support it.

mohamedelhabib commented 3 years ago

But the issue seem to be related to @apidevtools/json-schema-ref-parser and not to ajv-validator ?

codekie commented 3 years ago

The only way I see to resolve this is to not dereference it in this case but instead keeping the reference pointer in the JSON-schema but then I'd have that circular reference that ajv can't handle.