Skip to content

Commit

Permalink
Make compiler JSON formatter more resilient to unknown keywords (#1194)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Sep 21, 2024
1 parent be7d39b commit 1f0cfa2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
17 changes: 7 additions & 10 deletions src/jsonschema/compile_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,14 @@ auto compiler_template_format_compare(const JSON::String &left,
{"absoluteKeywordLocation", 4},
{"relativeSchemaLocation", 5},
{"relativeInstanceLocation", 6},
{"location", 7},
{"report", 8},
{"dynamic", 9},
{"children", 10}};
{"report", 7},
{"dynamic", 8},
{"children", 9}};

// We define and control all of these keywords, so if we are missing
// some here, then we did something wrong?
assert(rank.contains(left));
assert(rank.contains(right));

return rank.at(left) < rank.at(right);
constexpr std::uint64_t DEFAULT_RANK{999};
const auto left_rank{rank.contains(left) ? rank.at(left) : DEFAULT_RANK};
const auto right_rank{rank.contains(right) ? rank.at(right) : DEFAULT_RANK};
return left_rank < right_rank;
}

} // namespace sourcemeta::jsontoolkit
4 changes: 3 additions & 1 deletion test/jsonschema/jsonschema_template_format_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ TEST(JSONSchema_compiler_template_format, example_1) {
"absoluteKeywordLocation": "#/properties",
"category": "annotation",
"report": true,
"unknown": 1,
"dynamic": true,
"type": "public",
"relativeSchemaLocation": "",
Expand Down Expand Up @@ -36,6 +37,7 @@ TEST(JSONSchema_compiler_template_format, example_1) {
"relativeSchemaLocation": "",
"relativeInstanceLocation": "",
"report": true,
"dynamic": true
"dynamic": true,
"unknown": 1
})JSON");
}

4 comments on commit 1f0cfa2

@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: 1f0cfa2 Previous: be7d39b Ratio
JSONSchema_Validate_Draft4_Meta_1_No_Callback 720.2525959256295 ns/iter 667.1735415967004 ns/iter 1.08
JSONSchema_Validate_Draft4_Required_Properties 934.1942275584938 ns/iter 785.4626709026937 ns/iter 1.19
JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match 152.4594895915016 ns/iter 142.42895641924292 ns/iter 1.07
JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match 134.2628155775465 ns/iter 104.10384579570086 ns/iter 1.29
JSONSchema_Validate_Draft4_Items_Schema 2097.7039604082793 ns/iter 1970.5488733595168 ns/iter 1.06
JSONSchema_Validate_Draft4_Nested_Object 1270.8307180228974 ns/iter 1210.4202302106576 ns/iter 1.05
JSONSchema_Validate_Draft4_Properties_Triad_Optional 1476.7680117961638 ns/iter 1319.5710442532716 ns/iter 1.12
JSONSchema_Validate_Draft4_Properties_Triad_Closed 1058.6010950961033 ns/iter 982.906971142387 ns/iter 1.08
JSONSchema_Validate_Draft4_Properties_Triad_Required 1471.9725273529396 ns/iter 1322.1507604358912 ns/iter 1.11
JSONSchema_Validate_Draft4_Non_Recursive_Ref 517.8857555334384 ns/iter 472.96031295359404 ns/iter 1.09
JSONSchema_Validate_Draft4_Pattern_Properties_True 1419.2381572618394 ns/iter 1363.0788039232045 ns/iter 1.04
JSONSchema_Validate_Draft4_Ref_To_Single_Property 118.16146903874511 ns/iter 111.56104119109007 ns/iter 1.06

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/llvm)

Benchmark suite Current: 1f0cfa2 Previous: be7d39b Ratio
JSONSchema_Validate_Draft4_Meta_1_No_Callback 17473.13042500601 ns/iter 17729.26830192477 ns/iter 0.99
JSONSchema_Validate_Draft4_Required_Properties 7151.661607241466 ns/iter 7261.3480942746755 ns/iter 0.98
JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match 1871.9930645663264 ns/iter 1890.6649045435702 ns/iter 0.99
JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match 913.8141543562224 ns/iter 939.6359202409514 ns/iter 0.97
JSONSchema_Validate_Draft4_Items_Schema 116654.19890054557 ns/iter 117619.84888216267 ns/iter 0.99
JSONSchema_Validate_Draft4_Nested_Object 56400.19112325901 ns/iter 56933.87480755148 ns/iter 0.99
JSONSchema_Validate_Draft4_Properties_Triad_Optional 8824.030131541012 ns/iter 8867.753292506675 ns/iter 1.00
JSONSchema_Validate_Draft4_Properties_Triad_Closed 8291.825763694877 ns/iter 8324.24364604875 ns/iter 1.00
JSONSchema_Validate_Draft4_Properties_Triad_Required 9005.2868175228 ns/iter 9051.317487531707 ns/iter 0.99
JSONSchema_Validate_Draft4_Non_Recursive_Ref 4378.716357634848 ns/iter 4481.3812133993815 ns/iter 0.98
JSONSchema_Validate_Draft4_Pattern_Properties_True 6435.050026566147 ns/iter 6437.8655236800505 ns/iter 1.00
JSONSchema_Validate_Draft4_Ref_To_Single_Property 923.3524514876088 ns/iter 939.7475171753259 ns/iter 0.98

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: 1f0cfa2 Previous: be7d39b Ratio
JSONSchema_Validate_Draft4_Meta_1_No_Callback 1072.1488220157507 ns/iter 1099.680419438895 ns/iter 0.97
JSONSchema_Validate_Draft4_Required_Properties 2106.4476105455738 ns/iter 2127.231024956107 ns/iter 0.99
JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match 184.91853167286337 ns/iter 201.64598630117217 ns/iter 0.92
JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match 151.01272440363317 ns/iter 154.67408025809732 ns/iter 0.98
JSONSchema_Validate_Draft4_Items_Schema 3639.095570093325 ns/iter 3644.8656233657716 ns/iter 1.00
JSONSchema_Validate_Draft4_Nested_Object 1687.571627229883 ns/iter 1688.0388400066759 ns/iter 1.00
JSONSchema_Validate_Draft4_Properties_Triad_Optional 1745.9663770913398 ns/iter 1822.1938109141336 ns/iter 0.96
JSONSchema_Validate_Draft4_Properties_Triad_Closed 1417.6420134954967 ns/iter 1489.4100042700743 ns/iter 0.95
JSONSchema_Validate_Draft4_Properties_Triad_Required 1806.6197879510853 ns/iter 1806.3300164895954 ns/iter 1.00
JSONSchema_Validate_Draft4_Non_Recursive_Ref 715.8868903826683 ns/iter 730.1348265515346 ns/iter 0.98
JSONSchema_Validate_Draft4_Pattern_Properties_True 2371.016070134145 ns/iter 2414.1310316580293 ns/iter 0.98
JSONSchema_Validate_Draft4_Ref_To_Single_Property 161.54215793264063 ns/iter 168.5694812717015 ns/iter 0.96

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: 1f0cfa2 Previous: be7d39b Ratio
JSONSchema_Validate_Draft4_Meta_1_No_Callback 2924.6640004413052 ns/iter 2965.8249536662247 ns/iter 0.99
JSONSchema_Validate_Draft4_Required_Properties 1603.5794642859407 ns/iter 1613.3189732143092 ns/iter 0.99
JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match 613.8299999999422 ns/iter 614.1255357142938 ns/iter 1.00
JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match 423.4408750001251 ns/iter 429.42656933000563 ns/iter 0.99
JSONSchema_Validate_Draft4_Items_Schema 11498.616071431146 ns/iter 11525.062500000771 ns/iter 1.00
JSONSchema_Validate_Draft4_Nested_Object 6696.429464286422 ns/iter 6598.435714286057 ns/iter 1.01
JSONSchema_Validate_Draft4_Properties_Triad_Optional 5602.031250000599 ns/iter 5740.125892857456 ns/iter 0.98
JSONSchema_Validate_Draft4_Properties_Triad_Closed 4655.377578967403 ns/iter 4820.480777215212 ns/iter 0.97
JSONSchema_Validate_Draft4_Properties_Triad_Required 5644.843749999089 ns/iter 5816.117857142851 ns/iter 0.97
JSONSchema_Validate_Draft4_Non_Recursive_Ref 1162.7771428570504 ns/iter 1171.3431249999574 ns/iter 0.99
JSONSchema_Validate_Draft4_Pattern_Properties_True 7942.603827658952 ns/iter 8116.256696428793 ns/iter 0.98
JSONSchema_Validate_Draft4_Ref_To_Single_Property 434.6361874999616 ns/iter 446.0694006064073 ns/iter 0.97

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

Please sign in to comment.