Skip to content
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

Usage of $ref causes error under certain circumstances #190

Open
RST-J opened this issue Nov 27, 2014 · 8 comments
Open

Usage of $ref causes error under certain circumstances #190

RST-J opened this issue Nov 27, 2014 · 8 comments
Labels

Comments

@RST-J
Copy link
Contributor

RST-J commented Nov 27, 2014

While finishing #188 I found out that there is an issue with $ref under certain circumstances.
If you checkout the branch any_ref_issue from my fork and run the tests you'll see

  1) Failure:
UriRelatedTest#test_schema_ref_from_file_with_spaces [/home/jonas/repos/json-schema/test/test_uri_related.rb:59]:
{"names"=>[{"first"=>"john"}]} should be valid for schema:
{"$schema"=>"http://json-schema.org/draft-04/schema#", "type"=>"object", "required"=>["names"], "properties"=>{"names"=>{"type"=>"array", "items"=>{"anyOf"=>[{"$ref"=>"test/schemas/ref john with spaces schema.json"}]}}}}.
--- expected
+++ actual
@@ -1 +1,4 @@
-[]
+["The property '#/names/0' of type Hash did not match one or more of the required schemas. The schema specific errors were:
+
+- anyOf #0:
+    - The property '#/names/0' was not a valid schema"]

I tracked it down to this spot where the schema is registered with full file URI but the determined schema key is just a path (which should be absolutized as in normalized URI).

(byebug) JSON::Validator.schemas.keys
["fd615350-b519-5597-a476-eb93ab9046b6#", "file:///home/jonas/repos/json-schema/test/schemas/ref%20john%20with%20spaces%20schema.json#"]
# but
(byebug) schema_key
"test/schemas/ref john with spaces schema.json#"

I feel like normalized_uri should be used here too. Has anyone a quick idea of how to do that?

P.S.: And why has the registered schema's key a # at the end?

@RST-J RST-J added the Bug label Nov 27, 2014
@pd
Copy link
Contributor

pd commented Nov 28, 2014

The registered schemas all get an empty fragment because we use uri.fragment = '' when we create them:

uri = Addressable::URI.parse('http://example.com/hi')
uri.to_s #=> "http://example.com/hi"
uri.fragment = ''
uri.to_s #=> "http://example.com/hi#"

If we assigned nil instead, we wouldn't have the empty fragment. I've considered changing it before but didn't really understand the consequences, so left it alone. =)

@RST-J
Copy link
Contributor Author

RST-J commented Nov 30, 2014

@pd so, you suggest that we never register schemas with a hash rather than keep that and looking them up with adding one if it is missing?

@RST-J RST-J mentioned this issue Nov 30, 2014
@iainbeeston
Copy link
Contributor

I'd have thought that, because $ref's for a schema always end in '#' it would be simpler to store them with that. Also, surely that way is optimised for the common case? (lookup is faster, at the expense of an extra operation during storage)

@RST-J
Copy link
Contributor Author

RST-J commented Dec 1, 2014

TBH, I was not sure about which way to go (and speculated you'll interfere if I pick the wrong choice), with or without # and wondered about which would be faster (which is the only criterion here I think).

Is it really required that $ref's end with #?

To increase performance, it would be necessary to reliably determine when a normalization is required and when not, so that we can skip calling addressable if an URI already is normalized. But I have no idea how to do that.

@iainbeeston
Copy link
Contributor

@hoxworth might be a good person to ask about this. Or I could poke to common test suite guys, who are usually good at pointing out the correct way to interpret the spec

@RST-J
Copy link
Contributor Author

RST-J commented Dec 1, 2014

Sure, if they can give us any useful hints we appreciate that.

@iainbeeston
Copy link
Contributor

I decided to post it on the json-schema google group first (rather than raising an issue on the common test suite just to ask a question)

https://groups.google.com/forum/#!topic/json-schema/9KdnjylLp8k

@BGarretson
Copy link

I'm having this problem; can someone confirm whether or not this bug was supposed to have been fixed in 2.5.0 or if the fix is yet to be released?

#214

brenetic added a commit to ministryofjustice/fb-metadata-api that referenced this issue Dec 2, 2020
Local referencing seems to be an issue with the json-schema gem. There
are a number of issues open:

voxpupuli/json-schema#190
voxpupuli/json-schema#370
voxpupuli/json-schema#411

To get around this we are preloading all the schemas into the
JSON::Validator at startup using the _name property as the identifier
for each schema. This should allow the JSON::Validator the ability to
more easily find the necessary referenced schemas.
brenetic added a commit to ministryofjustice/fb-metadata-api that referenced this issue Dec 2, 2020
Local referencing seems to be an issue with the json-schema gem. There
are a number of issues open:

voxpupuli/json-schema#190
voxpupuli/json-schema#370
voxpupuli/json-schema#411

To get around this we are preloading all the schemas into the
JSON::Validator at startup using the _name property as the identifier
for each schema. This should allow the JSON::Validator the ability to
more easily find the necessary referenced schemas.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants