json_schema 0.20.3 → 0.20.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45216e42386e80c6e481d45fafb3bcecad1c55cde5137efb455bb45261e87424
4
- data.tar.gz: 8764080e6a1c38b519110a8001b53ebda5864e7b16cdf418b272459542f60e1f
3
+ metadata.gz: 0b59e2abb8260fc05004ddde201c9107a0be38149e0dbf9aff1f3d774e5d5ae8
4
+ data.tar.gz: b40758dada1ba42ac46f7aa45c9370eae4d9ef5178126813fff6ba6ad8ab0ece
5
5
  SHA512:
6
- metadata.gz: 0f43b198fc8050550fb78d3a96ad14dff765fdcce80b5ae73ce080e6fb13600697658cf3b9d2b73e1ed9adc0d40cedfb890047696778dc5e3770dead5889e666
7
- data.tar.gz: 6f30c6b07f81396b3f7366fd30186b83ddbf1ef474e375838c77df5a635b96bc2ac4dc4bf2036789a235f19efa3bc22ac76c246d31db9a697b8235790f045c4b
6
+ metadata.gz: e06bc21c170498b1bcb09b1142a1971b64c8c9e376b395e72017d0aab538eacf54c1061e1a09be7e633734f75706fafe399b9272d9ac633fc7976b4d8084decc
7
+ data.tar.gz: 98cb1d66418ebcdfe94367c01e946c210f7f79ffa89ee3daa609876398753cb7239c9e8f189087f01f81d33b55f4f038764d21d8da64049c6cc5dfd0034606aa
@@ -84,9 +84,25 @@ module JsonSchema
84
84
  end
85
85
  end
86
86
 
87
- def dereference(ref_schema, ref_stack)
87
+ def dereference(ref_schema, ref_stack, parent_ref: nil)
88
88
  ref = ref_schema.reference
89
89
 
90
+ # Some schemas don't have a reference, but do
91
+ # have children. If that's the case, we need to
92
+ # dereference the subschemas.
93
+ if !ref
94
+ schema_children(ref_schema) do |subschema|
95
+ next unless subschema.reference
96
+
97
+ if !subschema.reference.uri && parent_ref
98
+ subschema.reference = JsonReference::Reference.new("#{parent_ref.uri}#{subschema.reference.pointer}")
99
+ end
100
+
101
+ dereference(subschema, ref_stack)
102
+ end
103
+ return true
104
+ end
105
+
90
106
  # detects a reference cycle
91
107
  if ref_stack.include?(ref)
92
108
  message = %{Reference loop detected: #{ref_stack.sort.join(", ")}.}
@@ -110,22 +126,33 @@ module JsonSchema
110
126
  # references.
111
127
  if ref.uri
112
128
  schema_children(new_schema) do |subschema|
113
- next if subschema.expanded?
114
- next unless subschema.reference
115
-
116
129
  # Don't bother if the subschema points to the same
117
130
  # schema as the reference schema.
118
131
  next if ref_schema == subschema
119
132
 
120
- if !subschema.reference.uri
121
- # the subschema's ref is local to the file that the
122
- # subschema is in; however since there's no URI
123
- # the 'resolve_pointer' method would try to look it up
124
- # within @schema. So: manually reconstruct the reference to
125
- # use the URI of the parent ref.
126
- subschema.reference = JsonReference::Reference.new("#{ref.uri}#{subschema.reference.pointer}")
133
+ if subschema.reference
134
+ # If the subschema has a reference, then
135
+ # we don't need to recurse if the schema is
136
+ # already expanded.
137
+ next if subschema.expanded?
138
+
139
+ if !subschema.reference.uri
140
+ # the subschema's ref is local to the file that the
141
+ # subschema is in; however since there's no URI
142
+ # the 'resolve_pointer' method would try to look it up
143
+ # within @schema. So: manually reconstruct the reference to
144
+ # use the URI of the parent ref.
145
+ subschema.reference = JsonReference::Reference.new("#{ref.uri}#{subschema.reference.pointer}")
146
+ end
127
147
  end
128
- dereference(subschema, ref_stack)
148
+
149
+ # If we're recursing into a schema via a global reference, then if
150
+ # the current subschema doesn't have a reference, we have no way of
151
+ # figuring out what schema we're in. The resolve_pointer method will
152
+ # default to looking it up in the initial schema. Instead, we're
153
+ # passing the parent ref here, so we can grab the URI
154
+ # later if needed.
155
+ dereference(subschema, ref_stack, parent_ref: ref)
129
156
  end
130
157
  end
131
158
 
@@ -320,6 +320,104 @@ describe JsonSchema::ReferenceExpander do
320
320
  assert_equal 3, schema1.properties["foo"].properties["omg"].max_length
321
321
  end
322
322
 
323
+ it "it handles oneOf with nested references to an external schema" do
324
+ sample1 = {
325
+ "$schema" => "http://json-schema.org/draft-04/hyper-schema",
326
+ "type" => "object",
327
+ "properties" => {
328
+ "foo" => {
329
+ "$ref" => "http://json-schema.org/b.json#"
330
+ }
331
+ }
332
+ }
333
+ schema1 = JsonSchema::Parser.new.parse!(sample1)
334
+ schema1.uri = "http://json-schema.org/a.json"
335
+
336
+ sample2 = {
337
+ "$schema" => "http://json-schema.org/draft-04/hyper-schema",
338
+ "type" => "object",
339
+ "properties" => {
340
+ "bar" => {
341
+ "oneOf" => [
342
+ {"type" => "null"},
343
+ {"$ref" => "http://json-schema.org/c.json#"}
344
+ ]
345
+ }
346
+ },
347
+ }
348
+ schema2 = JsonSchema::Parser.new.parse!(sample2)
349
+ schema2.uri = "http://json-schema.org/b.json"
350
+
351
+ sample3 = {
352
+ "$schema" => "http://json-schema.org/draft-04/hyper-schema",
353
+ "type" => "object",
354
+ "properties" => {
355
+ "baz" => {
356
+ "type" => "string",
357
+ "maxLength" => 3
358
+ }
359
+ }
360
+ }
361
+ schema3 = JsonSchema::Parser.new.parse!(sample3)
362
+ schema3.uri = "http://json-schema.org/c.json"
363
+
364
+ # Initialize a store and add our schema to it.
365
+ store = JsonSchema::DocumentStore.new
366
+ store.add_schema(schema1)
367
+ store.add_schema(schema2)
368
+ store.add_schema(schema3)
369
+
370
+ expander = JsonSchema::ReferenceExpander.new
371
+ expander.expand(schema1, store: store)
372
+
373
+ assert_equal 3, schema1.properties["foo"].properties["bar"].one_of[1].properties["baz"].max_length
374
+ end
375
+
376
+ it "it handles oneOf with nested references to a local schema" do
377
+ sample1 = {
378
+ "$schema" => "http://json-schema.org/draft-04/hyper-schema",
379
+ "type" => "object",
380
+ "properties" => {
381
+ "foo" => {
382
+ "$ref" => "http://json-schema.org/b.json#"
383
+ }
384
+ }
385
+ }
386
+ schema1 = JsonSchema::Parser.new.parse!(sample1)
387
+ schema1.uri = "http://json-schema.org/a.json"
388
+
389
+ sample2 = {
390
+ "$schema" => "http://json-schema.org/draft-04/hyper-schema",
391
+ "type" => "object",
392
+ "definitions" => {
393
+ "baz" => {
394
+ "type" => "string",
395
+ "maxLength" => 3
396
+ }
397
+ },
398
+ "properties" => {
399
+ "bar" => {
400
+ "oneOf" => [
401
+ {"type" => "null"},
402
+ {"$ref" => "#/definitions/baz"}
403
+ ]
404
+ }
405
+ },
406
+ }
407
+ schema2 = JsonSchema::Parser.new.parse!(sample2)
408
+ schema2.uri = "http://json-schema.org/b.json"
409
+
410
+ # Initialize a store and add our schema to it.
411
+ store = JsonSchema::DocumentStore.new
412
+ store.add_schema(schema1)
413
+ store.add_schema(schema2)
414
+
415
+ expander = JsonSchema::ReferenceExpander.new
416
+ expander.expand(schema1, store: store)
417
+
418
+ assert_equal 3, schema1.properties["foo"].properties["bar"].one_of[1].max_length
419
+ end
420
+
323
421
  it "expands a schema with a reference to an external schema with a nested local property reference" do
324
422
  sample1 = {
325
423
  "$schema" => "http://json-schema.org/draft-04/hyper-schema",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.3
4
+ version: 0.20.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandur
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-23 00:00:00.000000000 Z
11
+ date: 2019-03-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -70,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  version: '0'
71
71
  requirements: []
72
72
  rubyforge_project:
73
- rubygems_version: 2.7.7
73
+ rubygems_version: 2.7.3
74
74
  signing_key:
75
75
  specification_version: 4
76
76
  summary: A JSON Schema V4 and Hyperschema V4 parser and validator.