Skip to content

Commit

Permalink
Fix FlatFileSchemaResolver messing up $recursiveRef
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Jan 17, 2025
1 parent d184391 commit bf3269a
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": "#"
}
}

0 comments on commit bf3269a

Please sign in to comment.