10000 How to resolve local references with jsonschema/referencing · Issue #140 · python-openapi/openapi-schema-validator · GitHub
[go: up one dir, main page]

Skip to content
How to resolve local references with jsonschema/referencing #140
Closed
@wosc

Description

@wosc

Hi, so I'm sorry for the probably dumb question, but I've been banging my head against this and am getting nowhere.

I have an openapi spec like this

components:
  schemas:
    something:
      type: array
      items:
        "$ref": "#/components/schemas/uuid"
    uuid:
      pattern: "^((\\{urn:uuid:)?([a-f0-9]{8})\\}?)$"
      type: string

Previously I could do validation like so:

schema = yaml.safe_load(open('openapi.yaml').read())
resolver = jsonschema.validators.RefResolver.from_schema(schema)
openapi_schema_validator.validate(
    ['{urn:uuid:deadbeef}'], 
    schema=schema['components']['schemas']['something'], 
    resolver=resolver)

But after the API switch to referencing, I'm stumped on how to create a Registry that "just" resolves these local pointers. I've tried following the guide in https://openapi-schema-validator.readthedocs.io/en/latest/references.html, like so

schema = yaml.safe_load(open('openapi.yaml').read())
definitions = schema['components']['schemas']
registry = referencing.Registry().with_resources([
    (f'urn:{k}-schema', referencing.Resource.from_contents(
        v, default_specification=DRAFT202012))
    for k, v in definitions.items()
])
openapi_schema_validator.validate(
    ['{urn:uuid:deadbeef}'],
    schema=schema['components']['schemas']['something'],
    registry=registry)

but that raises PointerToNowhere: '/components/schemas/uuid' does not exist within {'type': 'array', 'items': {'$ref': '#/components/schemas/uuid'}}

Thanks for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0