Giter VIP home page Giter VIP logo

Comments (7)

EMAN6 avatar EMAN6 commented on July 19, 2024

Does the library supports local references? Like if I have common.json and then schema1.json and schema2.json, where I would like to use something defined in common.json. All files in the same directory. I noticed that it tried to download the referenced schema but I would prefer to use the local file - just giving relative path as "$ref": "common.json#/definitions/xyz".

Have you find a way to solve this problem ?

from python-fastjsonschema.

MarHerUMR avatar MarHerUMR commented on July 19, 2024

@EMAN6 Have a look here: https://github.com/robofit/arcor2/blob/6b57dc4a7431fab77a72d5c8547157a8607cea94/helpers.py#L60

from python-fastjsonschema.

heitorlessa avatar heitorlessa commented on July 19, 2024

hey @horejsek is this something we could contribute to fastjsonschema? In case you're interested in supporting it.

from python-fastjsonschema.

horejsek avatar horejsek commented on July 19, 2024

@heitorlessa Hi, it is already supported. You can pass handlers parameter where you can define how to retrieve your local references.

from python-fastjsonschema.

WilliamHarvey97 avatar WilliamHarvey97 commented on July 19, 2024

Personnally, I have a hard time using the handlers parameter. It is never ran and I get a urllib.error.URLError: <urlopen error [Errno 2] No such file or directory: '/soil_sample.schema.json'>. It makes me think my handler did not have the chance to change the ref to the absolute path because it was never ran.

from python-fastjsonschema.

horejsek avatar horejsek commented on July 19, 2024

@WilliamHarvey97 could you provide smallest runnable code to showcase the problem? Ideally with opening new issue.

from python-fastjsonschema.

jpetersen23 avatar jpetersen23 commented on July 19, 2024

I've made an example to demonstrate the incorrect behavior I'm seeing with local references. It might be that I'm using handlers incorrectly or my schema is invalid, but I don't think so.

I tested this with (Python 3.8.10) :
jsonschema==4.21.1
fastjsonschema==2.19.1

I've inlined the schemas into the python file to make this easier to debug, but it wouldnt have worked correctly if it was in an external file either.

import fastjsonschema
from jsonschema import Draft7Validator, RefResolver
from referencing import Registry, Resource
from referencing.jsonschema import DRAFT7


def remotes_handler(uri):
    print(f"remotes_handler: {uri}")
    raise Exception("remotes_handler: {uri}")
    # req = urlopen(uri)
    # encoding = req.info().get_content_charset() or "utf-8"
    # return json.loads(
    #    req.read().decode(encoding),
    # )


handlers = {"": remotes_handler}

shared_schema = {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "shared_schema.template.json",
    "type": "object",
    "definitions": {"metadata": {"type": "object"}},
}

schema = {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "id_path/example.template.json",
    "type": "object",
    "additionalProperties": {
        "type": "object",
        "properties": {
            "metadata": {"$ref": "shared_schema.template.json#/definitions/metadata"},
        },
    },
}

json_data = {"tacos": {"metadata": {"version": "1.0.0"}}}

# resolver = RefResolver(base_uri="file:///", referrer=handlers)

schema_resource = Resource.from_contents(shared_schema)
registry = Registry().with_resource(
    uri="id_path/shared_schema.template.json",
    resource=schema_resource,
)

validator = Draft7Validator(schema, registry=registry)
errors = sorted(validator.iter_errors(json_data), key=lambda e: e.path)
print(errors)

print(f"is_valid: {validator.is_valid(json_data)}")
validator = fastjsonschema.compile(
    schema,
    handlers=handlers,
)

# this doesnt get reached, because resolver is incorrect.
validator(json_data)

What seem to be happening is that inside RefResolver's resolving, its incorrectly concatenating paths with the $id's path:
https://github.com/horejsek/python-fastjsonschema/blob/master/fastjsonschema/ref_resolver.py#L126

When doing so it also gets into a recursive look of resolving references until my handler gets called, at which point the url sent to it is:
'id_path/id_path/id_path/id_path/id_path/shared_schema.template.json'

I'm not sure exactly the right general solution, but one idea is if this behavior is generally wanted, then for my case I could pass in my own RefResolver, then I could correct for the behavior on my side without strange hacks).

Since I also wanted to make sure my schemas arent somehow invalid, I validated them using python jsonschema which is also in this example.

from python-fastjsonschema.

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.