python-jsonschema / python-jsonschema/referencing

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

Open
#160 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
53
Forks
27
Avg merge
1d 8h
Merged PRs (30d)
8

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Registry().with_resources, Resource.from_contents, and the Draft202012Validator(registry=registry, schema=schema) example in the issue. Compare the OpenAPI components/schemas entries with the failing JSON-pointer lookup and check the referencing documentation for registry construction. Done means the OpenAPI dictionary can be registered and validation no longer raises the shown KeyError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.