Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bsmedberg-xometry opened this issue Feb 21, 2022 · 0 comments

Comments

@bsmedberg-xometry
Copy link

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant