Open
Description
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
Activity
Julian commentedon Aug 6, 2024
I haven't looked at this carefully but two things:
#/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 thecomponents/schemas
sub-properties. So maybe try only adding the parent document and then using$ref
s toyourSpecDocument#/components/schemas/foo
, but whether that works exactly correctly or not depends on any differences in OpenAPI.rafalkrupinski commentedon Sep 1, 2024
Support for OpenAPI would be greatly appreciated.
nitg16 commentedon Feb 22, 2025
For anyone who is looking for a solution, this worked for me.