Skip to content

Commit acfa98a

Browse files
authored
Wrap schemas using a relative default URI identifier (#1701)
Fixes: #1700 See: sourcemeta/jsonschema#338 Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 7678612 commit acfa98a

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/core/jsonschema/jsonschema.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,10 @@ auto sourcemeta::core::wrap(const sourcemeta::core::JSON &schema,
731731
// at least an artificial one, otherwise a standalone instance of `$schema`
732732
// outside of the root of a schema resource is not valid according to
733733
// JSON Schema
734-
constexpr auto WRAPPER_IDENTIFIER{"tag:core.sourcemeta.com,2025:wrap"};
734+
// However, note that we use a relative URI so that references to
735+
// other schemas whose top-level identifiers are relative URIs don't
736+
// get affected. Otherwise, we would cause unintended base resolution.
737+
constexpr auto WRAPPER_IDENTIFIER{"__sourcemeta-core-wrap__"};
735738
const auto id{identify(copy, resolver, SchemaIdentificationStrategy::Strict,
736739
default_dialect)
737740
.value_or(WRAPPER_IDENTIFIER)};

test/jsonschema/jsonschema_wrap_test.cc

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ TEST(JSONSchema_wrap, schema_without_identifier) {
5454

5555
const auto expected{sourcemeta::core::parse_json(R"JSON({
5656
"$schema": "https://json-schema.org/draft/2020-12/schema",
57-
"$ref": "tag:core.sourcemeta.com,2025:wrap#/items",
57+
"$ref": "__sourcemeta-core-wrap__#/items",
5858
"$defs": {
5959
"schema": {
6060
"$schema": "https://json-schema.org/draft/2020-12/schema",
61-
"$id": "tag:core.sourcemeta.com,2025:wrap",
61+
"$id": "__sourcemeta-core-wrap__",
6262
"items": {
6363
"type": "string"
6464
}
@@ -69,6 +69,36 @@ TEST(JSONSchema_wrap, schema_without_identifier) {
6969
EXPECT_EQ(result, expected);
7070
}
7171

72+
TEST(JSONSchema_wrap, schema_without_identifier_and_relative_uri) {
73+
const auto schema{sourcemeta::core::parse_json(R"JSON({
74+
"$schema": "https://json-schema.org/draft/2020-12/schema",
75+
"items": {
76+
"$ref": "relative"
77+
}
78+
})JSON")};
79+
80+
const auto result{sourcemeta::core::wrap(
81+
schema, {"items"}, sourcemeta::core::schema_official_resolver)};
82+
83+
// We don't want the relative reference to be resolved against
84+
// an absolute base
85+
const auto expected{sourcemeta::core::parse_json(R"JSON({
86+
"$schema": "https://json-schema.org/draft/2020-12/schema",
87+
"$ref": "__sourcemeta-core-wrap__#/items",
88+
"$defs": {
89+
"schema": {
90+
"$schema": "https://json-schema.org/draft/2020-12/schema",
91+
"$id": "__sourcemeta-core-wrap__",
92+
"items": {
93+
"$ref": "relative"
94+
}
95+
}
96+
}
97+
})JSON")};
98+
99+
EXPECT_EQ(result, expected);
100+
}
101+
72102
TEST(JSONSchema_wrap, schema_without_identifier_with_default_dialect) {
73103
const auto schema{sourcemeta::core::parse_json(R"JSON({
74104
"items": {
@@ -82,11 +112,11 @@ TEST(JSONSchema_wrap, schema_without_identifier_with_default_dialect) {
82112

83113
const auto expected{sourcemeta::core::parse_json(R"JSON({
84114
"$schema": "https://json-schema.org/draft/2020-12/schema",
85-
"$ref": "tag:core.sourcemeta.com,2025:wrap#/items",
115+
"$ref": "__sourcemeta-core-wrap__#/items",
86116
"$defs": {
87117
"schema": {
88118
"$schema": "https://json-schema.org/draft/2020-12/schema",
89-
"$id": "tag:core.sourcemeta.com,2025:wrap",
119+
"$id": "__sourcemeta-core-wrap__",
90120
"items": {
91121
"type": "string"
92122
}
@@ -112,11 +142,11 @@ TEST(JSONSchema_wrap,
112142

113143
const auto expected{sourcemeta::core::parse_json(R"JSON({
114144
"$schema": "https://json-schema.org/draft/2020-12/schema",
115-
"$ref": "tag:core.sourcemeta.com,2025:wrap#/items",
145+
"$ref": "__sourcemeta-core-wrap__#/items",
116146
"$defs": {
117147
"schema": {
118148
"$schema": "https://json-schema.org/draft/2020-12/schema",
119-
"$id": "tag:core.sourcemeta.com,2025:wrap",
149+
"$id": "__sourcemeta-core-wrap__",
120150
"items": {
121151
"type": "string"
122152
}

0 commit comments

Comments
 (0)