Skip to content

Commit

Permalink
Fix FlatFileSchemaResolver messing up $recursiveRef (#1451)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Jan 17, 2025
1 parent e6fca87 commit ab68410
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/jsonschema/relativize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ auto relativize(JSON &schema, const SchemaWalker &walker,
continue;
}

// In 2019-09, `$recursiveRef` can only be `#`, so there
// is nothing else we can possibly do
if (property.first == "$recursiveRef") {
continue;
}

URI reference{property.second.to_string()};
reference.relative_to(base);
reference.canonicalize();
Expand Down
25 changes: 25 additions & 0 deletions test/jsonschema/jsonschema_flat_file_resolver_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,28 @@ TEST(JSONSchema_FlatFileSchemaResolver, case_insensitive_reidentify) {
EXPECT_TRUE(resolver("https://EXAMPLE.com").has_value());
EXPECT_TRUE(resolver("https://example.com").has_value());
}

TEST(JSONSchema_FlatFileSchemaResolver, with_recursive_ref) {
sourcemeta::jsontoolkit::FlatFileSchemaResolver resolver;
const auto schema_path{std::filesystem::path{SCHEMAS_PATH} /
"2019-09-recursive-ref.json"};

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://www.sourcemeta.com/2019-09-recursive-ref.json",
"$recursiveAnchor": true,
"additionalProperties": {
"$recursiveRef": "#"
}
})JSON");

const auto &identifier{resolver.add(schema_path)};
EXPECT_EQ(identifier,
"https://www.sourcemeta.com/2019-09-recursive-ref.json");
EXPECT_TRUE(resolver("https://www.sourcemeta.com/2019-09-recursive-ref.json")
.has_value());
EXPECT_EQ(
resolver("https://www.sourcemeta.com/2019-09-recursive-ref.json").value(),
expected);
}
26 changes: 26 additions & 0 deletions test/jsonschema/jsonschema_relativize_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,29 @@ TEST(JSONSchema_relativize, 2020_12_2) {

EXPECT_EQ(schema, expected);
}

TEST(JSONSchema_relativize, recursive_ref) {
auto schema = sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://www.sourcemeta.com",
"$recursiveAnchor": true,
"additionalProperties": {
"$recursiveRef": "#"
}
})JSON");

sourcemeta::jsontoolkit::relativize(
schema, sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver);

const auto expected = sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://www.sourcemeta.com",
"$recursiveAnchor": true,
"additionalProperties": {
"$recursiveRef": "#"
}
})JSON");

EXPECT_EQ(schema, expected);
}
8 changes: 8 additions & 0 deletions test/jsonschema/schemas/2019-09-recursive-ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://www.sourcemeta.com/2019-09-recursive-ref.json",
"$recursiveAnchor": true,
"additionalProperties": {
"$recursiveRef": "#"
}
}

4 comments on commit ab68410

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (linux/llvm)

Benchmark suite Current: ab68410 Previous: e6fca87 Ratio
JSON_Array_Of_Objects_Unique 405.4087768965295 ns/iter 409.838349262604 ns/iter 0.99
JSON_Parse_1 30050.479886661986 ns/iter 30251.810662080312 ns/iter 0.99
JSON_Fast_Hash_Helm_Chart_Lock 58.75380905066242 ns/iter 58.77655625276818 ns/iter 1.00
JSON_Equality_Helm_Chart_Lock 147.47621722270134 ns/iter 146.46662526998583 ns/iter 1.01
Regex_Lower_S_Or_Upper_S_Asterisk 2.1943911367335462 ns/iter 2.1921244836652 ns/iter 1.00
Regex_Caret_Lower_S_Or_Upper_S_Asterisk_Dollar 2.1928236905687597 ns/iter 2.203594754079846 ns/iter 1.00
Regex_Period_Asterisk 2.200648054033529 ns/iter 2.2047149694666444 ns/iter 1.00
Regex_Group_Period_Asterisk_Group 2.208171738595412 ns/iter 2.1984505890426322 ns/iter 1.00
Regex_Period_Plus 2.4874078533843997 ns/iter 2.6337649151701643 ns/iter 0.94
Regex_Period 2.491542922876592 ns/iter 2.487388017347584 ns/iter 1.00
Regex_Caret_Period_Plus_Dollar 2.8348517540431186 ns/iter 2.79539099133569 ns/iter 1.01
Regex_Caret_Group_Period_Plus_Group_Dollar 2.489764173306145 ns/iter 2.4865848672745137 ns/iter 1.00
Regex_Caret_Period_Asterisk_Dollar 2.7964434953525985 ns/iter 2.4867245811005314 ns/iter 1.12
Regex_Caret_Group_Period_Asterisk_Group_Dollar 2.4868624397991543 ns/iter 2.2066267324559705 ns/iter 1.13
Regex_Caret_X_Hyphen 12.61211599502065 ns/iter 12.607855555087779 ns/iter 1.00
Regex_Period_Md_Dollar 73.98092552578082 ns/iter 73.96110324329327 ns/iter 1.00
Regex_Caret_Slash_Period_Asterisk 5.645576535530873 ns/iter 5.602721321897821 ns/iter 1.01
Regex_Caret_Period_Range_Dollar 2.93188900430747 ns/iter 3.109421073130435 ns/iter 0.94
Regex_Nested_Backtrack 527.9802633776685 ns/iter 492.77271603869923 ns/iter 1.07
Pointer_Object_Traverse 44.7509890093832 ns/iter 45.781133513913744 ns/iter 0.98
Pointer_Object_Try_Traverse 52.61736747873644 ns/iter 52.598010671334826 ns/iter 1.00
Pointer_Push_Back_Pointer_To_Weak_Pointer 353.5945207821983 ns/iter 351.9847645060383 ns/iter 1.00

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (windows/msvc)

Benchmark suite Current: ab68410 Previous: e6fca87 Ratio
JSON_Array_Of_Objects_Unique 432.63173500940434 ns/iter 419.039040921279 ns/iter 1.03
JSON_Parse_1 79515.49107141988 ns/iter 82802.70089285732 ns/iter 0.96
JSON_Fast_Hash_Helm_Chart_Lock 57.18004000000292 ns/iter 56.54554000000189 ns/iter 1.01
JSON_Equality_Helm_Chart_Lock 219.6698437499833 ns/iter 217.84103125000343 ns/iter 1.01
Regex_Lower_S_Or_Upper_S_Asterisk 8.082893267488405 ns/iter 8.084268713908735 ns/iter 1.00
Regex_Caret_Lower_S_Or_Upper_S_Asterisk_Dollar 8.539217372592159 ns/iter 7.866164062500403 ns/iter 1.09
Regex_Period_Asterisk 7.874678571428666 ns/iter 7.765224072475957 ns/iter 1.01
Regex_Group_Period_Asterisk_Group 7.7803606795516975 ns/iter 7.772327643872847 ns/iter 1.00
Regex_Period_Plus 8.084341035338017 ns/iter 8.18612388392838 ns/iter 0.99
Regex_Period 7.889927643348755 ns/iter 8.114655133928395 ns/iter 0.97
Regex_Caret_Period_Plus_Dollar 7.777138358137729 ns/iter 8.046731026785737 ns/iter 0.97
Regex_Caret_Group_Period_Plus_Group_Dollar 8.34171558775942 ns/iter 8.333210937500215 ns/iter 1.00
Regex_Caret_Period_Asterisk_Dollar 7.752839285715448 ns/iter 7.805968749999995 ns/iter 0.99
Regex_Caret_Group_Period_Asterisk_Group_Dollar 8.252888802442433 ns/iter 7.883953124999518 ns/iter 1.05
Regex_Caret_X_Hyphen 11.52970892857143 ns/iter 11.701323214284441 ns/iter 0.99
Regex_Period_Md_Dollar 144.04887080138974 ns/iter 142.23713873940164 ns/iter 1.01
Regex_Caret_Slash_Period_Asterisk 11.871064285713828 ns/iter 11.584375000000234 ns/iter 1.02
Regex_Caret_Period_Range_Dollar 8.72346022891264 ns/iter 8.750487500000403 ns/iter 1.00
Regex_Nested_Backtrack 588.913035714305 ns/iter 593.2228571428562 ns/iter 0.99
Pointer_Object_Traverse 55.86197321428748 ns/iter 55.98514285714324 ns/iter 1.00
Pointer_Object_Try_Traverse 75.86683928572313 ns/iter 76.21847098213357 ns/iter 1.00
Pointer_Push_Back_Pointer_To_Weak_Pointer 204.95559375000025 ns/iter 173.78108083353501 ns/iter 1.18

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (linux/gcc)

Benchmark suite Current: ab68410 Previous: e6fca87 Ratio
Pointer_Object_Traverse 43.787553208088354 ns/iter 43.74717301802706 ns/iter 1.00
Pointer_Object_Try_Traverse 22.41124544337635 ns/iter 22.44070633166494 ns/iter 1.00
Pointer_Push_Back_Pointer_To_Weak_Pointer 273.8907181186028 ns/iter 214.40295712544386 ns/iter 1.28
Regex_Lower_S_Or_Upper_S_Asterisk 3.126580226329182 ns/iter 2.4897889438979663 ns/iter 1.26
Regex_Caret_Lower_S_Or_Upper_S_Asterisk_Dollar 3.1227632974684716 ns/iter 2.4866515285104773 ns/iter 1.26
Regex_Period_Asterisk 3.139236759969375 ns/iter 2.4851797466016174 ns/iter 1.26
Regex_Group_Period_Asterisk_Group 3.1213498640962416 ns/iter 2.4858996461720753 ns/iter 1.26
Regex_Period_Plus 3.204227054445672 ns/iter 2.796108939528642 ns/iter 1.15
Regex_Period 3.1224567659490727 ns/iter 2.8057062637665093 ns/iter 1.11
Regex_Caret_Period_Plus_Dollar 3.132681140440094 ns/iter 2.551185585410132 ns/iter 1.23
Regex_Caret_Group_Period_Plus_Group_Dollar 3.1221949265765643 ns/iter 2.488255504378302 ns/iter 1.25
Regex_Caret_Period_Asterisk_Dollar 3.128634079979289 ns/iter 2.816829537727164 ns/iter 1.11
Regex_Caret_Group_Period_Asterisk_Group_Dollar 3.1238544209663948 ns/iter 2.796529875212853 ns/iter 1.12
Regex_Caret_X_Hyphen 12.467811302022618 ns/iter 12.430006779901062 ns/iter 1.00
Regex_Period_Md_Dollar 89.43583007612096 ns/iter 89.09971143253057 ns/iter 1.00
Regex_Caret_Slash_Period_Asterisk 6.233218240167974 ns/iter 7.153418768446902 ns/iter 0.87
Regex_Caret_Period_Range_Dollar 3.164863949913841 ns/iter 4.039049303935131 ns/iter 0.78
Regex_Nested_Backtrack 962.2664948856608 ns/iter 833.5671672221104 ns/iter 1.15
JSON_Array_Of_Objects_Unique 370.76628366877634 ns/iter 379.3086258695145 ns/iter 0.98
JSON_Parse_1 33518.916578973156 ns/iter 34849.64040514822 ns/iter 0.96
JSON_Fast_Hash_Helm_Chart_Lock 62.691208493806386 ns/iter 63.88203925776167 ns/iter 0.98
JSON_Equality_Helm_Chart_Lock 142.28515035221162 ns/iter 142.53894650079172 ns/iter 1.00

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (macos/llvm)

Benchmark suite Current: ab68410 Previous: e6fca87 Ratio
JSON_Array_Of_Objects_Unique 350.47395898792377 ns/iter 354.94943650442565 ns/iter 0.99
JSON_Parse_1 22607.683524393928 ns/iter 22584.097259697224 ns/iter 1.00
JSON_Fast_Hash_Helm_Chart_Lock 53.16230146127368 ns/iter 49.75259797855129 ns/iter 1.07
JSON_Equality_Helm_Chart_Lock 141.86687899286926 ns/iter 146.89975887514794 ns/iter 0.97
Regex_Lower_S_Or_Upper_S_Asterisk 1.6688067166406624 ns/iter 1.7049316276674866 ns/iter 0.98
Regex_Caret_Lower_S_Or_Upper_S_Asterisk_Dollar 1.6314653185879897 ns/iter 1.6524565066681374 ns/iter 0.99
Regex_Period_Asterisk 1.628110511983515 ns/iter 1.6579705167102348 ns/iter 0.98
Regex_Group_Period_Asterisk_Group 1.6168747198733036 ns/iter 1.6889200897531687 ns/iter 0.96
Regex_Period_Plus 1.9231676268681612 ns/iter 1.9449536724458873 ns/iter 0.99
Regex_Period 1.913064776984137 ns/iter 2.065658082207715 ns/iter 0.93
Regex_Caret_Period_Plus_Dollar 1.9242512358145853 ns/iter 2.1154090106169594 ns/iter 0.91
Regex_Caret_Group_Period_Plus_Group_Dollar 1.9232875642558818 ns/iter 2.0281539175192185 ns/iter 0.95
Regex_Caret_Period_Asterisk_Dollar 1.6546072896662736 ns/iter 1.7153824469517167 ns/iter 0.96
Regex_Caret_Group_Period_Asterisk_Group_Dollar 1.716380651075979 ns/iter 1.7010444097471187 ns/iter 1.01
Regex_Caret_X_Hyphen 7.000494139069195 ns/iter 6.968329826764318 ns/iter 1.00
Regex_Period_Md_Dollar 74.34986990987146 ns/iter 75.45184394733849 ns/iter 0.99
Regex_Caret_Slash_Period_Asterisk 7.172824442786625 ns/iter 6.247339810727807 ns/iter 1.15
Regex_Caret_Period_Range_Dollar 2.1971364469509034 ns/iter 2.246039300808138 ns/iter 0.98
Regex_Nested_Backtrack 766.7800550260856 ns/iter 770.6727172648007 ns/iter 0.99
Pointer_Object_Traverse 16.02025539220174 ns/iter 15.833272729850124 ns/iter 1.01
Pointer_Object_Try_Traverse 34.092414464472924 ns/iter 34.69839771951231 ns/iter 0.98
Pointer_Push_Back_Pointer_To_Weak_Pointer 188.7245385365732 ns/iter 189.38296961132184 ns/iter 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.