Skip to content

Nested references are not loaded and the exception is unclear #209

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

Closed
traut opened this issue Mar 20, 2015 · 9 comments
Closed

Nested references are not loaded and the exception is unclear #209

traut opened this issue Mar 20, 2015 · 9 comments
Labels
Invalid Not a bug, PEBKAC, or an unsupported setup

Comments

@traut
Copy link

traut commented Mar 20, 2015

Test case (all files are in the same directory):

File schema-a.json

{
    "$schema": "http://json-schema.org/draft-04/schema",

    "title": "schema A",
    "type": "object",
    "allOf": [
        {"$ref": "file:schema-b.json"}
    ]
}

File schema-b.json:

{
    "$schema": "http://json-schema.org/draft-04/schema",

    "title": "schema B",
    "type": "object",
    "allOf": [
        {"$ref": "file:schema-c.json"}
    ]
}

File schema-c.json:

{
    "$schema": "http://json-schema.org/draft-04/schema",

    "title": "schema C",
    "type": "object"
}

File validate.py:

import json
from os.path import dirname
from jsonschema import validate, RefResolver

with open('schema-a.json', 'r') as f:
    schema_a = json.loads(f.read())

try:
    validate({}, schema_a)
except Exception, e:
    print "1 failed: %s" % e

try:
    CURRENT_DIR = dirname(__file__)
    resolver = RefResolver(base_uri='file://' + CURRENT_DIR + '/', referrer=None)
    validate({}, schema_a, resolver=resolver)
except Exception, e:
    print "2 failed: %s" % e

Result:

$ python validate.py
1 failed: <urlopen error [Errno 2] No such file or directory: '/schema-c.json'>
2 failed: <urlopen error [Errno 2] No such file or directory: '/schema-b.json'>
$
@traut
Copy link
Author

traut commented Mar 21, 2015

my bad, if CURRENT_DIR is not empty (it was when I was testing my code) and base_uri ends with /, validation succeeds.

So there are 2 issues still open for me:
— Is there a reason why jsonschema can't load the files in the same local directory without asking for specific base_uri? It seems like an easy feature to add and would save a lot of user's time during development.
RefResolver behaviour should not be affected by trailing / in base_uri

@wadeaa
Copy link

wadeaa commented Apr 9, 2015

This may be related to the above or may be down to my misunderstanding but I am hoping someone canoffer a little insight.
With the following:

File parameter_value_obj.json:
{
"$schema": "http://json-schema.org/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"description":"Unique identifier for the parameter.",
"type": "string",
"minLength": 1
},
"value": {
"description":"List of values for the parameter.",
"type": "array",
"uniqueItems": true,
"additionalItems": {
"type": "number"
},
"minItems": 1
}
},
"required": [
"value",
"id"
]
}

File results_run.json:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"parameters": {
"description":"List of app_parameters values needed.",
"type": "array",
"uniqueItems": true,
"additionalItems": {
"type": "object",
"$ref": "parameter_value_obj.json"
}
},
"name": {
"type": "string",
"minLength": 1
}
},
"required": [
"parameters",
"name"
]
}

Code:

json_body = {"name": "6", "parameters": [{"wibble": 23}]}
schema_file = "/home/user/schemata/results_run.json"
schema_path = "/home/user/schemata/"

with open(schema_file, "r") as res_file:
result_read = res_file.read().replace('\n', '')

try:
json_schema = json.loads(result_read)
except ValueError as detail:
logger.warning("ValueError loading schema: "+str(detail))

try:
resolver = jsonschema.RefResolver('file://' + schema_path + '/', None)
jsonschema.validate(json_body, json_schema, resolver=resolver)
except Exception, e:
logger.warning("Validation Failed")

I get no exceptions thrown, the schema is accepted and the validation seemingly passes - But it shouldn't, as the object within the "parameters" array is clearly not of type parameter_value_obj. Is this likely to be because I am not properly resolving the referrenced file? Or is this a bug in validation?

@traut
Copy link
Author

traut commented Apr 9, 2015

@wadeaa I recommend you to try not to reference parameter_value_obj.json directly, but put it in definitions block, like that - https://github.com/traut/taxii-json-schemas-draft/blob/master/schemas/message.json#L7 - and reference it with local path #definitions/name

this works nicely for me

@wadeaa
Copy link

wadeaa commented Apr 10, 2015

Hi Traut, thanks. Yes that is the way I have it now, and it does work. But the documentation is growing and I want to be able to reuse certain objects in multiple schemas. It would make sense to be able to only define them (and also update them) once in their own files.

I just can't see how it can be properly loading the reference if it is passing validation, but then I am not getting any SchemaError either. I will keep playing with it.

@traut
Copy link
Author

traut commented Apr 10, 2015

@wadeaa if you look around in that repo I referenced, you'll see that I'm reusing some primitives there, referencing them in different (bigger) schemas

@gregbillock
Copy link

@traut This looks like the same issue I'm having in #274 . Could you give a bit more detail about setting the CURRENT_DIR variable and base_uri, and corresponding workarounds for specifying the $ref: file:... values in nested schemas?

@traut
Copy link
Author

traut commented Feb 22, 2016

@gregbillock
Copy link

Thanks @traut. I was diving into the code to see if I needed a resolver subclass to get the job done, but this works well for what I'm doing right now. Thanks!

Julian added a commit that referenced this issue Dec 8, 2017
86f965e Merge pull request #209 from json-schema-org/refactor-format
f056cb7 fix: incorrect relative-json-pointer test, closes #208
814eb16 refactor: split format.json to multiple files, closes #162
870b9f7 Merge pull request #207 from json-schema-org/draft-7-ajv
3d9830a feat: test draft-7 with Ajv
9ce7b4c Merge pull request #200 from handrews/draft-07
cf6e663 Apparently descriptions must be < 60 chars
1042cd6 Update draft coverage information
0680b46 Restore regex format test from draft-03
f58637d Basic relative json pointer tests for format
1b43ffa Partial IRI and IRI-reference test suite
9b020b8 Add idn-email and idn-hostname format tests
ca342c9 Add tests for "date" and "time" formats
0d64501 Tests for contentMediaType and contentEncoding
ea357f3 Update draft coverage information
321db9a Tests for if, then, and else keywords
dedd3f1 Unmodified copy of draft6 tests to draft7

git-subtree-dir: json
git-subtree-split: 86f965e53dda0b6c57e70ddd726243e1e061cf84
@Julian Julian added the Needs Simplification An issue which is in need of simplifying the example or issue being demonstrated for diagnosis. label Mar 24, 2020
@Julian
Copy link
Member

Julian commented Jul 28, 2022

Looking back at this (after ages, apologies), but

— RefResolver behaviour should not be affected by trailing / in base_uri

This is the same as #274 (comment) and the other duplicates mentioned there -- essentially that's just how relative URIs work, this library is just following the RFC (3986 in this case), but it's similar behavior to what you're all going to be used to from a browser.

There's really 2 options:

  • remember to use a trailing slash, as you did ultimately
  • always use a file as your base URI (e.g. one of your schemas) rather than a directory

@Julian Julian closed this as completed Jul 28, 2022
@Julian Julian added Invalid Not a bug, PEBKAC, or an unsupported setup and removed Needs Simplification An issue which is in need of simplifying the example or issue being demonstrated for diagnosis. labels Jul 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Invalid Not a bug, PEBKAC, or an unsupported setup
Projects
None yet
Development

No branches or pull requests

4 participants