swagger-parser icon indicating copy to clipboard operation
swagger-parser copied to clipboard

Circular reference not getting resolved properly

Open niteshagrawalgmail opened this issue 6 years ago • 2 comments

Hi,

We have a swagger json which we want to parse using swagger parser. The swagger is containing circular references. When we observe the parsed output, the circular reference is resolved infinitely.

paths->/A_Sample->post->schema->properties->to_item->to_sample->to_item->to_sample..................

Please refer to screenshot.

Setting dereference { circular : "ignore"} results in non resolution of any available reference (even if they are non-circular) and
Setting dereference { circular : false } results in failure of parsing.

This seems to be a bug.

Swagger parser version used : 6.0.5 circular_reference_issue

sample swagger json :

{
   "swagger":"2.0",
   "info":{
      "title":"Sample API for circular issue",
      "version":"1 ",
      "description":"sample API description"
   },
   "security":[
      {
         "basicAuthentication":[

         ]
      }
   ],
   "securityDefinitions":{
      "basicAuthentication":{
         "type":"basic"
      }
   },
   "schemes":[
      "https"
   ],
   "host":"example.com",
   "basePath":"/niaa",
   "consumes":[
      "application/json"
   ],
   "produces":[
      "application/json"
   ],
   "tags":[
      {
         "name":"A_Sample"
      },
      {
         "name":"A_Item"
      }
   ],
   "paths":{
      "/A_Sample":{
         "post":{
            "summary":"Add new entity to A_Sample",
            "tags":[
               "A_Sample"
            ],
            "parameters":[
               {
                  "name":"A_SampleType",
                  "in":"body",
                  "description":"New entity",
                  "schema":{
                     "$ref":"#/definitions/Sample-create"
                  }
               }
            ],
            "responses":{
               "201":{
                  "description":"Created entity",
                  "schema":{
                     "title":"A_SampleType"
                  }
               },
               "default":{
                  "$ref":"#/responses/error"
               }
            }
         }
      }
   },
   "definitions":{
      "Sample-create":{
         "type":"object",
         "properties":{
            "SampleCreateProperty":{
               "type":"string",
               "maxLength":10,
               "title":"SampleCreateProperty",
               "description":"SampleCreateProperty description"
            },
            "to_Item":{
               "type":"object",
               "properties":{
                  "results":{
                     "type":"array",
                     "items":{
                        "$ref":"#/definitions/Item-create"
                     }
                  }
               }
            }
         },
         "title":"",
         "description":""
      },
      "Item-create":{
         "type":"object",
         "properties":{
            "ItemCreateProperty":{
               "type":"string",
               "maxLength":10,
               "title":"ItemCreateProperty",
               "description":"ItemCreateProperty description"
            },
            "to_Sample":{
               "$ref":"#/definitions/Sample-create"
            },
            "to_other":{
               "$ref":"#/definitions/Other-create"
            }
         },
         "title":"",
         "description":""
      },
      "Other-create":{
         "type":"object",
         "properties":{
            "OtherCreateProperty":{
               "type":"string",
               "maxLength":10,
               "title":"OtherCreateProperty",
               "description":"OtherCreateProperty description"
            }
         },
         "title":"",
         "description":""
      }
   },
   "responses":{
      "error":{
         "description":"Error"
      }
   }
}

niteshagrawalgmail avatar May 10 '19 02:05 niteshagrawalgmail

Having this issue too. I have a schema Person that have field name: string, id: string, and manager: Person

The current behavior is either:

  • resolve everything circular, leading to infinite recursion
  • or don't resolve the entire object.

Would love if there is a way to still dereference non-circular field, and only ignore circular field, and not ignoring the entire object

bhuynhdev avatar Jun 24 '23 22:06 bhuynhdev

I know htis is years later, but this is how redocly-cli outputs the input. Is that what you wanted? If so, lets just close this repo down and tell folks to use that.

swagger: '2.0'
info:
  title: Sample API for circular issue
  version: '1 '
  description: sample API description
host: example.com
basePath: /niaa
schemes:
  - https
consumes:
  - application/json
produces:
  - application/json
security:
  - basicAuthentication: []
tags:
  - name: A_Sample
  - name: A_Item
paths:
  /A_Sample:
    post:
      summary: Add new entity to A_Sample
      tags:
        - A_Sample
      parameters:
        - name: A_SampleType
          in: body
          description: New entity
          schema:
            $ref: '#/definitions/Sample-create'
      responses:
        '201':
          description: Created entity
          schema:
            title: A_SampleType
        default:
          $ref: '#/responses/error'
definitions:
  Sample-create:
    type: object
    properties:
      SampleCreateProperty:
        type: string
        maxLength: 10
        title: SampleCreateProperty
        description: SampleCreateProperty description
      to_Item:
        type: object
        properties:
          results:
            type: array
            items:
              $ref: '#/definitions/Item-create'
    title: ''
    description: ''
  Item-create:
    type: object
    properties:
      ItemCreateProperty:
        type: string
        maxLength: 10
        title: ItemCreateProperty
        description: ItemCreateProperty description
      to_Sample:
        $ref: '#/definitions/Sample-create'
      to_other:
        $ref: '#/definitions/Other-create'
    title: ''
    description: ''
  Other-create:
    type: object
    properties:
      OtherCreateProperty:
        type: string
        maxLength: 10
        title: OtherCreateProperty
        description: OtherCreateProperty description
    title: ''
    description: ''
responses:
  error:
    description: Error
securityDefinitions:
  basicAuthentication:
    type: basic

philsturgeon avatar Aug 18 '23 17:08 philsturgeon