Skip to content

Can not handle circular references #58

@megachweng

Description

@megachweng

Sample json

{
  "swagger": "2.0",
  "host": "xxxxxxxx",
  "paths": {
    "/": {
      "get": {
        "parameters": [],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "$ref": "#/definitions/ResponseData«List«DcDistrictVO»»"
            }
          }
        },
        "deprecated": false
      }
    }
  },
  "definitions": {
    "DcDistrictVO": {
      "type": "object",
      "properties": {
        "districtList": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/DcProvinceVO"
          }
        },
        "initials": {
          "type": "string"
        }
      },
      "title": "DcDistrictVO"
    },
    "DcProvinceVO": {
      "type": "object",
      "properties": {
        "code": {
          "type": "string"
        },
        "districtList": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/DcDistrictVO"
          }
        },
        "name": {
          "type": "string"
        }
      },
      "title": "DcProvinceVO"
    },
    "ResponseData«List«DcDistrictVO»»": {
      "type": "object",
      "properties": {
        "code": {
          "type": "integer",
          "format": "int32"
        },
        "data": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/DcDistrictVO"
          }
        },
        "message": {
          "type": "string"
        },
        "success": {
          "type": "boolean"
        }
      },
      "title": "ResponseData«List«DcDistrictVO»»"
    }
  }
}

DcProvinceVO and DcDistrictVO are circular referenced, there is not issue when jsonref.loads the raw json, but raise Exception when jsonref.dumps
Traceback:

Traceback (most recent call last):
  File "/Users/megachweng/Documents/Code/Test/zest-engine/zest_engine/cook/swagger/derefer.py", line 7, in <module>
    a = jsonref.dumps(raw_data)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/zest-engine/lib/python3.10/site-packages/jsonref.py", line 556, in dumps
    return json.dumps(obj, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/zest-engine/lib/python3.10/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/zest-engine/lib/python3.10/json/encoder.py", line 201, in encode
    chunks = list(chunks)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/zest-engine/lib/python3.10/json/encoder.py", line 431, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/zest-engine/lib/python3.10/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/opt/homebrew/Caskroom/miniconda/base/envs/zest-engine/lib/python3.10/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/opt/homebrew/Caskroom/miniconda/base/envs/zest-engine/lib/python3.10/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  [Previous line repeated 6 more times]
  File "/opt/homebrew/Caskroom/miniconda/base/envs/zest-engine/lib/python3.10/json/encoder.py", line 340, in _iterencode_dict
    raise ValueError("Circular reference detected")
ValueError: Circular reference detected

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions