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

How to build a registry from a simple OpenAPI document dict? #160

Open
meoyawn opened this issue Jul 4, 2024 · 2 comments
Open

How to build a registry from a simple OpenAPI document dict? #160

meoyawn opened this issue Jul 4, 2024 · 2 comments

Comments

@meoyawn
Copy link

meoyawn commented Jul 4, 2024

I'm trying to build a registry

self.registry = Registry().with_resources(
    (f"#/components/schemas/{k}", Resource.from_contents(v, referencing.jsonschema.DRAFT202012)
    for k, v in openapi["components"]["schemas"].items()
)

but then it fails during validation

jsonschema.Draft202012Validator(registry=registry, schema=schema).validate(dict)

with

self = Resource(contents={'oneOf': [{'type': 'object', 'properties': {'status': {'enum': [200]}, 'headers': {'type': 'object'...['mime', 'body']}]}}, 'required': ['status', 'headers', 'body']}]}, _specification=<Specification name='draft2020-12'>)
pointer = '/components/schemas/YtDlInfo'
resolver = Resolver(_base_uri='', _registry=<Registry (25 resources, 5 uncrawled)>)

    def pointer(self, pointer: str, resolver: Resolver[D]) -> Resolved[D]:
        """
        Resolve the given JSON pointer.
    
        Raises:
    
            `exceptions.PointerToNowhere`
    
                if the pointer points to a location not present in the document
    
        """
        if not pointer:
            return Resolved(contents=self.contents, resolver=resolver)
    
        contents = self.contents
        segments: list[int | str] = []
        for segment in unquote(pointer[1:]).split("/"):
            if isinstance(contents, Sequence):
                segment = int(segment)
            else:
                segment = segment.replace("~1", "/").replace("~0", "~")
            try:
>               contents = contents[segment]  # type: ignore[reportUnknownArgumentType]
E               KeyError: 'components'

how do I correctly build a registry from an OpenAPI dict? Docs don't mention this

Thanks

@Julian
Copy link
Member

Julian commented Aug 6, 2024

I haven't looked at this carefully but two things:

  • Support specifically for the OpenAPI spec is/was planned for but not implemented yet, so it's possible the answer here depends on finishing that work
  • You're tagging the resources with JSON Schema 2020-12, so you're getting behavior for JSON Schema 2020-12, where what you have isn't going to work
  • The reason it won't work is that #/components/schemas/{k} means "look for a components/schemas/k key in this subschema" for each subschema you're looping over -- and that is (probably) incorrect -- you want # to be the parent OpenAPI spec document, as that's presumably the one with the components/schemas sub-properties. So maybe try only adding the parent document and then using $refs to yourSpecDocument#/components/schemas/foo, but whether that works exactly correctly or not depends on any differences in OpenAPI.

@rafalkrupinski
Copy link

Support for OpenAPI would be greatly appreciated.

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

3 participants