Skip to content

Commit

Permalink
Test framing with relative base identifiers (#1321)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Nov 4, 2024
1 parent 688abb0 commit 49e5bf6
Show file tree
Hide file tree
Showing 5 changed files with 497 additions and 0 deletions.
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

4 comments on commit 49e5bf6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (macos/llvm)

Benchmark suite Current: 49e5bf6 Previous: 688abb0 Ratio
JSON_Array_Of_Objects_Unique 4799.2133962654725 ns/iter 4029.1647011253103 ns/iter 1.19

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (linux/llvm)

Benchmark suite Current: 49e5bf6 Previous: 688abb0 Ratio
JSON_Array_Of_Objects_Unique 2120.1592437369236 ns/iter 2109.325732947923 ns/iter 1.01

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (linux/gcc)

Benchmark suite Current: 49e5bf6 Previous: 688abb0 Ratio
JSON_Array_Of_Objects_Unique 3025.0932040988773 ns/iter 3227.411917567828 ns/iter 0.94

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark (windows/msvc)

Benchmark suite Current: 49e5bf6 Previous: 688abb0 Ratio
JSON_Array_Of_Objects_Unique 4502.117500000224 ns/iter 4335.509999999943 ns/iter 1.04

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.