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

Remove anchors() from the public JSON Schema interface #1500

Merged
merged 1 commit into from
Jan 28, 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
4 changes: 2 additions & 2 deletions src/core/jsonschema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ include(./official_resolver.cmake)

sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME jsonschema
FOLDER "Core/JSON Schema"
PRIVATE_HEADERS anchor.h bundle.h resolver.h
PRIVATE_HEADERS bundle.h resolver.h
walker.h reference.h frame.h error.h unevaluated.h
keywords.h transform.h
SOURCES jsonschema.cc default_walker.cc frame.cc
anchor.cc resolver.cc walker.cc bundle.cc
resolver.cc walker.cc bundle.cc
unevaluated.cc relativize.cc unidentify.cc
transform_rule.cc transformer.cc
"${CMAKE_CURRENT_BINARY_DIR}/official_resolver.cc")
Expand Down
101 changes: 0 additions & 101 deletions src/core/jsonschema/anchor.cc

This file was deleted.

102 changes: 92 additions & 10 deletions src/core/jsonschema/frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,92 @@
#include <utility> // std::pair, std::move
#include <vector> // std::vector

enum class AnchorType : std::uint8_t { Static, Dynamic, All };

static auto find_anchors(const sourcemeta::core::JSON &schema,
const std::map<std::string, bool> &vocabularies)
-> std::map<std::string, AnchorType> {
std::map<std::string, AnchorType> result;

// 2020-12
if (schema.is_object() &&
vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/core")) {
if (schema.defines("$dynamicAnchor")) {
const auto &anchor{schema.at("$dynamicAnchor")};
assert(anchor.is_string());
result.insert({anchor.to_string(), AnchorType::Dynamic});
}

if (schema.defines("$anchor")) {
const auto &anchor{schema.at("$anchor")};
assert(anchor.is_string());
const auto anchor_string{anchor.to_string()};
const auto success = result.insert({anchor_string, AnchorType::Static});
assert(success.second || result.contains(anchor_string));
if (!success.second) {
result[anchor_string] = AnchorType::All;
}
}
}

// 2019-09
if (schema.is_object() &&
vocabularies.contains(
"https://json-schema.org/draft/2019-09/vocab/core")) {
if (schema.defines("$recursiveAnchor")) {
const auto &anchor{schema.at("$recursiveAnchor")};
assert(anchor.is_boolean());
if (anchor.to_boolean()) {
// We store a 2019-09 recursive anchor as an empty anchor
result.insert({"", AnchorType::Dynamic});
}
}

if (schema.defines("$anchor")) {
const auto &anchor{schema.at("$anchor")};
assert(anchor.is_string());
const auto anchor_string{anchor.to_string()};
const auto success = result.insert({anchor_string, AnchorType::Static});
assert(success.second || result.contains(anchor_string));
if (!success.second) {
result[anchor_string] = AnchorType::All;
}
}
}

// Draft 7 and 6
// Old `$id` anchor form
if (schema.is_object() &&
(vocabularies.contains("http://json-schema.org/draft-07/schema#") ||
vocabularies.contains("http://json-schema.org/draft-06/schema#"))) {
if (schema.defines("$id")) {
assert(schema.at("$id").is_string());
const sourcemeta::core::URI identifier(schema.at("$id").to_string());
if (identifier.is_fragment_only()) {
result.insert(
{std::string{identifier.fragment().value()}, AnchorType::Static});
}
}
}

// Draft 4
// Old `id` anchor form
if (schema.is_object() &&
vocabularies.contains("http://json-schema.org/draft-04/schema#")) {
if (schema.defines("id")) {
assert(schema.at("id").is_string());
const sourcemeta::core::URI identifier(schema.at("id").to_string());
if (identifier.is_fragment_only()) {
result.insert(
{std::string{identifier.fragment().value()}, AnchorType::Static});
}
}
}

return result;
}

static auto find_nearest_bases(
const std::map<sourcemeta::core::Pointer, std::vector<std::string>> &bases,
const sourcemeta::core::Pointer &pointer,
Expand Down Expand Up @@ -254,26 +340,24 @@ auto internal_analyse(const sourcemeta::core::JSON &schema,
}

// Handle schema anchors
for (const auto &[name, type] : sourcemeta::core::anchors(
entry.common.value, entry.common.vocabularies)) {
for (const auto &[name, type] :
find_anchors(entry.common.value, entry.common.vocabularies)) {
const auto bases{
find_nearest_bases(base_uris, entry.common.pointer, entry.id)};

if (bases.first.empty()) {
const auto anchor_uri{sourcemeta::core::URI::from_fragment(name)};
const auto relative_anchor_uri{anchor_uri.recompose()};

if (type == sourcemeta::core::AnchorType::Static ||
type == sourcemeta::core::AnchorType::All) {
if (type == AnchorType::Static || type == AnchorType::All) {
store(frame, ReferenceType::Static, Frame::LocationType::Anchor,
relative_anchor_uri, root_id, "", entry.common.pointer,
entry.common.pointer.resolve_from(bases.second),
entry.common.dialect.value(),
entry.common.base_dialect.value());
}

if (type == sourcemeta::core::AnchorType::Dynamic ||
type == sourcemeta::core::AnchorType::All) {
if (type == AnchorType::Dynamic || type == AnchorType::All) {
store(frame, ReferenceType::Dynamic, Frame::LocationType::Anchor,
relative_anchor_uri, root_id, "", entry.common.pointer,
entry.common.pointer.resolve_from(bases.second),
Expand Down Expand Up @@ -310,8 +394,7 @@ auto internal_analyse(const sourcemeta::core::JSON &schema,
continue;
}

if (type == sourcemeta::core::AnchorType::Static ||
type == sourcemeta::core::AnchorType::All) {
if (type == AnchorType::Static || type == AnchorType::All) {
store(frame, sourcemeta::core::ReferenceType::Static,
Frame::LocationType::Anchor, anchor_uri, root_id, base_string,
entry.common.pointer,
Expand All @@ -320,8 +403,7 @@ auto internal_analyse(const sourcemeta::core::JSON &schema,
entry.common.base_dialect.value());
}

if (type == sourcemeta::core::AnchorType::Dynamic ||
type == sourcemeta::core::AnchorType::All) {
if (type == AnchorType::Dynamic || type == AnchorType::All) {
store(frame, sourcemeta::core::ReferenceType::Dynamic,
Frame::LocationType::Anchor, anchor_uri, root_id, base_string,
entry.common.pointer,
Expand Down
1 change: 0 additions & 1 deletion src/core/jsonschema/include/sourcemeta/core/jsonschema.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#endif

#include <sourcemeta/core/json.h>
#include <sourcemeta/core/jsonschema_anchor.h>
#include <sourcemeta/core/jsonschema_bundle.h>
#include <sourcemeta/core/jsonschema_error.h>
#include <sourcemeta/core/jsonschema_frame.h>
Expand Down
64 changes: 0 additions & 64 deletions src/core/jsonschema/include/sourcemeta/core/jsonschema_anchor.h

This file was deleted.

5 changes: 0 additions & 5 deletions test/jsonschema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ sourcemeta_googletest(NAMESPACE sourcemeta PROJECT core NAME jsonschema
FOLDER "Core/JSON Schema"
SOURCES
jsonschema_test_utils.h
jsonschema_anchor_2020_12_test.cc
jsonschema_anchor_2019_09_test.cc
jsonschema_anchor_draft7_test.cc
jsonschema_anchor_draft6_test.cc
jsonschema_anchor_draft4_test.cc
jsonschema_identify_2020_12_test.cc
jsonschema_identify_2019_09_test.cc
jsonschema_identify_draft7_test.cc
Expand Down
Loading