Skip to content

Commit

Permalink
[WIP] Support relative base URIs
Browse files Browse the repository at this point in the history
Fixes: #960
See: sourcemeta/jsonschema#185
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Nov 4, 2024
1 parent 3b644bf commit caaccc3
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/uri/uri.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ static auto canonicalize_path(const std::string &path, const bool is_relative)

// Reconstruct the canonical path
std::string canonical_path;
std::string separator = is_relative ? "/" : "";
std::string separator = (is_relative && !has_leading_with_word) ? "/" : "";

for (const auto &seg : segments) {
canonical_path += separator + seg;
separator = "/";
Expand Down
50 changes: 50 additions & 0 deletions test/jsonschema/jsonschema_bundle_draft4_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,53 @@ TEST(JSONSchema_bundle_draft4, metaschema_without_id) {

EXPECT_EQ(document, expected);
}

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

sourcemeta::jsontoolkit::bundle(
document, sourcemeta::jsontoolkit::default_schema_walker, test_resolver);

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "foo"
})JSON");

EXPECT_EQ(document, expected);
}

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

sourcemeta::jsontoolkit::bundle(
document, sourcemeta::jsontoolkit::default_schema_walker, test_resolver);

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

EXPECT_EQ(document, expected);
}
34 changes: 34 additions & 0 deletions test/jsonschema/jsonschema_frame_draft4_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,37 @@ 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({
"id": "common",
"$schema": "http://json-schema.org/draft-04/schema#"
})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, "#/id", "common", "/id", "common",
"/id");
EXPECT_FRAME_STATIC_DRAFT4_POINTER(frame, "#/$schema", "common", "/$schema",
"common", "/$schema");

// 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);
}
8 changes: 7 additions & 1 deletion test/uri/uri_canonicalize_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ TEST(URI_canonicalize, example_relative_1) {
TEST(URI_canonicalize, example_relative_2) {
sourcemeta::jsontoolkit::URI uri{"./foo"};
uri.canonicalize();
EXPECT_EQ(uri.recompose(), "/foo");
EXPECT_EQ(uri.recompose(), "foo");
}

TEST(URI_canonicalize, example_relative_3) {
sourcemeta::jsontoolkit::URI uri{"foo"};
uri.canonicalize();
EXPECT_EQ(uri.recompose(), "foo");
}

TEST(URI_canonicalize, example_12) {
Expand Down

0 comments on commit caaccc3

Please sign in to comment.