Skip to content

Return found identifiers in FlatFileSchemaResolver #1418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ class SOURCEMETA_JSONTOOLKIT_JSONSCHEMA_EXPORT FlatFileSchemaResolver {
/// Construct an empty resolver that has another schema resolver as a fallback
FlatFileSchemaResolver(const SchemaResolver &resolver);

/// Register a schema to the flat file resolver
/// Register a schema to the flat file resolver, returning the detected
/// identifier for the schema
auto add(const std::filesystem::path &path,
const std::optional<std::string> &default_dialect = std::nullopt,
const std::optional<std::string> &default_id = std::nullopt) -> void;
const std::optional<std::string> &default_id = std::nullopt)
-> const std::string &;

/// Attempt to resolve a schema
auto operator()(std::string_view identifier) const -> std::optional<JSON>;
Expand Down
4 changes: 3 additions & 1 deletion src/jsonschema/resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ FlatFileSchemaResolver::FlatFileSchemaResolver(const SchemaResolver &resolver)
auto FlatFileSchemaResolver::add(
const std::filesystem::path &path,
const std::optional<std::string> &default_dialect,
const std::optional<std::string> &default_id) -> void {
const std::optional<std::string> &default_id) -> const std::string & {
const auto canonical{std::filesystem::canonical(path)};
const auto schema{sourcemeta::jsontoolkit::from_file(canonical)};
assert(sourcemeta::jsontoolkit::is_schema(schema));
Expand All @@ -103,6 +103,8 @@ auto FlatFileSchemaResolver::add(
<< identifier.value();
throw SchemaError(error.str());
}

return result.first->first;
}

auto FlatFileSchemaResolver::operator()(std::string_view identifier) const
Expand Down
40 changes: 27 additions & 13 deletions test/jsonschema/jsonschema_flat_file_resolver_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ TEST(JSONSchema_FlatFileSchemaResolver, single_schema) {
sourcemeta::jsontoolkit::FlatFileSchemaResolver resolver;
const auto schema_path{std::filesystem::path{SCHEMAS_PATH} /
"2020-12-id.json"};
resolver.add(schema_path);
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.json").has_value());
EXPECT_EQ(resolver("https://www.sourcemeta.com/2020-12-id.json").value(),
Expand All @@ -39,8 +40,9 @@ TEST(JSONSchema_FlatFileSchemaResolver, single_schema_with_default_dialect) {
"$schema": "https://json-schema.org/draft/2020-12/schema"
})JSON");

resolver.add(schema_path, "https://json-schema.org/draft/2020-12/schema");

const auto &identifier{resolver.add(
schema_path, "https://json-schema.org/draft/2020-12/schema")};
EXPECT_EQ(identifier, "https://www.sourcemeta.com/only-id.json");
EXPECT_TRUE(resolver("https://www.sourcemeta.com/only-id.json").has_value());
EXPECT_EQ(resolver("https://www.sourcemeta.com/only-id.json").value(),
expected);
Expand All @@ -57,8 +59,9 @@ TEST(JSONSchema_FlatFileSchemaResolver, single_schema_anonymous_with_default) {
"$schema": "https://json-schema.org/draft/2020-12/schema"
})JSON");

resolver.add(schema_path, std::nullopt, "https://www.sourcemeta.com/test");

const auto &identifier{resolver.add(schema_path, std::nullopt,
"https://www.sourcemeta.com/test")};
EXPECT_EQ(identifier, "https://www.sourcemeta.com/test");
EXPECT_TRUE(resolver("https://www.sourcemeta.com/test").has_value());
EXPECT_EQ(resolver("https://www.sourcemeta.com/test").value(), expected);
}
Expand All @@ -68,9 +71,13 @@ TEST(JSONSchema_FlatFileSchemaResolver, single_schema_idempotent) {
const auto schema_path{std::filesystem::path{SCHEMAS_PATH} /
"2020-12-id.json"};

resolver.add(schema_path);
resolver.add(schema_path);
resolver.add(schema_path);
const auto &identifier_1{resolver.add(schema_path)};
const auto &identifier_2{resolver.add(schema_path)};
const auto &identifier_3{resolver.add(schema_path)};

EXPECT_EQ(identifier_1, "https://www.sourcemeta.com/2020-12-id.json");
EXPECT_EQ(identifier_2, "https://www.sourcemeta.com/2020-12-id.json");
EXPECT_EQ(identifier_3, "https://www.sourcemeta.com/2020-12-id.json");

EXPECT_TRUE(
resolver("https://www.sourcemeta.com/2020-12-id.json").has_value());
Expand All @@ -86,7 +93,8 @@ TEST(JSONSchema_FlatFileSchemaResolver, duplicate_ids) {
const auto schema_path_2{std::filesystem::path{SCHEMAS_PATH} /
"2020-12-id-duplicate.json"};

resolver.add(schema_path_1);
const auto &identifier{resolver.add(schema_path_1)};
EXPECT_EQ(identifier, "https://www.sourcemeta.com/2020-12-id.json");
EXPECT_THROW(resolver.add(schema_path_2),
sourcemeta::jsontoolkit::SchemaError);
}
Expand All @@ -96,7 +104,8 @@ TEST(JSONSchema_FlatFileSchemaResolver, no_embedded_resource) {
const auto schema_path{std::filesystem::path{SCHEMAS_PATH} /
"2020-12-embedded.json"};

resolver.add(schema_path);
const auto &identifier{resolver.add(schema_path)};
EXPECT_EQ(identifier, "https://www.sourcemeta.com/2020-12-embedded.json");

EXPECT_TRUE(
resolver("https://www.sourcemeta.com/2020-12-embedded.json").has_value());
Expand All @@ -113,15 +122,19 @@ TEST(JSONSchema_FlatFileSchemaResolver, metaschema_out_of_order) {
const auto metaschema_path{std::filesystem::path{SCHEMAS_PATH} /
"2020-12-meta-1.json"};

resolver.add(schema_path);
const auto &identifier{resolver.add(schema_path)};
EXPECT_EQ(identifier,
"https://www.sourcemeta.com/2020-12-meta-1-schema.json");

// We can't resolve it yet until eventually satisfying the metaschema
EXPECT_THROW(
resolver("https://www.sourcemeta.com/2020-12-meta-1-schema.json"),
sourcemeta::jsontoolkit::SchemaResolutionError);

// Note we add the metaschema AFTER the schema
resolver.add(metaschema_path);
const auto &metaschema_identifier{resolver.add(metaschema_path)};
EXPECT_EQ(metaschema_identifier,
"https://www.sourcemeta.com/2020-12-meta-1.json");

EXPECT_TRUE(
resolver("https://www.sourcemeta.com/2020-12-meta-1.json").has_value());
Expand All @@ -139,7 +152,8 @@ TEST(JSONSchema_FlatFileSchemaResolver, iterators) {
sourcemeta::jsontoolkit::FlatFileSchemaResolver resolver;
const auto schema_path{std::filesystem::path{SCHEMAS_PATH} /
"2020-12-id.json"};
resolver.add(schema_path);
const auto &identifier{resolver.add(schema_path)};
EXPECT_EQ(identifier, "https://www.sourcemeta.com/2020-12-id.json");

std::vector<std::string> identifiers;
std::vector<sourcemeta::jsontoolkit::FlatFileSchemaResolver::Entry> entries;
Expand Down
Loading