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

OpenAPI v3.1 causes stack overflow #51

Closed
andymurd opened this issue Jan 2, 2023 · 2 comments · Fixed by #52
Closed

OpenAPI v3.1 causes stack overflow #51

andymurd opened this issue Jan 2, 2023 · 2 comments · Fixed by #52

Comments

@andymurd
Copy link

andymurd commented Jan 2, 2023

Hi,

I've been trying to get jsonref to work with the OpenAPI v3.1 spec and always hit an error when the lazy-loaded refs are expanded. OpenAPI v3.0 works fine, so I guess the cause is down to the introduction of a $defs section. Here's an example program:

from pprint import pprint
import jsonref

result = jsonref.load_uri("https://spec.openapis.org/oas/3.1/schema/2022-10-07")
pprint(result)

Running this on my system with Python 3.10 results in this error (with a very long stack trace):

jsonref.JsonRefError: Error while resolving `https://spec.openapis.org/oas/3.1/schema/2022-10-07#/$defs/specification-extensions`: RecursionError: maximum recursion depth exceeded while calling a Python object

Do you have any suggestions for a work-around? Thanks in advance.

@gazpachoking
Copy link
Owner

Huh. That schema is a real head scratcher. It has a root level reference, which points to somewhere within itself. I'm tempted to say that's not even a valid json reference.

@gazpachoking
Copy link
Owner

To elaborate on what I mean, here is a stripped down subset of the schema:

{
  "$ref": "#/$defs/specification-extensions",
  "type": "object",
  "$defs": {
    "specification-extensions": {
      "patternProperties": {"^x-": true}
    }
  }
}

Since this is an object with a $ref property, this whole object is considered a reference object. And it references something inside of iteself. Since this library allows reference uri fragments to point inside of other references in the doc, it tries to resolve the base level reference before doing the json pointer lookup. Since it resolves to itself, this is why we are getting the recursion error. Also #40

The other issue is that OpenAPI is clearly relying on the merging of extra properties from the reference object in with referent data. Since the json reference spec says that implementations may replace a json reference object with the value it refers to, this whole thing would evaluate to just:

{
  "patternProperties": {"^x-": true}
}

(assuming the self referent reference objects are even allowed)
This can be overcome by using the merge_props option to jsonref though. Discussed more here #35

All that being said, I think I figured out a way to make things work. I really wish someone would update the JSON reference spec though, since as is both of these seem outside the current writing, but many libraries/schemas are still using it in the wild.

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

Successfully merging a pull request may close this issue.

2 participants