diff --git a/src/spec/jsonschema/frame.cc b/src/spec/jsonschema/frame.cc index 1c90f9953b..c22bc46303 100644 --- a/src/spec/jsonschema/frame.cc +++ b/src/spec/jsonschema/frame.cc @@ -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(""); +} + // 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 @@ -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); } } @@ -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), @@ -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; } } @@ -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; } } @@ -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; } } diff --git a/src/spec/jsonschema/known_resolver.in.cc b/src/spec/jsonschema/known_resolver.in.cc index cd5b484566..c90c91d554 100644 --- a/src/spec/jsonschema/known_resolver.in.cc +++ b/src/spec/jsonschema/known_resolver.in.cc @@ -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" || diff --git a/test/spec/jsonschema/bundle/2019-09/recursive_anchor_beside_a_name_the_caller_gave.json b/test/spec/jsonschema/bundle/2019-09/recursive_anchor_beside_a_name_the_caller_gave.json new file mode 100644 index 0000000000..3ae3970ef5 --- /dev/null +++ b/test/spec/jsonschema/bundle/2019-09/recursive_anchor_beside_a_name_the_caller_gave.json @@ -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": [] } +} diff --git a/test/spec/jsonschema/jsonschema_frame_test.cc b/test/spec/jsonschema/jsonschema_frame_test.cc index 198ec34978..10b852a4b4 100644 --- a/test/spec/jsonschema/jsonschema_frame_test.cc +++ b/test/spec/jsonschema/jsonschema_frame_test.cc @@ -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",