Skip to content

Commit befe473

Browse files
authored
Merge pull request #1226 from michaeltlombardi/gh-538/main/get_keyword_as_subschema
(GH-538) Enable retrieving keywords as `Schema` objects
2 parents 1219bac + 48a672e commit befe473

File tree

12 files changed

+707
-615
lines changed

12 files changed

+707
-615
lines changed

lib/dsc-lib-jsonschema/src/schema_utility_extensions.rs

Lines changed: 188 additions & 89 deletions
Large diffs are not rendered by default.

lib/dsc-lib-jsonschema/src/tests/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@
1313
//! of the modules from the rest of the source tree.
1414
1515
#[cfg(test)] mod schema_utility_extensions;
16-
#[cfg(test)] mod transforms;
1716
#[cfg(test)] mod vscode;

lib/dsc-lib-jsonschema/src/tests/schema_utility_extensions/mod.rs renamed to lib/dsc-lib-jsonschema/src/tests/schema_utility_extensions.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ static OBJECT_VALUE: LazyLock<Map<String, Value>> = LazyLock::new(|| json!({
2626
}).as_object().unwrap().clone());
2727
static NULL_VALUE: () = ();
2828
static STRING_VALUE: &str = "value";
29+
static SUBSCHEMA_VALUE: LazyLock<Schema> = LazyLock::new(|| json_schema!({
30+
"$id": "https://schema.contoso.com/test/get_keyword_as/subschema.json"
31+
}));
2932
static TEST_SCHEMA: LazyLock<Schema> = LazyLock::new(|| json_schema!({
3033
"$id": "https://schema.contoso.com/test/get_keyword_as.json",
3134
"array": *ARRAY_VALUE,
@@ -35,6 +38,7 @@ static TEST_SCHEMA: LazyLock<Schema> = LazyLock::new(|| json_schema!({
3538
"object": *OBJECT_VALUE,
3639
"null": null,
3740
"string": *STRING_VALUE,
41+
"subschema": *SUBSCHEMA_VALUE,
3842
}));
3943

4044
/// Defines test cases for a given `get_keyword_as` function (non-mutable).
@@ -135,12 +139,14 @@ test_cases_for_get_keyword_as!(
135139
get_keyword_as_number: "array", "integer", Some(&(INTEGER_VALUE.into())),
136140
get_keyword_as_str: "array", "string", Some(STRING_VALUE),
137141
get_keyword_as_string: "array", "string", Some(STRING_VALUE.to_string()),
142+
get_keyword_as_subschema: "array", "subschema", Some(&*SUBSCHEMA_VALUE),
138143
);
139144

140145

141146
test_cases_for_get_keyword_as_mut!(
142147
get_keyword_as_array_mut: "boolean", "array", Some(&mut (*ARRAY_VALUE).clone()),
143148
get_keyword_as_object_mut: "array", "object", Some(&mut (*OBJECT_VALUE).clone()),
149+
get_keyword_as_subschema_mut: "array", "subschema", Some(&mut (*SUBSCHEMA_VALUE).clone()),
144150
);
145151

146152
#[cfg(test)] mod get_id {
@@ -335,7 +341,6 @@ test_cases_for_get_keyword_as_mut!(
335341

336342
use pretty_assertions::assert_eq;
337343
use schemars::json_schema;
338-
use serde_json::json;
339344

340345
use crate::schema_utility_extensions::SchemaUtilityExtensions;
341346

@@ -382,13 +387,13 @@ test_cases_for_get_keyword_as_mut!(
382387
}
383388
}
384389
});
385-
let ref expected = json!({
390+
let ref expected = json_schema!({
386391
"$id": "https://contoso.com/schemas/foo.json",
387392
"title": "Foo"
388393
});
389394
assert_eq!(
390395
schema.get_defs_subschema_from_id("https://contoso.com/schemas/foo.json"),
391-
expected.as_object()
396+
Some(expected)
392397
);
393398
}
394399
}
@@ -397,7 +402,6 @@ test_cases_for_get_keyword_as_mut!(
397402

398403
use pretty_assertions::assert_eq;
399404
use schemars::json_schema;
400-
use serde_json::json;
401405

402406
use crate::schema_utility_extensions::SchemaUtilityExtensions;
403407

@@ -444,20 +448,19 @@ test_cases_for_get_keyword_as_mut!(
444448
}
445449
}
446450
});
447-
let ref mut expected = json!({
451+
let ref mut expected = json_schema!({
448452
"$id": "https://contoso.com/schemas/foo.json",
449453
"title": "Foo"
450454
});
451455
assert_eq!(
452456
schema.get_defs_subschema_from_id_mut("https://contoso.com/schemas/foo.json"),
453-
expected.as_object_mut()
457+
Some(expected)
454458
);
455459
}
456460
}
457461
#[cfg(test)] mod get_defs_subschema_from_reference {
458462
use pretty_assertions::assert_eq;
459463
use schemars::json_schema;
460-
use serde_json::json;
461464

462465
use crate::schema_utility_extensions::SchemaUtilityExtensions;
463466

@@ -518,13 +521,13 @@ test_cases_for_get_keyword_as_mut!(
518521
}
519522
}
520523
});
521-
let expected = json!({
524+
let ref expected = json_schema!({
522525
"$id": "https://contoso.com/schemas/foo.json",
523526
"title": "Foo"
524527
});
525528
assert_eq!(
526529
schema.get_defs_subschema_from_reference("#/$defs/foo").unwrap(),
527-
expected.as_object().unwrap()
530+
expected
528531
);
529532
}
530533
#[test] fn with_absolute_id_uri_reference() {
@@ -539,13 +542,13 @@ test_cases_for_get_keyword_as_mut!(
539542
}
540543
}
541544
});
542-
let expected = json!({
545+
let ref expected = json_schema!({
543546
"$id": "https://contoso.com/schemas/foo.json",
544547
"title": "Foo"
545548
});
546549
assert_eq!(
547550
schema.get_defs_subschema_from_reference("/schemas/foo.json").unwrap(),
548-
expected.as_object().unwrap()
551+
expected
549552
);
550553
}
551554
#[test] fn with_relative_id_uri_reference() {
@@ -560,20 +563,19 @@ test_cases_for_get_keyword_as_mut!(
560563
}
561564
}
562565
});
563-
let expected = json!({
566+
let ref expected = json_schema!({
564567
"$id": "https://contoso.com/schemas/foo.json",
565568
"title": "Foo"
566569
});
567570
assert_eq!(
568571
schema.get_defs_subschema_from_reference("https://contoso.com/schemas/foo.json").unwrap(),
569-
expected.as_object().unwrap()
572+
expected
570573
);
571574
}
572575
}
573576

574577
#[cfg(test)] mod get_defs_subschema_from_reference_mut {
575578
use schemars::json_schema;
576-
use serde_json::json;
577579

578580
use crate::schema_utility_extensions::SchemaUtilityExtensions;
579581

@@ -634,14 +636,14 @@ test_cases_for_get_keyword_as_mut!(
634636
}
635637
}
636638
});
637-
let ref mut expected = json!({
639+
let ref mut expected = json_schema!({
638640
"$id": "https://contoso.com/schemas/foo.json",
639641
"title": "Foo"
640642
});
641643

642644
assert_eq!(
643645
schema.get_defs_subschema_from_reference_mut("#/$defs/foo"),
644-
expected.as_object_mut()
646+
Some(expected)
645647
);
646648
}
647649
#[test] fn with_absolute_id_uri_reference() {
@@ -656,13 +658,13 @@ test_cases_for_get_keyword_as_mut!(
656658
}
657659
}
658660
});
659-
let ref mut expected = json!({
661+
let ref mut expected = json_schema!({
660662
"$id": "https://contoso.com/schemas/foo.json",
661663
"title": "Foo"
662664
});
663665
assert_eq!(
664666
schema.get_defs_subschema_from_reference_mut("/schemas/foo.json").unwrap(),
665-
expected.as_object_mut().unwrap()
667+
expected
666668
);
667669
}
668670
#[test] fn with_relative_id_uri_reference() {
@@ -677,13 +679,13 @@ test_cases_for_get_keyword_as_mut!(
677679
}
678680
}
679681
});
680-
let ref mut expected = json!({
682+
let ref mut expected = json_schema!({
681683
"$id": "https://contoso.com/schemas/foo.json",
682684
"title": "Foo"
683685
});
684686
assert_eq!(
685687
schema.get_defs_subschema_from_reference_mut("https://contoso.com/schemas/foo.json").unwrap(),
686-
expected.as_object_mut().unwrap()
688+
expected
687689
);
688690
}
689691
}
@@ -883,7 +885,6 @@ test_cases_for_get_keyword_as_mut!(
883885

884886
use pretty_assertions::assert_eq;
885887
use schemars::json_schema;
886-
use serde_json::json;
887888

888889
use crate::schema_utility_extensions::SchemaUtilityExtensions;
889890

@@ -916,7 +917,7 @@ test_cases_for_get_keyword_as_mut!(
916917
assert_eq!(schema.get_property_subschema("foo"), None)
917918
}
918919
#[test] fn when_given_property_is_object() {
919-
let ref property = json!({
920+
let ref property = json_schema!({
920921
"title": "Foo property"
921922
});
922923
let ref schema = json_schema!({
@@ -926,7 +927,7 @@ test_cases_for_get_keyword_as_mut!(
926927
});
927928
assert_eq!(
928929
schema.get_property_subschema("foo").unwrap(),
929-
property.as_object().unwrap()
930+
property
930931
)
931932
}
932933
}
@@ -936,7 +937,6 @@ test_cases_for_get_keyword_as_mut!(
936937

937938
use pretty_assertions::assert_eq;
938939
use schemars::json_schema;
939-
use serde_json::json;
940940

941941
use crate::schema_utility_extensions::SchemaUtilityExtensions;
942942

@@ -969,7 +969,7 @@ test_cases_for_get_keyword_as_mut!(
969969
assert_eq!(schema.get_property_subschema_mut("foo"), None)
970970
}
971971
#[test] fn when_given_property_is_object() {
972-
let ref mut property = json!({
972+
let ref mut property = json_schema!({
973973
"title": "Foo property"
974974
});
975975
let ref mut schema = json_schema!({
@@ -979,7 +979,7 @@ test_cases_for_get_keyword_as_mut!(
979979
});
980980
assert_eq!(
981981
schema.get_property_subschema_mut("foo").unwrap(),
982-
property.as_object_mut().unwrap()
982+
property
983983
)
984984
}
985985
}

lib/dsc-lib-jsonschema/src/tests/transforms/mod.rs

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)