Git Product home page Git Product logo

Comments (12)

lawilog avatar lawilog commented on June 15, 2024 2

Indeed not even the example on https://openapi-schema-validator.readthedocs.io/en/latest/references.html works.

jsonschema.exceptions._WrappedReferencingError: PointerToNowhere: '/components/schemas/Name' does not exist within {'type': 'object', 'required': ['name'], 'properties': {'name': {'$ref': '#/components/schemas/Name'}, 'age': {'$ref': '#/components/schemas/Age'}, 'birth-date': {'$ref': '#/components/schemas/BirthDate'}}, 'additionalProperties': False}

How should the connection from "#/components/schemas/Name" to "urn:name-schema" be established?

from openapi-schema-validator.

wosc avatar wosc commented on June 15, 2024 2

@Cynocracy Thanks for the full code example! I think understand your approach better now, namely validating against the whole schema. My uneducated guess from my short debugging session is that your example should still work even if you omit the with_resources() call.

Unfortunately, my use case is validating that the given data matches one part precisely, and not just "is valid under any possibilities given in the schema", e.g. in my simplified example I want to validate that the data is a list of uuid, and not a single uuid. So using the whole schema does not work there, I think.

from openapi-schema-validator.

AlexeyKatanaev avatar AlexeyKatanaev commented on June 15, 2024 1

Guys, if you need WA for the current issue I did the following:

  • replaced #/components/schemas/Name with urn:Name in the schema
import collections.abc

def update(out, intput):
    for key, val in intput.items():
        if isinstance(val, collections.abc.Mapping):
            out,[key] = update(out.get(key, {}), val)
        elif key == "$ref":
            out[key] = val.replace("#/components/schemas/", "urn:")
        else:
            out,[key] = val
    return out,
schema = {}
update(schema, schema_with_PointerToNowhere)
  • populatedRegistry with these urns
resources = [(f"urn:{res_name}", Resource.from_contents(res, default_specification=DRAFT202012))
                 for res_name, res in schema['components']['schemas'].items()]
registry = Registry().with_resources(resources)

from openapi-schema-validator.

Cynocracy avatar Cynocracy commented on June 15, 2024 1

@wosc

Maybe a little more context to what I wrote helps?

I was taking the full apidoc for an openAPI service (saved to schema.json) and doing

from openapi_schema_validator import validate
from referencing import Registry, Resource
from referencing.jsonschema import DRAFT202012
import json

f = open('schema.json')
schema = json.load(f)

resources = [(f"#/components/schemas/{res_name}", Resource.from_contents(res, default_specification=DRAFT202012))
                 for res_name, res in schema['components']['schemas'].items()]
registry = Registry().with_resources(resources)

create_schema = schema["components"]["requestBodies"]["MYBODY"]["content"]["application/json"]["schema"]

from jsonschema import Draft202012Validator
validator = Draft202012Validator(
    create_schema,
    registry=registry,
)

validator.validate({ "foo": "bar"})

I agree that the inclusion of the urn reference in the thing being validated in the docs confused me, and I don't fully understand that bit :)

I've just now had a brief debugging session. It seems to me that #/components... uris are looked up in two stages. With my code example in the issue description, there's firstly a lookup on base_uri='', and then afterwards the fragment is tried to resolve as a pointer /components/schema/uuid in the Resource that was found there, which fails

Huh, now I'm curious how my example worked... looking at it a bit now 🕵️‍♂️

from openapi-schema-validator.

Cynocracy avatar Cynocracy commented on June 15, 2024
resources = [(f"#/components/schemas/{res_name}", Resource.from_contents(res, default_specification=DRAFT202012))
                 for res_name, res in schema['components']['schemas'].items()]
registry = Registry().with_resources(resources)

seems to work for me, without the replacement of "#/components/schemas/ -> urn:

If this is supported by Registry (I haven't really read through their docs well enough to know :)), maybe the example just needs updating?

edit: incorrect! see below

from openapi-schema-validator.

wosc avatar wosc commented on June 15, 2024

I'm sorry to say, @AlexeyKatanaev the idea you propose is exactly what the docs instruct to do, and what I've already tried to do (see issue description), but unfortunately this simply does not work (and it's not just me, @lawilog also confirmed it).

@Cynocracy thanks for the suggestion, but I've now tried f'#/components/schemas/{name}' and f'/components/schemas/{name}' and also both of those with an additional urn: prefix, but none of those work for me, unfortunately.

I've just now had a brief debugging session. It seems to me that #/components... uris are looked up in two stages. With my code example in the issue description, there's firstly a lookup on base_uri='', and then afterwards the fragment is tried to resolve as a pointer /components/schema/uuid in the Resource that was found there, which fails. Because it further seems to me that in the call of validate(['{urn:uuid:deadbeef}'], schema=schema['components']['schemas']['something'], registry=registry) the base_uri='' is already occupied by the value of the schema= parameter. So there is no chance at all to resolve these references, because of course the something definition does not also contain the referenced uuid definition, as that's the whole point of the reference in the first place.

So I'm sorry to say, I'm still completely stuck on how to migrate local references from RefResolver to referencing.Registry, and would appreciate any help. Thanks!

from openapi-schema-validator.

Cynocracy avatar Cynocracy commented on June 15, 2024

@wosc Ah yeah, you're correct! It turns out the schema I was using already had $refs evaluated, so the registry was doing a whole lot of nothing.

edit: can also confirm that using the YAML with the $refs in it quickly becomes a headache, because my $refs also have $refs and that doesn't seem to be handled correctly, afaict, even with the find replace to urn:

from openapi-schema-validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.