Skip to content

Commit

Permalink
Consider .json as a fallback on FlatFileSchemaResolver
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 13, 2025
1 parent 155f7df commit 408d290
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/jsonschema/resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,14 @@ auto FlatFileSchemaResolver::reidentify(const std::string &schema,
auto FlatFileSchemaResolver::operator()(std::string_view identifier) const
-> std::optional<JSON> {
const std::string string_identifier{to_lowercase(identifier)};
const auto result{this->schemas.find(string_identifier)};
auto result{this->schemas.find(string_identifier)};

// Try with a `.json` extension (if not already) as a fallback, given
// how common that case is when using a file-based resolver
if (result == this->schemas.cend() && !string_identifier.ends_with(".json")) {
result = this->schemas.find(string_identifier + ".json");
}

if (result != this->schemas.cend()) {
auto schema{sourcemeta::jsontoolkit::from_file(result->second.path)};
assert(sourcemeta::jsontoolkit::is_schema(schema));
Expand Down
24 changes: 24 additions & 0 deletions test/jsonschema/jsonschema_flat_file_resolver_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ TEST(JSONSchema_FlatFileSchemaResolver, single_schema_with_default_dialect) {
expected);
}

TEST(JSONSchema_FlatFileSchemaResolver, no_json_extension_on_insert) {
sourcemeta::jsontoolkit::FlatFileSchemaResolver resolver;
const auto schema_path{std::filesystem::path{SCHEMAS_PATH} /
"2020-12-without-extension.json"};
const auto &identifier{resolver.add(schema_path)};
EXPECT_EQ(identifier, "https://www.sourcemeta.com/2020-12-without-extension");
EXPECT_TRUE(resolver("https://www.sourcemeta.com/2020-12-without-extension")
.has_value());
EXPECT_EQ(
resolver("https://www.sourcemeta.com/2020-12-without-extension").value(),
sourcemeta::jsontoolkit::from_file(schema_path));
}

TEST(JSONSchema_FlatFileSchemaResolver, no_json_extension_on_lookup) {
sourcemeta::jsontoolkit::FlatFileSchemaResolver resolver;
const auto schema_path{std::filesystem::path{SCHEMAS_PATH} /
"2020-12-id.json"};
const auto &identifier{resolver.add(schema_path)};
EXPECT_EQ(identifier, "https://www.sourcemeta.com/2020-12-id.json");
EXPECT_TRUE(resolver("https://www.sourcemeta.com/2020-12-id").has_value());
EXPECT_EQ(resolver("https://www.sourcemeta.com/2020-12-id").value(),
sourcemeta::jsontoolkit::from_file(schema_path));
}

TEST(JSONSchema_FlatFileSchemaResolver, single_schema_anonymous_with_default) {
sourcemeta::jsontoolkit::FlatFileSchemaResolver resolver;
const auto schema_path{std::filesystem::path{SCHEMAS_PATH} /
Expand Down
4 changes: 4 additions & 0 deletions test/jsonschema/schemas/2020-12-without-extension.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$id": "https://www.sourcemeta.com/2020-12-without-extension",
"$schema": "https://json-schema.org/draft/2020-12/schema"
}

0 comments on commit 408d290

Please sign in to comment.