Skip to content

Commit de95f16

Browse files
committed
[WIP] Implement support for custom reachability checks
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent b4c99ff commit de95f16

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/core/jsonschema/frame.cc

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,14 @@ auto SchemaFrame::populate_reachability(const Location &base,
14571457
}
14581458

14591459
for (const auto &pointer_reference : has_non_pointer_location) {
1460-
const bool is_reachable = has_non_orphan.contains(pointer_reference);
1460+
const auto &pointer{pointer_reference.get()};
1461+
bool is_reachable{false};
1462+
if (pointer == base.pointer) {
1463+
is_reachable = true;
1464+
} else if (pointer.starts_with(base.pointer)) {
1465+
is_reachable = has_non_orphan.contains(pointer_reference);
1466+
}
1467+
14611468
cache.emplace(pointer_reference, is_reachable);
14621469
if (!is_reachable) {
14631470
unreachable_pointers.push_back(pointer_reference);
@@ -1474,12 +1481,20 @@ auto SchemaFrame::populate_reachability(const Location &base,
14741481
continue;
14751482
}
14761483

1477-
const auto any_non_orphan{
1478-
std::ranges::any_of(locations, [](const Location *location) {
1479-
return location->type != LocationType::Pointer && !location->orphan;
1480-
})};
1481-
cache.emplace(pointer_reference, any_non_orphan);
1482-
if (!any_non_orphan) {
1484+
const auto &pointer{pointer_reference.get()};
1485+
bool is_reachable{false};
1486+
if (pointer == base.pointer) {
1487+
is_reachable = true;
1488+
} else if (pointer.starts_with(base.pointer)) {
1489+
is_reachable =
1490+
std::ranges::any_of(locations, [](const Location *location) {
1491+
return location->type != LocationType::Pointer &&
1492+
!location->orphan;
1493+
});
1494+
}
1495+
1496+
cache.emplace(pointer_reference, is_reachable);
1497+
if (!is_reachable) {
14831498
unreachable_pointers.push_back(pointer_reference);
14841499
}
14851500
}
@@ -1651,13 +1666,10 @@ auto SchemaFrame::populate_reachability(const Location &base,
16511666
}
16521667

16531668
const auto &source_parent{potential_source.source_pointer->initial()};
1654-
bool source_parent_reachable{source_parent.empty()};
1655-
if (!source_parent_reachable) {
1656-
const auto reachability_iterator{
1657-
cache.find(std::cref(source_parent))};
1658-
source_parent_reachable = reachability_iterator != cache.end() &&
1659-
reachability_iterator->second;
1660-
}
1669+
const auto reachability_iterator{cache.find(std::cref(source_parent))};
1670+
const bool source_parent_reachable{reachability_iterator !=
1671+
cache.end() &&
1672+
reachability_iterator->second};
16611673

16621674
if (source_parent_reachable) {
16631675
became_reachable = true;

test/jsonschema/jsonschema_frame_2020_12_test.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ TEST(JSONSchema_frame_2020_12, anonymous_with_nested_schema_resource) {
112112
frame.root());
113113
EXPECT_FRAME_LOCATION_REACHABLE(frame, Static, "https://example.com",
114114
frame.root());
115+
116+
EXPECT_FRAME_LOCATION_NON_REACHABLE(frame, Static, "", "https://example.com");
117+
EXPECT_FRAME_LOCATION_REACHABLE(frame, Static, "#/additionalProperties",
118+
"https://example.com");
119+
EXPECT_FRAME_LOCATION_REACHABLE(frame, Static, "https://example.com",
120+
"https://example.com");
115121
}
116122

117123
TEST(JSONSchema_frame_2020_12, empty_schema) {

0 commit comments

Comments
 (0)