Skip to content

Infinite recursion when "$ref" at root. #92

@NfNitLoop

Description

@NfNitLoop

Describe the bug
When the root type of a schema is a $ref, statham has infinite recursion.

Steps to Reproduce

Working schema:

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "required": [
    "name"
  ],
  "additionalProperties": false,
  "definitions": {},
  "$schema": "http://json-schema.org/draft-07/schema#"
}

Schema that breaks statham:

{
  "$ref": "#/definitions/Example",
  "definitions": {
    "Example": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        }
      },
      "required": [
        "name"
      ],
      "additionalProperties": false
    }
  },
  "$schema": "http://json-schema.org/draft-07/schema#"
}

Expected behaviour
I expect both above schemas to output identical/similar models.

Actual behaviour
stack overflow:

Traceback (most recent call last):
  File "/opt/homebrew/bin/statham", line 8, in <module>
    sys.exit(entry_point())
             ^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/statham/__main__.py", line 105, in entry_point
    output.write(main(uri))  # pragma: no cover
                 ^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/statham/__main__.py", line 94, in main
    RefDict.from_uri(input_uri), context_labeller=title_labeller()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/json_ref_dict/ref_dict.py", line 46, in from_uri
    uri, value = _resolve_uri(uri)
                 ^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/json_ref_dict/ref_dict.py", line 11, in _resolve_uri
    return resolve_uri_to_urivalue_pair(uri)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/json_ref_dict/ref_pointer.py", line 183, in resolve_uri_to_urivalue_pair
    remote_uri, value = pointer.resolve_with_uri(document)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/json_ref_dict/ref_pointer.py", line 98, in resolve_with_uri
    remote_uri, remote = self.resolve_remote_with_uri(doc, -1)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/json_ref_dict/ref_pointer.py", line 58, in resolve_remote_with_uri
    resolved_remote_uri, value = resolve_uri_to_urivalue_pair(remote_uri)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/json_ref_dict/ref_pointer.py", line 183, in resolve_uri_to_urivalue_pair

[ … ]

  File "/opt/homebrew/lib/python3.11/site-packages/json_ref_dict/ref_pointer.py", line 98, in resolve_with_uri
    remote_uri, remote = self.resolve_remote_with_uri(doc, -1)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/json_ref_dict/ref_pointer.py", line 51, in resolve_remote_with_uri
    isinstance(doc, abc.Mapping) and isinstance(doc.get("$ref"), str)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen abc>", line 119, in __instancecheck__
RecursionError: maximum recursion depth exceeded in comparison

System information

  • OS: MacOS Ventura 13.3.1a (arm64)
  • Python version: 3.11.3
  • Installed python libraries: (output of pip freeze)
> pip freeze | grep statham
statham-schema==0.14.0

Workaround

I guess I won't use a "$ref" at my root.

Though the schema generator I'm using (https://github.com/StefanTerdell/zod-to-json-schema) does this by default.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions