-
-
Notifications
You must be signed in to change notification settings - Fork 592
jsonschema validation fails to resolve “grandchild” local file references #398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
|
@awwright this does not work. In any combinations.
No. This is happening only because someone did not think about developers using json schemas locally. |
@USSX-Hares I'm not sure the specifics of this library, but you may need to import the files you're trying to reference beforehand. Check out https://python-jsonschema.readthedocs.io/en/latest/references/#jsonschema.RefResolver and particularly the "store" argument. Using |
@awwright I tried: All of them have the same result |
@USSX-Hares The
Note the three consecutive forward slashes! What's going on here is that Since files are local, there is no "authority" (hostname) component, which usually comes between the |
@handrews you speak about the absolute path. And, did any of you at least try before suggesting? |
@USSX-Hares your attitude isn't welcome here. Fix it, and consider apologizing to the folks above who were trying to help. You also likely should apologize to me for your sass, though I care less about that. Looking at other open issues to see whether this is the same one is another thing you likely should have done, or feel free to send a patch fixing whatever issue you're encountering if you think you're encountering a bug. It's unlikely I'll look at this considering the number of open $ref bugs and my desire to have it rewritten to fix them. You're owed nothing, please act like it. |
This could be fixed with a little patch to the How to fix
Changed project filesref_resolver.py:import os.path
import json
from jsonschema import RefResolver
class ExtendedRefResolver(RefResolver):
def resolve_remote(self, uri):
print(f"Resolving URI: '{uri}'")
path = None
if (uri.startswith('file:')):
path = uri[len('file:'):]
if (path.startswith('//')):
path = path[len('//'):]
elif (os.path.isfile(uri)):
path = uri
if (path is not None):
return self.resolve_local(path)
else:
return super().resolve_remote(uri)
def resolve_local(self, path: str):
with open(path) as file:
schema = json.load(file)
if (self.cache_remote):
self.store[path] = schema
return schema code.py:import os.path
import json
from jsonschema import validate
from ref_resolver import ExtendedRefResolver
contact = \
{
"name": "William Johns",
"age": 25,
"birthDate": { "month": "apr", "day": 15 }
}
def main():
schema_path = "schemas/main_schema.json"
schema = json.load(open(schema_path))
validate(contact, schema, resolver=ExtendedRefResolver(base_uri='file://' + os.path.dirname(os.path.abspath(schema_path)), referrer=schema))
if (__name__ == '__main__'):
main() main_schema.json:{
"title": "MainSchema",
"properties":
{
"name": { "type": "string" },
"age": { "$ref": "dependencies/positive_integer.json" },
"birthDate": { "$ref": "dependencies/date.json" }
},
"additionalProperties": false
} dependencies/date.json:{
"title": "date",
"type": "object",
"properties":
{
"month": { "$ref": "month.json" },
"day": { "$ref": "positive_integer.json" }
},
"additionalProperties": false
} |
57001d26b [294] Add tests for "additionalProperties" and "additionalItems" 27192102c Merge pull request #398 from ChALkeR/chalker/no-dec-ipv4 f5f481a63 Decimal IPs are also not dotted-quad git-subtree-dir: json git-subtree-split: 57001d26bf50598fe16ac3062aba5ddbd650a73c
Background:
I have multiple json schemas referring large same objects.
These objects are moved to a subdirectory.
In the example below, the following dependencies appear:
The jsonschema library fails to resolve only the last dependency, processing all other fine.
Project Tree
code.py:
JSON Schemas
main_schema json
date.json
month.json:
positive_integer.json:
Problem
When I run this, program fails with the stacktrace:
As I investigated, the 'grandchild' dependencies could be resolved only if they were preloaded earlier.
So, if remove the "date => month" dependency, or forcibly "preload" it from the rool level, all would work fine.
Workaround
Modify main_schema.json to be something like this:
If do so, validation would be successful.
However, I really do not like this workaround.
If you have any ideas how to fix it, please, tell me.
System Info:
The text was updated successfully, but these errors were encountered: