Skip to content

Commit

Permalink
Mark subschemas in .frame()
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
  • Loading branch information
jviotti committed Dec 23, 2024
1 parent 2e2d204 commit 60323bc
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 211 deletions.
20 changes: 14 additions & 6 deletions src/jsonschema/frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
#include <functional> // std::less
#include <map> // std::map
#include <optional> // std::optional
#include <set> // std::set
#include <sstream> // std::ostringstream
#include <utility> // std::pair, std::move
#include <vector> // std::vector

#include <iostream>

static auto find_nearest_bases(const std::map<sourcemeta::jsontoolkit::Pointer,
std::vector<std::string>> &bases,
const sourcemeta::jsontoolkit::Pointer &pointer,
Expand Down Expand Up @@ -138,6 +137,7 @@ auto sourcemeta::jsontoolkit::frame(
const std::optional<std::string> &default_dialect,
const std::optional<std::string> &default_id) -> void {
std::vector<InternalEntry> subschema_entries;
std::set<Pointer> subschemas;
std::map<sourcemeta::jsontoolkit::Pointer, std::vector<std::string>>
base_uris;
std::map<sourcemeta::jsontoolkit::Pointer, std::vector<std::string>>
Expand Down Expand Up @@ -186,6 +186,7 @@ auto sourcemeta::jsontoolkit::frame(

// Store information
subschema_entries.emplace_back(InternalEntry{entry, std::move(id)});
subschemas.insert(entry.pointer);
}

for (const auto &entry : subschema_entries) {
Expand Down Expand Up @@ -376,10 +377,17 @@ auto sourcemeta::jsontoolkit::frame(
const auto nearest_bases{
find_nearest_bases(base_uris, pointer, base.first)};
assert(!nearest_bases.first.empty());
store(frame, ReferenceType::Static, FrameLocationType::Pointer, result,
root_id, nearest_bases.first.front(), pointer,
pointer.resolve_from(nearest_bases.second),
dialects.first.front());
if (subschemas.contains(pointer)) {
store(frame, ReferenceType::Static, FrameLocationType::Subschema,
result, root_id, nearest_bases.first.front(), pointer,
pointer.resolve_from(nearest_bases.second),
dialects.first.front());
} else {
store(frame, ReferenceType::Static, FrameLocationType::Pointer,
result, root_id, nearest_bases.first.front(), pointer,
pointer.resolve_from(nearest_bases.second),
dialects.first.front());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ using FrameReferences =
#endif
/// @ingroup jsonschema
/// The type of a location frame
enum class FrameLocationType : std::uint8_t { Resource, Anchor, Pointer };
enum class FrameLocationType : std::uint8_t {
Resource,
Anchor,
Pointer,
Subschema
};
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
Expand Down
103 changes: 56 additions & 47 deletions test/jsonschema/jsonschema_frame_2019_09_test.cc

Large diffs are not rendered by default.

83 changes: 46 additions & 37 deletions test/jsonschema/jsonschema_frame_2020_12_test.cc

Large diffs are not rendered by default.

26 changes: 17 additions & 9 deletions test/jsonschema/jsonschema_frame_draft0_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
expected_base, expected_relative_pointer, \
expected_destination_of_size);

#define EXPECT_FRAME_STATIC_DRAFT0_SUBSCHEMA( \
frame, reference, root_id, expected_pointer, expected_base, \
expected_relative_pointer, expected_destination_of_size) \
EXPECT_FRAME_STATIC_SUBSCHEMA(frame, reference, root_id, expected_pointer, \
"http://json-schema.org/draft-00/schema#", \
expected_base, expected_relative_pointer, \
expected_destination_of_size);

TEST(JSONSchema_frame_draft0, anonymous_with_nested_schema_resource) {
const sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
Expand All @@ -46,12 +54,12 @@ TEST(JSONSchema_frame_draft0, anonymous_with_nested_schema_resource) {
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
frame, "https://example.com#/id", "/additionalProperties/id",
"http://json-schema.org/draft-00/schema#", 0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
EXPECT_ANONYMOUS_FRAME_STATIC_SUBSCHEMA(
frame, "", "", "http://json-schema.org/draft-00/schema#", 0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
frame, "#/$schema", "/$schema", "http://json-schema.org/draft-00/schema#",
0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
EXPECT_ANONYMOUS_FRAME_STATIC_SUBSCHEMA(
frame, "#/additionalProperties", "/additionalProperties",
"http://json-schema.org/draft-00/schema#", 0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
Expand Down Expand Up @@ -139,7 +147,7 @@ TEST(JSONSchema_frame_draft0, one_level_applicators_without_identifiers) {
frame, "https://www.sourcemeta.com/schema#/$schema",
"https://www.sourcemeta.com/schema", "/$schema",
"https://www.sourcemeta.com/schema", "/$schema", 0);
EXPECT_FRAME_STATIC_DRAFT0_POINTER(
EXPECT_FRAME_STATIC_DRAFT0_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/items",
"https://www.sourcemeta.com/schema", "/items",
"https://www.sourcemeta.com/schema", "/items", 0);
Expand All @@ -151,7 +159,7 @@ TEST(JSONSchema_frame_draft0, one_level_applicators_without_identifiers) {
frame, "https://www.sourcemeta.com/schema#/properties",
"https://www.sourcemeta.com/schema", "/properties",
"https://www.sourcemeta.com/schema", "/properties", 0);
EXPECT_FRAME_STATIC_DRAFT0_POINTER(
EXPECT_FRAME_STATIC_DRAFT0_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/properties/foo",
"https://www.sourcemeta.com/schema", "/properties/foo",
"https://www.sourcemeta.com/schema", "/properties/foo", 0);
Expand Down Expand Up @@ -203,7 +211,7 @@ TEST(JSONSchema_frame_draft0, one_level_applicators_with_identifiers) {
frame, "https://www.sourcemeta.com/test/qux#/$schema",
"https://www.sourcemeta.com/test/qux", "/$schema",
"https://www.sourcemeta.com/test/qux", "/$schema", 0);
EXPECT_FRAME_STATIC_DRAFT0_POINTER(
EXPECT_FRAME_STATIC_DRAFT0_SUBSCHEMA(
frame, "https://www.sourcemeta.com/test/qux#/items",
"https://www.sourcemeta.com/test/qux", "/items",
"https://www.sourcemeta.com/foo", "", 0);
Expand Down Expand Up @@ -270,7 +278,7 @@ TEST(JSONSchema_frame_draft0, subschema_absolute_identifier) {
frame, "https://www.sourcemeta.com/schema#/$schema",
"https://www.sourcemeta.com/schema", "/$schema",
"https://www.sourcemeta.com/schema", "/$schema", 0);
EXPECT_FRAME_STATIC_DRAFT0_POINTER(
EXPECT_FRAME_STATIC_DRAFT0_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/items",
"https://www.sourcemeta.com/schema", "/items",
"https://www.sourcemeta.com/foo", "", 0);
Expand Down Expand Up @@ -416,15 +424,15 @@ TEST(JSONSchema_frame_draft0, explicit_argument_id_different) {
frame, "https://www.sourcemeta.com/schema#/properties",
"https://www.sourcemeta.com/schema", "/properties",
"https://www.example.com", "/properties", 0);
EXPECT_FRAME_STATIC_DRAFT0_POINTER(
EXPECT_FRAME_STATIC_DRAFT0_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/properties/one",
"https://www.sourcemeta.com/schema", "/properties/one",
"https://www.example.com/test", "", 0);
EXPECT_FRAME_STATIC_DRAFT0_POINTER(
frame, "https://www.sourcemeta.com/schema#/properties/one/id",
"https://www.sourcemeta.com/schema", "/properties/one/id",
"https://www.example.com/test", "/id", 0);
EXPECT_FRAME_STATIC_DRAFT0_POINTER(
EXPECT_FRAME_STATIC_DRAFT0_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/properties/two",
"https://www.sourcemeta.com/schema", "/properties/two",
"https://www.test.com", "", 0);
Expand Down Expand Up @@ -464,7 +472,7 @@ TEST(JSONSchema_frame_draft0, ref_metaschema) {

EXPECT_EQ(frame.size(), 3);

EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
EXPECT_ANONYMOUS_FRAME_STATIC_SUBSCHEMA(
frame, "", "", "http://json-schema.org/draft-00/schema#", 0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
frame, "#/$schema", "/$schema", "http://json-schema.org/draft-00/schema#",
Expand Down
26 changes: 17 additions & 9 deletions test/jsonschema/jsonschema_frame_draft1_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
expected_base, expected_relative_pointer, \
expected_destination_of_size);

#define EXPECT_FRAME_STATIC_DRAFT1_SUBSCHEMA( \
frame, reference, root_id, expected_pointer, expected_base, \
expected_relative_pointer, expected_destination_of_size) \
EXPECT_FRAME_STATIC_SUBSCHEMA(frame, reference, root_id, expected_pointer, \
"http://json-schema.org/draft-01/schema#", \
expected_base, expected_relative_pointer, \
expected_destination_of_size);

TEST(JSONSchema_frame_draft1, anonymous_with_nested_schema_resource) {
const sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
Expand All @@ -46,12 +54,12 @@ TEST(JSONSchema_frame_draft1, anonymous_with_nested_schema_resource) {
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
frame, "https://example.com#/id", "/additionalProperties/id",
"http://json-schema.org/draft-01/schema#", 0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
EXPECT_ANONYMOUS_FRAME_STATIC_SUBSCHEMA(
frame, "", "", "http://json-schema.org/draft-01/schema#", 0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
frame, "#/$schema", "/$schema", "http://json-schema.org/draft-01/schema#",
0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
EXPECT_ANONYMOUS_FRAME_STATIC_SUBSCHEMA(
frame, "#/additionalProperties", "/additionalProperties",
"http://json-schema.org/draft-01/schema#", 0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
Expand Down Expand Up @@ -139,7 +147,7 @@ TEST(JSONSchema_frame_draft1, one_level_applicators_without_identifiers) {
frame, "https://www.sourcemeta.com/schema#/$schema",
"https://www.sourcemeta.com/schema", "/$schema",
"https://www.sourcemeta.com/schema", "/$schema", 0);
EXPECT_FRAME_STATIC_DRAFT1_POINTER(
EXPECT_FRAME_STATIC_DRAFT1_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/items",
"https://www.sourcemeta.com/schema", "/items",
"https://www.sourcemeta.com/schema", "/items", 0);
Expand All @@ -151,7 +159,7 @@ TEST(JSONSchema_frame_draft1, one_level_applicators_without_identifiers) {
frame, "https://www.sourcemeta.com/schema#/properties",
"https://www.sourcemeta.com/schema", "/properties",
"https://www.sourcemeta.com/schema", "/properties", 0);
EXPECT_FRAME_STATIC_DRAFT1_POINTER(
EXPECT_FRAME_STATIC_DRAFT1_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/properties/foo",
"https://www.sourcemeta.com/schema", "/properties/foo",
"https://www.sourcemeta.com/schema", "/properties/foo", 0);
Expand Down Expand Up @@ -203,7 +211,7 @@ TEST(JSONSchema_frame_draft1, one_level_applicators_with_identifiers) {
frame, "https://www.sourcemeta.com/test/qux#/$schema",
"https://www.sourcemeta.com/test/qux", "/$schema",
"https://www.sourcemeta.com/test/qux", "/$schema", 0);
EXPECT_FRAME_STATIC_DRAFT1_POINTER(
EXPECT_FRAME_STATIC_DRAFT1_SUBSCHEMA(
frame, "https://www.sourcemeta.com/test/qux#/items",
"https://www.sourcemeta.com/test/qux", "/items",
"https://www.sourcemeta.com/foo", "", 0);
Expand Down Expand Up @@ -270,7 +278,7 @@ TEST(JSONSchema_frame_draft1, subschema_absolute_identifier) {
frame, "https://www.sourcemeta.com/schema#/$schema",
"https://www.sourcemeta.com/schema", "/$schema",
"https://www.sourcemeta.com/schema", "/$schema", 0);
EXPECT_FRAME_STATIC_DRAFT1_POINTER(
EXPECT_FRAME_STATIC_DRAFT1_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/items",
"https://www.sourcemeta.com/schema", "/items",
"https://www.sourcemeta.com/foo", "", 0);
Expand Down Expand Up @@ -416,15 +424,15 @@ TEST(JSONSchema_frame_draft1, explicit_argument_id_different) {
frame, "https://www.sourcemeta.com/schema#/properties",
"https://www.sourcemeta.com/schema", "/properties",
"https://www.example.com", "/properties", 0);
EXPECT_FRAME_STATIC_DRAFT1_POINTER(
EXPECT_FRAME_STATIC_DRAFT1_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/properties/one",
"https://www.sourcemeta.com/schema", "/properties/one",
"https://www.example.com/test", "", 0);
EXPECT_FRAME_STATIC_DRAFT1_POINTER(
frame, "https://www.sourcemeta.com/schema#/properties/one/id",
"https://www.sourcemeta.com/schema", "/properties/one/id",
"https://www.example.com/test", "/id", 0);
EXPECT_FRAME_STATIC_DRAFT1_POINTER(
EXPECT_FRAME_STATIC_DRAFT1_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/properties/two",
"https://www.sourcemeta.com/schema", "/properties/two",
"https://www.test.com", "", 0);
Expand Down Expand Up @@ -464,7 +472,7 @@ TEST(JSONSchema_frame_draft1, ref_metaschema) {

EXPECT_EQ(frame.size(), 3);

EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
EXPECT_ANONYMOUS_FRAME_STATIC_SUBSCHEMA(
frame, "", "", "http://json-schema.org/draft-01/schema#", 0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
frame, "#/$schema", "/$schema", "http://json-schema.org/draft-01/schema#",
Expand Down
26 changes: 17 additions & 9 deletions test/jsonschema/jsonschema_frame_draft2_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
expected_base, expected_relative_pointer, \
expected_destination_of_size);

#define EXPECT_FRAME_STATIC_DRAFT2_SUBSCHEMA( \
frame, reference, root_id, expected_pointer, expected_base, \
expected_relative_pointer, expected_destination_of_size) \
EXPECT_FRAME_STATIC_SUBSCHEMA(frame, reference, root_id, expected_pointer, \
"http://json-schema.org/draft-02/schema#", \
expected_base, expected_relative_pointer, \
expected_destination_of_size);

TEST(JSONSchema_frame_draft2, anonymous_with_nested_schema_resource) {
const sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
Expand All @@ -46,12 +54,12 @@ TEST(JSONSchema_frame_draft2, anonymous_with_nested_schema_resource) {
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
frame, "https://example.com#/id", "/additionalProperties/id",
"http://json-schema.org/draft-02/schema#", 0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
EXPECT_ANONYMOUS_FRAME_STATIC_SUBSCHEMA(
frame, "", "", "http://json-schema.org/draft-02/schema#", 0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
frame, "#/$schema", "/$schema", "http://json-schema.org/draft-02/schema#",
0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
EXPECT_ANONYMOUS_FRAME_STATIC_SUBSCHEMA(
frame, "#/additionalProperties", "/additionalProperties",
"http://json-schema.org/draft-02/schema#", 0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
Expand Down Expand Up @@ -139,7 +147,7 @@ TEST(JSONSchema_frame_draft2, one_level_applicators_without_identifiers) {
frame, "https://www.sourcemeta.com/schema#/$schema",
"https://www.sourcemeta.com/schema", "/$schema",
"https://www.sourcemeta.com/schema", "/$schema", 0);
EXPECT_FRAME_STATIC_DRAFT2_POINTER(
EXPECT_FRAME_STATIC_DRAFT2_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/items",
"https://www.sourcemeta.com/schema", "/items",
"https://www.sourcemeta.com/schema", "/items", 0);
Expand All @@ -151,7 +159,7 @@ TEST(JSONSchema_frame_draft2, one_level_applicators_without_identifiers) {
frame, "https://www.sourcemeta.com/schema#/properties",
"https://www.sourcemeta.com/schema", "/properties",
"https://www.sourcemeta.com/schema", "/properties", 0);
EXPECT_FRAME_STATIC_DRAFT2_POINTER(
EXPECT_FRAME_STATIC_DRAFT2_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/properties/foo",
"https://www.sourcemeta.com/schema", "/properties/foo",
"https://www.sourcemeta.com/schema", "/properties/foo", 0);
Expand Down Expand Up @@ -203,7 +211,7 @@ TEST(JSONSchema_frame_draft2, one_level_applicators_with_identifiers) {
frame, "https://www.sourcemeta.com/test/qux#/$schema",
"https://www.sourcemeta.com/test/qux", "/$schema",
"https://www.sourcemeta.com/test/qux", "/$schema", 0);
EXPECT_FRAME_STATIC_DRAFT2_POINTER(
EXPECT_FRAME_STATIC_DRAFT2_SUBSCHEMA(
frame, "https://www.sourcemeta.com/test/qux#/items",
"https://www.sourcemeta.com/test/qux", "/items",
"https://www.sourcemeta.com/foo", "", 0);
Expand Down Expand Up @@ -270,7 +278,7 @@ TEST(JSONSchema_frame_draft2, subschema_absolute_identifier) {
frame, "https://www.sourcemeta.com/schema#/$schema",
"https://www.sourcemeta.com/schema", "/$schema",
"https://www.sourcemeta.com/schema", "/$schema", 0);
EXPECT_FRAME_STATIC_DRAFT2_POINTER(
EXPECT_FRAME_STATIC_DRAFT2_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/items",
"https://www.sourcemeta.com/schema", "/items",
"https://www.sourcemeta.com/foo", "", 0);
Expand Down Expand Up @@ -416,15 +424,15 @@ TEST(JSONSchema_frame_draft2, explicit_argument_id_different) {
frame, "https://www.sourcemeta.com/schema#/properties",
"https://www.sourcemeta.com/schema", "/properties",
"https://www.example.com", "/properties", 0);
EXPECT_FRAME_STATIC_DRAFT2_POINTER(
EXPECT_FRAME_STATIC_DRAFT2_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/properties/one",
"https://www.sourcemeta.com/schema", "/properties/one",
"https://www.example.com/test", "", 0);
EXPECT_FRAME_STATIC_DRAFT2_POINTER(
frame, "https://www.sourcemeta.com/schema#/properties/one/id",
"https://www.sourcemeta.com/schema", "/properties/one/id",
"https://www.example.com/test", "/id", 0);
EXPECT_FRAME_STATIC_DRAFT2_POINTER(
EXPECT_FRAME_STATIC_DRAFT2_SUBSCHEMA(
frame, "https://www.sourcemeta.com/schema#/properties/two",
"https://www.sourcemeta.com/schema", "/properties/two",
"https://www.test.com", "", 0);
Expand Down Expand Up @@ -464,7 +472,7 @@ TEST(JSONSchema_frame_draft2, ref_metaschema) {

EXPECT_EQ(frame.size(), 3);

EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
EXPECT_ANONYMOUS_FRAME_STATIC_SUBSCHEMA(
frame, "", "", "http://json-schema.org/draft-02/schema#", 0);
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
frame, "#/$schema", "/$schema", "http://json-schema.org/draft-02/schema#",
Expand Down
Loading

0 comments on commit 60323bc

Please sign in to comment.