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

Test framing with relative base identifiers #1321

Merged
merged 1 commit into from
Nov 4, 2024
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
94 changes: 94 additions & 0 deletions test/jsonschema/jsonschema_frame_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1893,3 +1893,97 @@ TEST(JSONSchema_frame_2019_09, ref_from_definitions) {
"https://www.sourcemeta.com/schema#/definitions/string",
"https://www.sourcemeta.com/schema", "/definitions/string");
}

TEST(JSONSchema_frame_2019_09, relative_base_uri_without_ref) {
const sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "common"
})JSON");

sourcemeta::jsontoolkit::ReferenceFrame frame;
sourcemeta::jsontoolkit::ReferenceMap references;
sourcemeta::jsontoolkit::frame(document, frame, references,
sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver);

EXPECT_EQ(frame.size(), 3);

EXPECT_FRAME_STATIC_2019_09_RESOURCE(frame, "common", "common", "", "common",
"");

// JSON Pointers

EXPECT_FRAME_STATIC_2019_09_POINTER(frame, "common#/$schema", "common",
"/$schema", "common", "/$schema");
EXPECT_FRAME_STATIC_2019_09_POINTER(frame, "common#/$id", "common", "/$id",
"common", "/$id");

// References

EXPECT_EQ(references.size(), 1);

EXPECT_STATIC_REFERENCE(
references, "/$schema", "https://json-schema.org/draft/2019-09/schema",
"https://json-schema.org/draft/2019-09/schema", std::nullopt);
}

TEST(JSONSchema_frame_2019_09, relative_base_uri_with_ref) {
const sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "common",
"allOf": [ { "$ref": "#foo" } ],
"$defs": {
"foo": {
"$anchor": "foo"
}
}
})JSON");

sourcemeta::jsontoolkit::ReferenceFrame frame;
sourcemeta::jsontoolkit::ReferenceMap references;
sourcemeta::jsontoolkit::frame(document, frame, references,
sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver);

EXPECT_EQ(frame.size(), 10);

EXPECT_FRAME_STATIC_2019_09_RESOURCE(frame, "common", "common", "", "common",
"");

// Anchors
EXPECT_FRAME_STATIC_2019_09_ANCHOR(frame, "common#foo", "common",
"/$defs/foo", "common", "/$defs/foo");

// JSON Pointers

EXPECT_FRAME_STATIC_2019_09_POINTER(frame, "common#/$schema", "common",
"/$schema", "common", "/$schema");
EXPECT_FRAME_STATIC_2019_09_POINTER(frame, "common#/$id", "common", "/$id",
"common", "/$id");
EXPECT_FRAME_STATIC_2019_09_POINTER(frame, "common#/allOf", "common",
"/allOf", "common", "/allOf");
EXPECT_FRAME_STATIC_2019_09_POINTER(frame, "common#/allOf/0", "common",
"/allOf/0", "common", "/allOf/0");
EXPECT_FRAME_STATIC_2019_09_POINTER(frame, "common#/allOf/0/$ref", "common",
"/allOf/0/$ref", "common",
"/allOf/0/$ref");
EXPECT_FRAME_STATIC_2019_09_POINTER(frame, "common#/$defs", "common",
"/$defs", "common", "/$defs");
EXPECT_FRAME_STATIC_2019_09_POINTER(frame, "common#/$defs/foo", "common",
"/$defs/foo", "common", "/$defs/foo");
EXPECT_FRAME_STATIC_2019_09_POINTER(frame, "common#/$defs/foo/$anchor",
"common", "/$defs/foo/$anchor", "common",
"/$defs/foo/$anchor");

// References

EXPECT_EQ(references.size(), 2);

EXPECT_STATIC_REFERENCE(
references, "/$schema", "https://json-schema.org/draft/2019-09/schema",
"https://json-schema.org/draft/2019-09/schema", std::nullopt);
EXPECT_STATIC_REFERENCE(references, "/allOf/0/$ref", "common#foo", "common",
"foo");
}
94 changes: 94 additions & 0 deletions test/jsonschema/jsonschema_frame_2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1445,3 +1445,97 @@ TEST(JSONSchema_frame_2020_12, ref_from_definitions) {
"https://www.sourcemeta.com/schema#/definitions/string",
"https://www.sourcemeta.com/schema", "/definitions/string");
}

TEST(JSONSchema_frame_2020_12, relative_base_uri_without_ref) {
const sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "common"
})JSON");

sourcemeta::jsontoolkit::ReferenceFrame frame;
sourcemeta::jsontoolkit::ReferenceMap references;
sourcemeta::jsontoolkit::frame(document, frame, references,
sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver);

EXPECT_EQ(frame.size(), 3);

EXPECT_FRAME_STATIC_2020_12_RESOURCE(frame, "common", "common", "", "common",
"");

// JSON Pointers

EXPECT_FRAME_STATIC_2020_12_POINTER(frame, "common#/$schema", "common",
"/$schema", "common", "/$schema");
EXPECT_FRAME_STATIC_2020_12_POINTER(frame, "common#/$id", "common", "/$id",
"common", "/$id");

// References

EXPECT_EQ(references.size(), 1);

EXPECT_STATIC_REFERENCE(
references, "/$schema", "https://json-schema.org/draft/2020-12/schema",
"https://json-schema.org/draft/2020-12/schema", std::nullopt);
}

TEST(JSONSchema_frame_2020_12, relative_base_uri_with_ref) {
const sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "common",
"allOf": [ { "$ref": "#foo" } ],
"$defs": {
"foo": {
"$anchor": "foo"
}
}
})JSON");

sourcemeta::jsontoolkit::ReferenceFrame frame;
sourcemeta::jsontoolkit::ReferenceMap references;
sourcemeta::jsontoolkit::frame(document, frame, references,
sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver);

EXPECT_EQ(frame.size(), 10);

EXPECT_FRAME_STATIC_2020_12_RESOURCE(frame, "common", "common", "", "common",
"");

// Anchors
EXPECT_FRAME_STATIC_2020_12_ANCHOR(frame, "common#foo", "common",
"/$defs/foo", "common", "/$defs/foo");

// JSON Pointers

EXPECT_FRAME_STATIC_2020_12_POINTER(frame, "common#/$schema", "common",
"/$schema", "common", "/$schema");
EXPECT_FRAME_STATIC_2020_12_POINTER(frame, "common#/$id", "common", "/$id",
"common", "/$id");
EXPECT_FRAME_STATIC_2020_12_POINTER(frame, "common#/allOf", "common",
"/allOf", "common", "/allOf");
EXPECT_FRAME_STATIC_2020_12_POINTER(frame, "common#/allOf/0", "common",
"/allOf/0", "common", "/allOf/0");
EXPECT_FRAME_STATIC_2020_12_POINTER(frame, "common#/allOf/0/$ref", "common",
"/allOf/0/$ref", "common",
"/allOf/0/$ref");
EXPECT_FRAME_STATIC_2020_12_POINTER(frame, "common#/$defs", "common",
"/$defs", "common", "/$defs");
EXPECT_FRAME_STATIC_2020_12_POINTER(frame, "common#/$defs/foo", "common",
"/$defs/foo", "common", "/$defs/foo");
EXPECT_FRAME_STATIC_2020_12_POINTER(frame, "common#/$defs/foo/$anchor",
"common", "/$defs/foo/$anchor", "common",
"/$defs/foo/$anchor");

// References

EXPECT_EQ(references.size(), 2);

EXPECT_STATIC_REFERENCE(
references, "/$schema", "https://json-schema.org/draft/2020-12/schema",
"https://json-schema.org/draft/2020-12/schema", std::nullopt);
EXPECT_STATIC_REFERENCE(references, "/allOf/0/$ref", "common#foo", "common",
"foo");
}
103 changes: 103 additions & 0 deletions test/jsonschema/jsonschema_frame_draft4_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
"http://json-schema.org/draft-04/schema#", \
expected_base, expected_relative_pointer);

#define EXPECT_FRAME_STATIC_DRAFT4_ANCHOR(frame, reference, root_id, \
expected_pointer, expected_base, \
expected_relative_pointer) \
EXPECT_FRAME_STATIC_ANCHOR(frame, reference, root_id, expected_pointer, \
"http://json-schema.org/draft-04/schema#", \
expected_base, expected_relative_pointer);

TEST(JSONSchema_frame_draft4, anonymous_with_nested_schema_resource) {
const sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
Expand Down Expand Up @@ -596,3 +603,99 @@ TEST(JSONSchema_frame_draft4, ref_with_id) {
EXPECT_STATIC_REFERENCE(references, "/$ref", "#/definitions/string",
std::nullopt, "/definitions/string");
}

TEST(JSONSchema_frame_draft4, relative_base_uri_without_ref) {
const sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "common"
})JSON");

sourcemeta::jsontoolkit::ReferenceFrame frame;
sourcemeta::jsontoolkit::ReferenceMap references;
sourcemeta::jsontoolkit::frame(document, frame, references,
sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver);

EXPECT_EQ(frame.size(), 3);

EXPECT_FRAME_STATIC_DRAFT4_RESOURCE(frame, "common", "common", "", "common",
"");

// JSON Pointers

EXPECT_FRAME_STATIC_DRAFT4_POINTER(frame, "common#/$schema", "common",
"/$schema", "common", "/$schema");
EXPECT_FRAME_STATIC_DRAFT4_POINTER(frame, "common#/id", "common", "/id",
"common", "/id");

// References

EXPECT_EQ(references.size(), 1);

EXPECT_STATIC_REFERENCE(
references, "/$schema", "http://json-schema.org/draft-04/schema",
"http://json-schema.org/draft-04/schema", std::nullopt);
}

TEST(JSONSchema_frame_draft4, relative_base_uri_with_ref) {
const sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "common",
"allOf": [ { "$ref": "#foo" } ],
"definitions": {
"foo": {
"id": "#foo"
}
}
})JSON");

sourcemeta::jsontoolkit::ReferenceFrame frame;
sourcemeta::jsontoolkit::ReferenceMap references;
sourcemeta::jsontoolkit::frame(document, frame, references,
sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver);

EXPECT_EQ(frame.size(), 10);

EXPECT_FRAME_STATIC_DRAFT4_RESOURCE(frame, "common", "common", "", "common",
"");

// Anchors
EXPECT_FRAME_STATIC_DRAFT4_ANCHOR(frame, "common#foo", "common",
"/definitions/foo", "common",
"/definitions/foo");

// JSON Pointers

EXPECT_FRAME_STATIC_DRAFT4_POINTER(frame, "common#/$schema", "common",
"/$schema", "common", "/$schema");
EXPECT_FRAME_STATIC_DRAFT4_POINTER(frame, "common#/id", "common", "/id",
"common", "/id");
EXPECT_FRAME_STATIC_DRAFT4_POINTER(frame, "common#/allOf", "common", "/allOf",
"common", "/allOf");
EXPECT_FRAME_STATIC_DRAFT4_POINTER(frame, "common#/allOf/0", "common",
"/allOf/0", "common", "/allOf/0");
EXPECT_FRAME_STATIC_DRAFT4_POINTER(frame, "common#/allOf/0/$ref", "common",
"/allOf/0/$ref", "common",
"/allOf/0/$ref");
EXPECT_FRAME_STATIC_DRAFT4_POINTER(frame, "common#/definitions", "common",
"/definitions", "common", "/definitions");
EXPECT_FRAME_STATIC_DRAFT4_POINTER(frame, "common#/definitions/foo", "common",
"/definitions/foo", "common",
"/definitions/foo");
EXPECT_FRAME_STATIC_DRAFT4_POINTER(frame, "common#/definitions/foo/id",
"common", "/definitions/foo/id", "common",
"/definitions/foo/id");

// References

EXPECT_EQ(references.size(), 2);

EXPECT_STATIC_REFERENCE(
references, "/$schema", "http://json-schema.org/draft-04/schema",
"http://json-schema.org/draft-04/schema", std::nullopt);
EXPECT_STATIC_REFERENCE(references, "/allOf/0/$ref", "common#foo", "common",
"foo");
}
Loading