gazpachoking / jsonref

Automatically dereferences JSON references within a JSON document.
http://jsonref.readthedocs.org
MIT License
122 stars 28 forks source link

Can not handle circular references #58

Open megachweng opened 1 year ago

megachweng commented 1 year ago

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
deckchairhq commented 11 months ago

+1

synnack commented 2 months ago

It works if you forego the optional indent= parameter to jsonref.dumps(), so this code is a very inefficient workaround:

    unref = json.loads(jsonref.dumps(db))
    print(json.dumps(unref, indent=2))

I had the same issue when I passed indent=2 to jsonref.dumps()