Skip to content

Self-references cause resolves URLs incorrectly and self-referencing objects cause python infinite stack recursion #1

@bsmedberg-xometry

Description

@bsmedberg-xometry

First, thanks for the tool!

I have a JSON schema generated by pydantic. It contains multiple object definitions all in one file. https://pydantic-docs.helpmanual.io/usage/schema/ contains a good example of the generated schema.

This schema references its own object definitions like this:

  "properties": {
    "foo_bar": {
      "$ref": "#/definitions/FooBar"
    },
    "Gender": {
      "$ref": "#/definitions/Gender"
    },

sphinx-json-schema doesn't handle this style of reference, and there are two bugs involved:

  • Relative links are resolved relative to the base directory of the file being loaded, rather than the file itself. This is because of the os.path.dirname call at
    os.path.dirname(stream if type(stream) == 'str' else self.file), value
  • If I correct this by changing the refs to be "$ref": "./MyFile.json#/definitions/FooBar" then the code causes infinite recursion. This is because it tries to keep parsing the same file over and over again.

I spent a few minutes trying to fix this, got a little stuck and I'm wondering about two things:

  • have you considered using URL infrastructure for all resolving? If you pass a base URL of file:///path/to/file then you can use standard URL infrastructure (urllib.parse.urljoin) to resolve URL references, without having to explicitly check for http:
  • Have you looked into using an existing ref-resolver library? At a quick glance, jsonschema.RefResolver appears to do a lot of what you need here relatively simply.

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