1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
|
require "test_helper"
require "json_schema"
describe JsonSchema::ReferenceExpander do
it "expands references" do
expand
assert_equal [], error_messages
# this was always a fully-defined property
referenced = @schema.definitions["app"]
# this used to be a $ref
reference = @schema.properties["app"]
assert_equal "#/definitions/app", reference.reference.pointer
assert_equal referenced.description, reference.description
assert_equal referenced.id, reference.id
assert_equal referenced.type, reference.type
assert_equal referenced.uri, reference.uri
end
it "takes a document store" do
store = JsonSchema::DocumentStore.new
expand(store: store)
assert_equal store, @expander.store
end
it "will expand anyOf" do
expand
assert_equal [], error_messages
schema = @schema.properties["app"].definitions["contrived_plus"]
assert_equal 3, schema.any_of[0].min_length
assert_equal 5, schema.any_of[1].min_length
end
it "will expand allOf" do
expand
assert_equal [], error_messages
schema = @schema.properties["app"].definitions["contrived_plus"]
assert_equal 30, schema.all_of[0].max_length
assert_equal 3, schema.all_of[1].min_length
end
it "will expand dependencies" do
expand
assert_equal [], error_messages
schema = @schema.properties["app"].dependencies["ssl"].properties["name"]
assert_equal ["string"], schema.type
end
it "will expand items list schema" do
pointer("#/definitions/app/definitions/flags").merge!(
"items" => {
"$ref" => "#/definitions/app/definitions/name"
}
)
expand
assert_equal [], error_messages
schema = @schema.properties["app"].properties["flags"].items
assert_equal ["string"], schema.type
end
it "will expand items tuple schema" do
pointer("#/definitions/app/definitions/flags").merge!(
"items" => [
{ "$ref" => "#/definitions/app/definitions/name" },
{ "$ref" => "#/definitions/app/definitions/owner" }
]
)
expand
assert_equal [], error_messages
schema0 = @schema.properties["app"].properties["flags"].items[0]
schema1 = @schema.properties["app"].properties["flags"].items[0]
assert_equal ["string"], schema0.type
assert_equal ["string"], schema1.type
end
it "will expand oneOf" do
expand
assert_equal [], error_messages
schema = @schema.properties["app"].definitions["contrived_plus"]
assert_equal(/^(foo|aaa)$/, schema.one_of[0].pattern)
assert_equal(/^(foo|zzz)$/, schema.one_of[1].pattern)
end
it "will expand not" do
expand
assert_equal [], error_messages
schema = @schema.properties["app"].definitions["contrived_plus"]
assert_equal(/^$/, schema.not.pattern)
end
it "will expand additionalProperties" do
pointer("#").merge!(
"additionalProperties" => { "$ref" => "#" }
)
expand
assert_equal [], error_messages
schema = @schema.additional_properties
assert_equal ["object"], schema.type
end
it "will expand patternProperties" do
expand
assert_equal [], error_messages
# value ([1]) of the #first tuple in hash
schema = @schema.properties["app"].definitions["roles"].
pattern_properties.first[1]
assert_equal ["string"], schema.type
end
it "will expand hyperschema link schemas" do
expand
assert_equal [], error_messages
schema = @schema.properties["app"].links[0].schema.properties["name"]
assert_equal ["string"], schema.type
end
it "will expand hyperschema link targetSchemas" do
expand
assert_equal [], error_messages
schema = @schema.properties["app"].links[0].target_schema.properties["name"]
assert_equal ["string"], schema.type
end
it "will perform multiple passes to resolve all references" do
pointer("#/properties").merge!(
"app0" => { "$ref" => "#/properties/app1" },
"app1" => { "$ref" => "#/properties/app2" },
"app2" => { "$ref" => "#/definitions/app" },
)
expand
assert_equal [], error_messages
schema = @schema.properties["app0"]
assert_equal ["object"], schema.type
end
it "will resolve circular dependencies" do
pointer("#/properties").merge!(
"app" => { "$ref" => "#" }
)
expand
assert_equal [], error_messages
schema = @schema.properties["app"]
assert_equal ["object"], schema.type
end
it "builds appropriate JSON Pointers for expanded references" do
expand
assert_equal [], error_messages
# the *referenced* schema should still have a proper pointer
schema = @schema.definitions["app"].definitions["name"]
assert_equal "#/definitions/app/definitions/name", schema.pointer
# the *reference* schema should have expanded a pointer
schema = @schema.properties["app"].properties["name"]
assert_equal "#/definitions/app/properties/name", schema.pointer
end
# clones are special in that they retain their original pointer despite where
# they've been nested
it "builds appropriate JSON Pointers for circular dependencies" do
pointer("#/properties").merge!(
"app" => { "$ref" => "#" },
"app1" => { "$ref" => "#/properties/app"}
)
expand
# the first self reference has the standard pointer as expected
schema = @schema.properties["app"]
assert_equal "#/properties/app", schema.pointer
# but diving deeper results in the same pointer again
schema = schema.properties["app"]
assert_equal "#/properties/app", schema.pointer
schema = @schema.properties["app1"]
assert_equal "#/properties/app1", schema.pointer
schema = schema.properties["app1"]
assert_equal "#/properties/app1", schema.pointer
end
it "errors on a JSON Pointer that can't be resolved" do
pointer("#/properties").merge!(
"app" => { "$ref" => "#/definitions/nope" }
)
refute expand
assert_includes error_messages, %{Couldn't resolve pointer "#/definitions/nope".}
assert_includes error_types, :unresolved_pointer
assert_includes error_messages, %{Couldn't resolve references: #/definitions/nope.}
assert_includes error_types, :unresolved_references
end
it "errors on a URI that can't be resolved" do
pointer("#/properties").merge!(
"app" => { "$ref" => "/schemata/user#/definitions/name" }
)
refute expand
assert_includes error_messages,
%{Couldn't resolve references: /schemata/user#/definitions/name.}
assert_includes error_types, :unresolved_references
assert_includes error_messages, %{Couldn't resolve URI: /schemata/user.}
assert_includes error_types, :unresolved_pointer
end
it "errors on a relative URI that cannot be transformed to an absolute" do
pointer("#/properties").merge!(
"app" => { "$ref" => "relative#definitions/name" }
)
refute expand
assert_includes error_messages,
%{Couldn't resolve references: relative#definitions/name.}
assert_includes error_types, :unresolved_references
end
it "errors on a reference cycle" do
pointer("#/properties").merge!(
"app0" => { "$ref" => "#/properties/app2" },
"app1" => { "$ref" => "#/properties/app0" },
"app2" => { "$ref" => "#/properties/app1" },
)
refute expand
properties = "#/properties/app0, #/properties/app1, #/properties/app2"
assert_includes error_messages, %{Reference loop detected: #{properties}.}
assert_includes error_types, :loop_detected
assert_includes error_messages, %{Couldn't resolve references: #{properties}.}
assert_includes error_types, :unresolved_references
end
it "raises an aggregate error with expand!" do
pointer("#/properties").merge!(
"app" => { "$ref" => "#/definitions/nope" }
)
schema = JsonSchema::Parser.new.parse!(schema_sample)
expander = JsonSchema::ReferenceExpander.new
# don't bother checking the particulars of the error here because we have
# other tests for that above
assert_raises JsonSchema::AggregateError do
expander.expand!(schema)
end
end
it "expands a schema that is just a reference" do
# First initialize another schema. Give it a fully qualified URI so that we
# can reference it across schemas.
schema = JsonSchema::Parser.new.parse!(schema_sample)
schema.uri = "http://json-schema.org/test"
# Initialize a store and add our schema to it.
store = JsonSchema::DocumentStore.new
store.add_schema(schema)
# Have the parser parse _just_ a reference. It should resolve to a
# subschema in the schema that we initialized above.
schema = JsonSchema::Parser.new.parse!(
{ "$ref" => "http://json-schema.org/test#/definitions/app" }
)
expander = JsonSchema::ReferenceExpander.new
expander.expand!(schema, store: store)
assert schema.expanded?
end
it "expands a schema with a reference to an external schema in a oneOf array" do
sample1 = {
"$schema" => "http://json-schema.org/draft-04/schema#",
"id" => "http://json-schema.org/draft-04/schema#",
"definitions" => {
"schemaArray" => {
"type" => "array",
"minItems" => 1,
"items" => { "$ref" => "#" }
}
}
}
schema1 = JsonSchema::Parser.new.parse!(sample1)
sample2 = {
"$schema" => "http://json-schema.org/draft-04/hyper-schema#",
"id" => "http://json-schema.org/draft-04/hyper-schema#",
"allOf" => [
{
"$ref" => "http://json-schema.org/draft-04/schema#"
}
]
}
schema2 = JsonSchema::Parser.new.parse!(sample2)
store = JsonSchema::DocumentStore.new
expander = JsonSchema::ReferenceExpander.new
store.add_schema(schema1)
store.add_schema(schema2)
expander.expand!(schema2, store: store)
assert schema1.expanded?
assert schema2.expanded?
end
it "expands a schema with a nested reference to an external schema in a oneOf array" do
sample1 = {
"$schema" => "http://json-schema.org/draft-04/schema#",
"id" => "http://json-schema.org/draft-04/schema#",
"definitions" => {
"thingy" => {
"type" => ["string"]
},
"schemaArray" => {
"type" => "array",
"minItems" => 1,
"items" => { "$ref" => "#/definitions/thingy" }
}
},
"properties" => {
"whatsit" => {
"$ref" => "#/definitions/schemaArray"
},
}
}
schema1 = JsonSchema::Parser.new.parse!(sample1)
sample2 = {
"$schema" => "http://json-schema.org/draft-04/hyper-schema#",
"id" => "http://json-schema.org/draft-04/hyper-schema#",
"allOf" => [
{
"$ref" => "http://json-schema.org/draft-04/schema#"
}
]
}
schema2 = JsonSchema::Parser.new.parse!(sample2)
store = JsonSchema::DocumentStore.new
expander = JsonSchema::ReferenceExpander.new
store.add_schema(schema1)
store.add_schema(schema2)
expander.expand!(schema2, store: store)
assert_equal ["string"], schema2.all_of[0].properties["whatsit"].items.type
end
it "expands a schema with a reference to an external schema with a nested external property reference" do
sample1 = {
"$schema" => "http://json-schema.org/draft-04/hyper-schema",
"type" => "object",
"properties" => {
"foo" => {
"$ref" => "http://json-schema.org/b.json#/definitions/bar"
}
}
}
schema1 = JsonSchema::Parser.new.parse!(sample1)
schema1.uri = "http://json-schema.org/a.json"
sample2 = {
"$schema" => "http://json-schema.org/draft-04/hyper-schema",
"type" => "object",
"definitions" => {
"bar" => {
"type" => "object",
"properties" => {
"omg" => {
"$ref" => "http://json-schema.org/c.json#/definitions/baz"
}
}
}
}
}
schema2 = JsonSchema::Parser.new.parse!(sample2)
schema2.uri = "http://json-schema.org/b.json"
sample3 = {
"$schema" => "http://json-schema.org/draft-04/hyper-schema",
"type" => "object",
"definitions" => {
"baz" => {
"type" => "string",
"maxLength" => 3
}
}
}
schema3 = JsonSchema::Parser.new.parse!(sample3)
schema3.uri = "http://json-schema.org/c.json"
# Initialize a store and add our schema to it.
store = JsonSchema::DocumentStore.new
store.add_schema(schema1)
store.add_schema(schema2)
store.add_schema(schema3)
expander = JsonSchema::ReferenceExpander.new
expander.expand!(schema1, store: store)
assert_equal 3, schema1.properties["foo"].properties["omg"].max_length
end
it "it handles oneOf with nested references to an external schema" do
sample1 = {
"$schema" => "http://json-schema.org/draft-04/hyper-schema",
"type" => "object",
"properties" => {
"foo" => {
"$ref" => "http://json-schema.org/b.json#"
}
}
}
schema1 = JsonSchema::Parser.new.parse!(sample1)
schema1.uri = "http://json-schema.org/a.json"
sample2 = {
"$schema" => "http://json-schema.org/draft-04/hyper-schema",
"type" => "object",
"properties" => {
"bar" => {
"oneOf" => [
{"type" => "null"},
{"$ref" => "http://json-schema.org/c.json#"}
]
}
},
}
schema2 = JsonSchema::Parser.new.parse!(sample2)
schema2.uri = "http://json-schema.org/b.json"
sample3 = {
"$schema" => "http://json-schema.org/draft-04/hyper-schema",
"type" => "object",
"properties" => {
"baz" => {
"type" => "string",
"maxLength" => 3
}
}
}
schema3 = JsonSchema::Parser.new.parse!(sample3)
schema3.uri = "http://json-schema.org/c.json"
# Initialize a store and add our schema to it.
store = JsonSchema::DocumentStore.new
store.add_schema(schema1)
store.add_schema(schema2)
store.add_schema(schema3)
expander = JsonSchema::ReferenceExpander.new
expander.expand(schema1, store: store)
assert_equal 3, schema1.properties["foo"].properties["bar"].one_of[1].properties["baz"].max_length
end
it "does not infinitely recurse when external ref is local to its schema" do
sample1 = {
"id" => "http://json-schema.org/draft-04/schema#",
"$schema" => "http://json-schema.org/draft-04/schema#",
"properties" => {
"additionalItems" => {
"anyOf" => [ { "$ref" => "#" } ]
}
}
}
schema1 = JsonSchema::Parser.new.parse!(sample1)
sample2 = {
"$schema" => "http://json-schema.org/draft-04/hyper-schema#",
"id" => "http://json-schema.org/draft-04/hyper-schema#",
"allOf" => [
{ "$ref" => "http://json-schema.org/draft-04/schema#" }
]
}
schema2 = JsonSchema::Parser.new.parse!(sample2)
store = JsonSchema::DocumentStore.new
expander = JsonSchema::ReferenceExpander.new
store.add_schema(schema1)
store.add_schema(schema2)
expander.expand!(schema2, store: store)
assert schema1.expanded?
assert schema2.expanded?
end
it "it handles oneOf with nested references to a local schema" do
sample1 = {
"$schema" => "http://json-schema.org/draft-04/hyper-schema",
"type" => "object",
"properties" => {
"foo" => {
"$ref" => "http://json-schema.org/b.json#"
}
}
}
schema1 = JsonSchema::Parser.new.parse!(sample1)
schema1.uri = "http://json-schema.org/a.json"
sample2 = {
"$schema" => "http://json-schema.org/draft-04/hyper-schema",
"type" => "object",
"definitions" => {
"baz" => {
"type" => "string",
"maxLength" => 3
}
},
"properties" => {
"bar" => {
"oneOf" => [
{"type" => "null"},
{"$ref" => "#/definitions/baz"}
]
}
},
}
schema2 = JsonSchema::Parser.new.parse!(sample2)
schema2.uri = "http://json-schema.org/b.json"
# Initialize a store and add our schema to it.
store = JsonSchema::DocumentStore.new
store.add_schema(schema1)
store.add_schema(schema2)
expander = JsonSchema::ReferenceExpander.new
expander.expand(schema1, store: store)
assert_equal 3, schema1.properties["foo"].properties["bar"].one_of[1].max_length
end
it "expands a schema with a reference to an external schema with a nested local property reference" do
sample1 = {
"$schema" => "http://json-schema.org/draft-04/hyper-schema",
"type" => "object",
"properties" => {
"foo" => {
"$ref" => "http://json-schema.org/b.json#/definitions/bar"
},
"foo2" => {
"$ref" => "http://json-schema.org/b.json#/definitions/baz"
}
}
}
schema1 = JsonSchema::Parser.new.parse!(sample1)
schema1.uri = "http://json-schema.org/a.json"
sample2 = {
"$schema" => "http://json-schema.org/draft-04/hyper-schema",
"type" => "object",
"definitions" => {
"bar" => {
"type" => "object",
"properties" => {
"omg" => {
"$ref" => "#/definitions/baz"
}
}
},
"baz" => {
"type" => "string",
"maxLength" => 3
}
}
}
schema2 = JsonSchema::Parser.new.parse!(sample2)
schema2.uri = "http://json-schema.org/b.json"
# Initialize a store and add our schema to it.
store = JsonSchema::DocumentStore.new
store.add_schema(schema1)
store.add_schema(schema2)
expander = JsonSchema::ReferenceExpander.new
expander.expand!(schema1, store: store)
# These both point to the same definition, 'baz', but
# 'foo' has a level of indirection.
assert_equal 3, schema1.properties["foo2"].max_length
assert_equal 3, schema1.properties["foo"].properties["omg"].max_length
end
it "expands a reference to a link" do
pointer("#/properties").merge!(
"link" => { "$ref" => "#/links/0" }
)
assert expand
referenced = @schema.links[0]
reference = @schema.properties["link"]
assert_equal reference.href, referenced.href
end
def error_messages
@expander.errors.map { |e| e.message }
end
def error_types
@expander.errors.map { |e| e.type }
end
def pointer(path)
JsonPointer.evaluate(schema_sample, path)
end
def schema_sample
@schema_sample ||= DataScaffold.schema_sample
end
def expand(options = {})
@schema = JsonSchema::Parser.new.parse!(schema_sample)
@expander = JsonSchema::ReferenceExpander.new
@expander.expand(@schema, options)
end
end
|