Skip to content

Commit ca03804

Browse files
authored
Extend SchemaMapResolver::add to return a boolean value (#1630)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent dfc6083 commit ca03804

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

src/core/jsonschema/include/sourcemeta/core/jsonschema_resolver.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ class SOURCEMETA_CORE_JSONSCHEMA_EXPORT SchemaMapResolver {
4949
/// Construct an empty resolver that has another schema resolver as a fallback
5050
SchemaMapResolver(const SchemaResolver &resolver);
5151

52-
/// Register a schema to the map resolver
52+
/// Register a schema to the map resolver. Returns whether at least one
53+
/// schema was imported into the resolver
5354
auto add(const JSON &schema,
5455
const std::optional<std::string> &default_dialect = std::nullopt,
55-
const std::optional<std::string> &default_id = std::nullopt) -> void;
56+
const std::optional<std::string> &default_id = std::nullopt) -> bool;
5657

5758
/// Attempt to resolve a schema
5859
auto operator()(std::string_view identifier) const -> std::optional<JSON>;

src/core/jsonschema/resolver.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ SchemaMapResolver::SchemaMapResolver(const SchemaResolver &resolver)
1515
auto SchemaMapResolver::add(const JSON &schema,
1616
const std::optional<std::string> &default_dialect,
1717
const std::optional<std::string> &default_id)
18-
-> void {
18+
-> bool {
1919
assert(sourcemeta::core::is_schema(schema));
2020

2121
// Registering the top-level schema is not enough. We need to check
@@ -24,6 +24,7 @@ auto SchemaMapResolver::add(const JSON &schema,
2424
frame.analyse(schema, schema_official_walker, *this, default_dialect,
2525
default_id);
2626

27+
bool added_any_schema{false};
2728
for (const auto &[key, entry] : frame.locations()) {
2829
if (entry.type != SchemaFrame::LocationType::Resource) {
2930
continue;
@@ -58,7 +59,11 @@ auto SchemaMapResolver::add(const JSON &schema,
5859
error << "Cannot register the same identifier twice: " << key.second;
5960
throw SchemaError(error.str());
6061
}
62+
63+
added_any_schema = true;
6164
}
65+
66+
return added_any_schema;
6267
}
6368

6469
auto SchemaMapResolver::operator()(std::string_view identifier) const

test/jsonschema/jsonschema_map_resolver_test.cc

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ TEST(JSONSchema_SchemaMapResolver, single_schema) {
2626
"$schema": "https://json-schema.org/draft/2020-12/schema"
2727
})JSON");
2828

29-
resolver.add(document);
29+
const auto result{resolver.add(document)};
30+
EXPECT_TRUE(result);
3031

3132
EXPECT_TRUE(resolver("https://www.sourcemeta.com/test").has_value());
3233
EXPECT_EQ(resolver("https://www.sourcemeta.com/test").value(), document);
@@ -44,7 +45,9 @@ TEST(JSONSchema_SchemaMapResolver, single_schema_with_default_dialect) {
4445
"$schema": "https://json-schema.org/draft/2020-12/schema"
4546
})JSON");
4647

47-
resolver.add(document, "https://json-schema.org/draft/2020-12/schema");
48+
const auto result{
49+
resolver.add(document, "https://json-schema.org/draft/2020-12/schema")};
50+
EXPECT_TRUE(result);
4851

4952
EXPECT_TRUE(resolver("https://www.sourcemeta.com/test").has_value());
5053
EXPECT_EQ(resolver("https://www.sourcemeta.com/test").value(), expected);
@@ -62,7 +65,9 @@ TEST(JSONSchema_SchemaMapResolver, single_schema_anonymous_with_default) {
6265
"$schema": "https://json-schema.org/draft/2020-12/schema"
6366
})JSON");
6467

65-
resolver.add(document, std::nullopt, "https://www.sourcemeta.com/test");
68+
const auto result{
69+
resolver.add(document, std::nullopt, "https://www.sourcemeta.com/test")};
70+
EXPECT_TRUE(result);
6671

6772
EXPECT_TRUE(resolver("https://www.sourcemeta.com/test").has_value());
6873
EXPECT_EQ(resolver("https://www.sourcemeta.com/test").value(), expected);
@@ -76,9 +81,13 @@ TEST(JSONSchema_SchemaMapResolver, single_schema_idempotent) {
7681
"$schema": "https://json-schema.org/draft/2020-12/schema"
7782
})JSON");
7883

79-
resolver.add(document);
80-
resolver.add(document);
81-
resolver.add(document);
84+
const auto result_1{resolver.add(document)};
85+
const auto result_2{resolver.add(document)};
86+
const auto result_3{resolver.add(document)};
87+
88+
EXPECT_TRUE(result_1);
89+
EXPECT_TRUE(result_2);
90+
EXPECT_TRUE(result_3);
8291

8392
EXPECT_TRUE(resolver("https://www.sourcemeta.com/test").has_value());
8493
EXPECT_EQ(resolver("https://www.sourcemeta.com/test").value(), document);
@@ -100,7 +109,8 @@ TEST(JSONSchema_SchemaMapResolver, duplicate_ids) {
100109
"type": "string"
101110
})JSON");
102111

103-
resolver.add(document_1);
112+
const auto result{resolver.add(document_1)};
113+
EXPECT_TRUE(result);
104114
EXPECT_THROW(resolver.add(document_2), sourcemeta::core::SchemaError);
105115
}
106116

@@ -118,7 +128,8 @@ TEST(JSONSchema_SchemaMapResolver, embedded_resource) {
118128
}
119129
})JSON");
120130

121-
resolver.add(document);
131+
const auto result{resolver.add(document)};
132+
EXPECT_TRUE(result);
122133

123134
const sourcemeta::core::JSON embedded = sourcemeta::core::parse_json(R"JSON({
124135
"$id": "https://www.sourcemeta.com/string",

0 commit comments

Comments
 (0)