APIDevTools / swagger-parser

Swagger 2.0 and OpenAPI 3.0 parser/validator
https://apitools.dev/swagger-parser
MIT License
1.09k stars 154 forks source link

TypeError: Converting circular structure to JSON #167

Closed iammelvin closed 3 years ago

iammelvin commented 3 years ago

I am using the online tool at apitools.dev for validating my open API spec and I get the below error but when testing the same with swagger editor I am not getting any errors. OData with . Net Core is been used for REST API's. Need some help to understand whether I have anything wrong in my swagger json file or its some issue with the tool itself.

This API is valid, but it cannot be shown because it contains circular references

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'properties' -> object with constructor 'Object'
    |     property 'Assembly' -> object with constructor 'Object'
    |     ...
    |     property 'GenericTypeParameters' -> object with constructor 'Object'
    --- property 'items' closes the circle
    at JSON.stringify (<anonymous>)
    at toText (https://apitools.dev/swagger-parser/online/js/bundle.min.js:17:226677)
    at Function.editors.addResult (https://apitools.dev/swagger-parser/online/js/bundle.min.js:17:227813)
    at Function.editors.showResult (https://apitools.dev/swagger-parser/online/js/bundle.min.js:17:227094)
    at https://apitools.dev/swagger-parser/online/js/bundle.min.js:17:231618
    at Array.forEach (<anonymous>)
    at https://apitools.dev/swagger-parser/online/js/bundle.min.js:17:231598
philsturgeon commented 3 years ago

There's a lot of different types of circular references so an example would be super handy. If you have a recreateable file or something?

philsturgeon commented 3 years ago

Cannot recreate.

solimant commented 1 year ago

@philsturgeon - here's an example:

openapi: 3.0.0
info:
  title: Employees API
  version: 0.0.1
paths:
  /employees:
    get:
      summary: Returns a list of employees.
      responses:
        '200':
          description: A JSON array of employees
          content:
            application/json:
              schema: 
                type: array
                items:
                  $ref: '#/components/schemas/Employee'
components:
  schemas:
    Employee:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        manager:
          $ref: '#/components/schemas/Employee'

This will give the following error because of the manager property under the Employee type:

This API is valid, but it cannot be shown because it contains circular references

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'properties' -> object with constructor 'Object'
    --- property 'manager' closes the circle
    at JSON.stringify (<anonymous>)
    at toText (https://apitools.dev/swagger-parser/online/js/bundle.min.js:29:40049)
    at editors.addResult (https://apitools.dev/swagger-parser/online/js/bundle.min.js:29:41185)
    at editors.showResult (https://apitools.dev/swagger-parser/online/js/bundle.min.js:29:40466)
    at https://apitools.dev/swagger-parser/online/js/bundle.min.js:29:44991
    at Array.forEach (<anonymous>)
    at https://apitools.dev/swagger-parser/online/js/bundle.min.js:29:44971