json-schema icon indicating copy to clipboard operation
json-schema copied to clipboard

Usage of $ref causes error under certain circumstances

Open RST-J opened this issue 10 years ago • 8 comments

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 avatar Nov 27 '14 22:11 RST-J

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. =)

pd avatar Nov 28 '14 16:11 pd

@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 avatar Nov 30 '14 21:11 RST-J

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)

iainbeeston avatar Dec 01 '14 07:12 iainbeeston

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.

RST-J avatar Dec 01 '14 08:12 RST-J

@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

iainbeeston avatar Dec 01 '14 08:12 iainbeeston

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

RST-J avatar Dec 01 '14 09:12 RST-J

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

iainbeeston avatar Dec 01 '14 22:12 iainbeeston

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?

https://github.com/ruby-json-schema/json-schema/issues/214

BGarretson avatar Dec 22 '14 23:12 BGarretson