python-jsonschema / referencing

Cross-specification JSON referencing (JSON Schema, OpenAPI, and the one you just made up!)
https://referencing.readthedocs.io/
MIT License
35 stars 12 forks source link

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

Open meoyawn opened 2 months ago

meoyawn commented 2 months ago

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 commented 1 month ago

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

rafalkrupinski commented 2 weeks ago

Support for OpenAPI would be greatly appreciated.