Skip to content
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

Don't depend on destination_of for framing instance locations #1554

Merged
merged 1 commit into from
Feb 11, 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
58 changes: 50 additions & 8 deletions src/core/jsonschema/frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ struct InternalEntry {
};

static auto traverse_origin_instance_locations(
const sourcemeta::core::SchemaFrame::References &references,
const sourcemeta::core::SchemaFrame::Locations &frame,
const sourcemeta::core::SchemaFrame::Location &entry,
const std::optional<sourcemeta::core::PointerTemplate> &current,
Expand All @@ -273,16 +274,56 @@ static auto traverse_origin_instance_locations(
output.push_back(current.value());
}

for (const auto &origin : entry.destination_of) {
const auto &subentry{frame.at(origin.get())};
// Avoid recursing to itself, in the case of circular subschemas
if (subentry.pointer == entry.pointer) {
// TODO: This whole algorithm gets simpler if let Pointer locations
// point to parents, as we can get the Pointer location from a reference

for (const auto &reference : references) {
if (reference.first.second.back().to_property() == "$schema") {
continue;
}

// Ignore if this is not a reference pointing to us
if (frame.contains({sourcemeta::core::SchemaReferenceType::Static,
reference.second.destination})) {
if (frame
.at({sourcemeta::core::SchemaReferenceType::Static,
reference.second.destination})
.pointer != entry.pointer) {
continue;
}
} else if (frame.contains({sourcemeta::core::SchemaReferenceType::Dynamic,
reference.second.destination})) {
if (frame
.at({sourcemeta::core::SchemaReferenceType::Dynamic,
reference.second.destination})
.pointer != entry.pointer) {
continue;
}
} else {
continue;
}

for (const auto &instance_location : subentry.instance_locations) {
traverse_origin_instance_locations(frame, subentry, instance_location,
output);
for (const auto &location : frame) {
if (location.second.type !=
sourcemeta::core::SchemaFrame::LocationType::Resource &&
location.second.type !=
sourcemeta::core::SchemaFrame::LocationType::Subschema) {
continue;
}

if (location.second.pointer != reference.first.second.initial()) {
continue;
}

// Avoid recursing to itself, in the case of circular subschemas
if (location.second.pointer == entry.pointer) {
continue;
}

for (const auto &instance_location : location.second.instance_locations) {
traverse_origin_instance_locations(references, frame, location.second,
instance_location, output);
}
}
}
}
Expand Down Expand Up @@ -913,7 +954,8 @@ auto internal_analyse(const sourcemeta::core::SchemaFrame::Mode mode,

// Calculate alternative unresolved instance locations
for (auto &entry : frame) {
traverse_origin_instance_locations(frame, entry.second, std::nullopt,
traverse_origin_instance_locations(references, frame, entry.second,
std::nullopt,
entry.second.instance_locations);
}

Expand Down
3 changes: 2 additions & 1 deletion test/jsonschema/jsonschema_frame_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,8 @@ TEST(JSONSchema_frame_2019_09, recursive_ref_nested_recursive_anchor_true) {
EXPECT_FRAME_STATIC_2019_09_RESOURCE(
frame, "https://www.sourcemeta.com/schema",
"https://www.sourcemeta.com/schema", "",
"https://www.sourcemeta.com/schema", "", {""}, 0, std::nullopt);
"https://www.sourcemeta.com/schema", "", POINTER_TEMPLATES("", "/~I~"), 0,
std::nullopt);

EXPECT_FRAME_STATIC_2019_09_POINTER(
frame, "https://www.sourcemeta.com/schema#/$id",
Expand Down