Skip to content

Wrap schemas using a relative default URI identifier #1701

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
May 26, 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
5 changes: 4 additions & 1 deletion src/core/jsonschema/jsonschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,10 @@ auto sourcemeta::core::wrap(const sourcemeta::core::JSON &schema,
// at least an artificial one, otherwise a standalone instance of `$schema`
// outside of the root of a schema resource is not valid according to
// JSON Schema
constexpr auto WRAPPER_IDENTIFIER{"tag:core.sourcemeta.com,2025:wrap"};
// However, note that we use a relative URI so that references to
// other schemas whose top-level identifiers are relative URIs don't
// get affected. Otherwise, we would cause unintended base resolution.
constexpr auto WRAPPER_IDENTIFIER{"__sourcemeta-core-wrap__"};
const auto id{identify(copy, resolver, SchemaIdentificationStrategy::Strict,
default_dialect)
.value_or(WRAPPER_IDENTIFIER)};
Expand Down
42 changes: 36 additions & 6 deletions test/jsonschema/jsonschema_wrap_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ TEST(JSONSchema_wrap, schema_without_identifier) {

const auto expected{sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "tag:core.sourcemeta.com,2025:wrap#/items",
"$ref": "__sourcemeta-core-wrap__#/items",
"$defs": {
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "tag:core.sourcemeta.com,2025:wrap",
"$id": "__sourcemeta-core-wrap__",
"items": {
"type": "string"
}
Expand All @@ -69,6 +69,36 @@ TEST(JSONSchema_wrap, schema_without_identifier) {
EXPECT_EQ(result, expected);
}

TEST(JSONSchema_wrap, schema_without_identifier_and_relative_uri) {
const auto schema{sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"items": {
"$ref": "relative"
}
})JSON")};

const auto result{sourcemeta::core::wrap(
schema, {"items"}, sourcemeta::core::schema_official_resolver)};

// We don't want the relative reference to be resolved against
// an absolute base
const auto expected{sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "__sourcemeta-core-wrap__#/items",
"$defs": {
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "__sourcemeta-core-wrap__",
"items": {
"$ref": "relative"
}
}
}
})JSON")};

EXPECT_EQ(result, expected);
}

TEST(JSONSchema_wrap, schema_without_identifier_with_default_dialect) {
const auto schema{sourcemeta::core::parse_json(R"JSON({
"items": {
Expand All @@ -82,11 +112,11 @@ TEST(JSONSchema_wrap, schema_without_identifier_with_default_dialect) {

const auto expected{sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "tag:core.sourcemeta.com,2025:wrap#/items",
"$ref": "__sourcemeta-core-wrap__#/items",
"$defs": {
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "tag:core.sourcemeta.com,2025:wrap",
"$id": "__sourcemeta-core-wrap__",
"items": {
"type": "string"
}
Expand All @@ -112,11 +142,11 @@ TEST(JSONSchema_wrap,

const auto expected{sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "tag:core.sourcemeta.com,2025:wrap#/items",
"$ref": "__sourcemeta-core-wrap__#/items",
"$defs": {
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "tag:core.sourcemeta.com,2025:wrap",
"$id": "__sourcemeta-core-wrap__",
"items": {
"type": "string"
}
Expand Down