Skip to content
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
59 changes: 30 additions & 29 deletions src/spec/jsonschema/frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,21 @@ auto set_base_and_fragment(sourcemeta::core::SchemaFrame::Reference &entry)
}
}

// A dynamic reference seeks an anchor by name, which its fragment holds. A
// `$recursiveRef` names no anchor at all, as Section 8.2.4.2.2 of JSON Schema
// 2019-09 settles its target by looking for a `$recursiveAnchor` of true
// rather than for a name, so what it seeks is the empty name that such an
// anchor is recorded under. That is also what the URI of one reads back as,
// given that canonicalising it dropped the empty fragment
// See
// https://json-schema.org/draft/2019-09/draft-handrews-json-schema-02#rfc.section.8.2.4.2.2
auto reference_seeks_anchor(
const sourcemeta::core::SchemaFrame::Reference &reference,
const sourcemeta::core::JSON::String &anchor_uri) -> bool {
return sourcemeta::core::URI{anchor_uri}.fragment().value_or("") ==
reference.fragment.value_or("");
Comment on lines +519 to +520

@augmentcode augmentcode Bot Nov 24, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At src/spec/jsonschema/frame.cc:519-520, the new empty-name matching rule is used by the accessors, but reference_destinations() at lines 668-670 still treats a missing fragment as matching every dynamic anchor. With the new mixed-dialect fixture, to_json() consequently reports hasReferencesTo/hasReferencesThrough as true for https://example.com/three while the accessors return false; could we keep these public representations consistent?

Severity: medium

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

}

// A location reports its base as a view, so that view has to point at a
// string that outlives the frame. The key of the location that owns the base
// is the usual answer
Expand Down Expand Up @@ -650,9 +665,7 @@ auto reference_destinations(const References &references,
continue;
}

if (!reference.second.fragment.has_value() ||
sourcemeta::core::URI{location.first.second}.fragment().value_or(
"") == reference.second.fragment.value()) {
if (reference_seeks_anchor(reference.second, location.first.second)) {
result.push_back(location.second.pointer);
}
}
Expand Down Expand Up @@ -896,20 +909,23 @@ auto store_anchors(Locations &locations, Owned &owned,
continue;
}

[[maybe_unused]] bool is_first{true};
bool is_first{true};
for (const auto &base_string : bases.first) {
sourcemeta::core::URI anchor_uri_builder{base_string};
anchor_uri_builder.fragment(name);
anchor_uri_builder.canonicalize();
const auto anchor_uri{anchor_uri_builder.recompose()};

// Two bases of one place never spell a single anchor, as the fragment
// put on them is the same and each base is a name of its own, so the
// first of them is the only one that can have taken the name this one
// is about to
assert(is_first ||
!locations.contains(
{sourcemeta::core::SchemaReferenceType::Static, anchor_uri}));
// An anchor that carries no name, which is how a recursive anchor of
// JSON Schema 2019-09 is recorded, marks the resource it sits in rather
// than a place within it, as it puts no fragment on the base to tell the
// two apart. The mark goes on the first name of that resource, which is
// the one that resolving a reference against the nearest base arrives
// at. A named anchor names a place of its own under every base, so each
// of those is stored and a name already taken collides as it should
if (!is_first && name.empty()) {
continue;
}

store_anchor(locations, max_locations, type, entry, anchor_uri,
owned_base_view(locations, owned, default_base, base_string),
Expand Down Expand Up @@ -2504,12 +2520,7 @@ auto SchemaFrame::has_references_to(
if (location.second.type == LocationType::Anchor &&
location.first.first == SchemaReferenceType::Dynamic &&
location.second.pointer == pointer) {
// A reference reads as dynamic only where it names an anchor,
// which is what its fragment holds, so there is always a name to
// hold the one of the location up against
assert(reference.second.fragment.has_value());
if (sourcemeta::core::URI{location.first.second}.fragment().value_or(
"") == reference.second.fragment.value()) {
if (reference_seeks_anchor(reference.second, location.first.second)) {
return true;
}
}
Expand Down Expand Up @@ -2538,12 +2549,7 @@ auto SchemaFrame::has_references_through(
if (location.second.type == LocationType::Anchor &&
location.first.first == SchemaReferenceType::Dynamic &&
location.second.pointer.starts_with(pointer)) {
// A reference reads as dynamic only where it names an anchor,
// which is what its fragment holds, so there is always a name to
// hold the one of the location up against
assert(reference.second.fragment.has_value());
if (sourcemeta::core::URI{location.first.second}.fragment().value_or(
"") == reference.second.fragment.value()) {
if (reference_seeks_anchor(reference.second, location.first.second)) {
return true;
}
}
Expand Down Expand Up @@ -2573,12 +2579,7 @@ auto SchemaFrame::has_references_through(
if (location.second.type == LocationType::Anchor &&
location.first.first == SchemaReferenceType::Dynamic &&
location.second.pointer.starts_with(pointer, tail)) {
// A reference reads as dynamic only where it names an anchor,
// which is what its fragment holds, so there is always a name to
// hold the one of the location up against
assert(reference.second.fragment.has_value());
if (sourcemeta::core::URI{location.first.second}.fragment().value_or(
"") == reference.second.fragment.value()) {
if (reference_seeks_anchor(reference.second, location.first.second)) {
return true;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/spec/jsonschema/known_resolver.in.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ enum class KnownSchema : std::uint8_t {
UNKNOWN
};

// A dispatch over every identifier that a known schema answers to is a table
// written as branches, so what it costs to read tracks how many schemas there
// are rather than how much there is to follow
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
static auto parse_identifier(const std::string_view identifier) -> KnownSchema {
// JSON Schema 2020-12
if (identifier == "https://json-schema.org/draft/2020-12/schema" ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"schema": {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://www.sourcemeta.com/metadata-tree",
"$recursiveAnchor": true,
"unevaluatedProperties": false
},
"defaultId": "https://www.sourcemeta.com/default",
"result": {
"nonOfficialMetaschemas": {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://www.sourcemeta.com/metadata-tree",
"$recursiveAnchor": true,
"unevaluatedProperties": false
},
"references": {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://www.sourcemeta.com/metadata-tree",
"$recursiveAnchor": true,
"unevaluatedProperties": false
}
},
"inserted": { "nonOfficialMetaschemas": [], "references": [] }
}
213 changes: 213 additions & 0 deletions test/spec/jsonschema/jsonschema_frame_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,219 @@ TEST(accessors_has_references_through_with_tail_dynamic) {
sourcemeta::core::to_weak_pointer(properties), other_token));
}

// A `$recursiveRef` names no anchor, as JSON Schema 2019-09 Section 8.2.4.2.2
// settles its target by looking for a `$recursiveAnchor` of true rather than
// for a name, and framing records such an anchor under the empty name
// See
// https://json-schema.org/draft/2019-09/draft-handrews-json-schema-02#rfc.section.8.2.4.2.2
TEST(accessors_has_references_to_recursive) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$id": "https://example.com/schema",
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$recursiveAnchor": true,
"$defs": {
"a": { "$id": "one", "$recursiveAnchor": true },
"b": { "$id": "two" },
"c": {
"$id": "https://example.com/three",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$dynamicAnchor": "shared"
}
},
"properties": { "value": { "$recursiveRef": "#" } }
})JSON");

const sourcemeta::core::SchemaFrame frame{
sourcemeta::core::SchemaFrame::Mode::References, document,
sourcemeta::core::schema_walker, sourcemeta::core::schema_resolver};

EXPECT_REFERENCE(frame, sourcemeta::core::SchemaReferenceType::Dynamic,
"/properties/value/$recursiveRef",
"https://example.com/schema", "https://example.com/schema",
std::nullopt, "#");

const sourcemeta::core::Pointer root;
const sourcemeta::core::Pointer first{"$defs", "a"};
const sourcemeta::core::Pointer second{"$defs", "b"};
const sourcemeta::core::Pointer third{"$defs", "c"};
const sourcemeta::core::Pointer value{"properties", "value"};
EXPECT_TRUE(frame.has_references_to(sourcemeta::core::to_weak_pointer(root)));
EXPECT_TRUE(
frame.has_references_to(sourcemeta::core::to_weak_pointer(first)));
EXPECT_FALSE(
frame.has_references_to(sourcemeta::core::to_weak_pointer(second)));
EXPECT_FALSE(
frame.has_references_to(sourcemeta::core::to_weak_pointer(third)));
EXPECT_FALSE(
frame.has_references_to(sourcemeta::core::to_weak_pointer(value)));
}

TEST(accessors_has_references_through_recursive) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$id": "https://example.com/schema",
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$recursiveAnchor": true,
"$defs": {
"a": { "$id": "one", "$recursiveAnchor": true }
},
"items": {
"$id": "https://example.com/two",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$dynamicAnchor": "shared"
},
"properties": { "value": { "$recursiveRef": "#" } }
})JSON");

const sourcemeta::core::SchemaFrame frame{
sourcemeta::core::SchemaFrame::Mode::References, document,
sourcemeta::core::schema_walker, sourcemeta::core::schema_resolver};

const sourcemeta::core::Pointer definitions{"$defs"};
const sourcemeta::core::Pointer items{"items"};
EXPECT_TRUE(frame.has_references_through(
sourcemeta::core::to_weak_pointer(definitions)));
EXPECT_FALSE(
frame.has_references_through(sourcemeta::core::to_weak_pointer(items)));
}

TEST(accessors_has_references_through_with_tail_recursive) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$id": "https://example.com/schema",
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$recursiveAnchor": true,
"$defs": {
"a": { "$id": "one", "$recursiveAnchor": true }
},
"items": {
"$id": "https://example.com/two",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$dynamicAnchor": "shared"
},
"properties": { "value": { "$recursiveRef": "#" } }
})JSON");

const sourcemeta::core::SchemaFrame frame{
sourcemeta::core::SchemaFrame::Mode::References, document,
sourcemeta::core::schema_walker, sourcemeta::core::schema_resolver};

const sourcemeta::core::Pointer definitions{"$defs"};
const sourcemeta::core::Pointer items{"items"};
const sourcemeta::core::Pointer first_extra{"$defs", "a", "extra"};
const sourcemeta::core::JSON::String extra{"extra"};
const sourcemeta::core::JSON::String other{"other"};
const sourcemeta::core::WeakPointer::Token extra_token{std::cref(extra)};
const sourcemeta::core::WeakPointer::Token other_token{std::cref(other)};
EXPECT_TRUE(frame.has_references_through(
sourcemeta::core::to_weak_pointer(first_extra), extra_token));
EXPECT_FALSE(frame.has_references_through(
sourcemeta::core::to_weak_pointer(first_extra), other_token));
EXPECT_TRUE(frame.has_references_through(
sourcemeta::core::to_weak_pointer(definitions), other_token));
EXPECT_FALSE(frame.has_references_through(
sourcemeta::core::to_weak_pointer(items), other_token));
}

// A recursive anchor carries no name, so the URI that names it is the base URI
// of the resource it marks rather than a fragment of it. Where the caller
// hands framing an identifier of its own, the resource answers to two names,
// and each of them already holds a location
TEST(recursive_anchor_of_a_resource_the_caller_names_too) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$id": "https://example.com/schema",
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$recursiveAnchor": true
})JSON");

const sourcemeta::core::SchemaFrame frame{
sourcemeta::core::SchemaFrame::Mode::Locations,
document,
sourcemeta::core::schema_walker,
sourcemeta::core::schema_resolver,
"",
"https://example.com/default"};

EXPECT_EQ(frame.location_count(), 3);
EXPECT_TRUE(frame
.location(sourcemeta::core::SchemaReferenceType::Static,
"https://example.com/schema")
.has_value());
EXPECT_TRUE(frame
.location(sourcemeta::core::SchemaReferenceType::Static,
"https://example.com/default")
.has_value());
EXPECT_TRUE(frame
.location(sourcemeta::core::SchemaReferenceType::Dynamic,
"https://example.com/schema")
.has_value());
EXPECT_FALSE(frame
.location(sourcemeta::core::SchemaReferenceType::Dynamic,
"https://example.com/default")
.has_value());
}

// The frame reports what a reference reaches both through its accessors and
// through its JSON representation, so the two have to agree on which places a
// reference that names no anchor reaches
TEST(to_json_agrees_with_the_accessors_on_a_recursive_reference) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$id": "https://example.com/schema",
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$recursiveAnchor": true,
"$defs": {
"a": { "$id": "one", "$recursiveAnchor": true },
"c": {
"$id": "https://example.com/three",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$dynamicAnchor": "shared"
}
},
"properties": { "value": { "$recursiveRef": "#" } }
})JSON");

const sourcemeta::core::SchemaFrame frame{
sourcemeta::core::SchemaFrame::Mode::References, document,
sourcemeta::core::schema_walker, sourcemeta::core::schema_resolver};

const auto result{frame.to_json(sourcemeta::core::schema_resolver)};
const auto &reported{result.at("locations").at("static")};

const sourcemeta::core::Pointer root;
const sourcemeta::core::Pointer first{"$defs", "a"};
const sourcemeta::core::Pointer third{"$defs", "c"};

EXPECT_TRUE(reported.at("https://example.com/schema")
.at("hasReferencesTo")
.to_boolean());
EXPECT_EQ(reported.at("https://example.com/schema")
.at("hasReferencesTo")
.to_boolean(),
frame.has_references_to(sourcemeta::core::to_weak_pointer(root)));

EXPECT_TRUE(reported.at("https://example.com/one")
.at("hasReferencesTo")
.to_boolean());
EXPECT_EQ(
reported.at("https://example.com/one").at("hasReferencesTo").to_boolean(),
frame.has_references_to(sourcemeta::core::to_weak_pointer(first)));

EXPECT_FALSE(reported.at("https://example.com/three")
.at("hasReferencesTo")
.to_boolean());
EXPECT_EQ(reported.at("https://example.com/three")
.at("hasReferencesTo")
.to_boolean(),
frame.has_references_to(sourcemeta::core::to_weak_pointer(third)));

EXPECT_FALSE(reported.at("https://example.com/three")
.at("hasReferencesThrough")
.to_boolean());
EXPECT_EQ(
reported.at("https://example.com/three")
.at("hasReferencesThrough")
.to_boolean(),
frame.has_references_through(sourcemeta::core::to_weak_pointer(third)));
}

TEST(accessors_traverse_pointer_with_type) {
const sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$id": "https://example.com/schema",
Expand Down
Loading