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

Correctly canonicalize relative URIs that do not begin with / #1318

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
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
4 changes: 2 additions & 2 deletions test/jsonschema/jsonschema_frame_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1709,7 +1709,7 @@ TEST(JSONSchema_frame_2019_09, recursive_anchor_on_relative_id) {
EXPECT_ANONYMOUS_FRAME_STATIC_POINTER(
frame, "", "", "https://json-schema.org/draft/2019-09/schema");
EXPECT_ANONYMOUS_FRAME_STATIC_RESOURCE(
frame, "/middle", "/additionalItems",
frame, "middle", "/additionalItems",
"https://json-schema.org/draft/2019-09/schema");

// JSON Pointers
Expand Down Expand Up @@ -1739,7 +1739,7 @@ TEST(JSONSchema_frame_2019_09, recursive_anchor_on_relative_id) {
EXPECT_ANONYMOUS_FRAME_DYNAMIC_ANCHOR(
frame, "", "", "https://json-schema.org/draft/2019-09/schema");
EXPECT_ANONYMOUS_FRAME_DYNAMIC_ANCHOR(
frame, "/middle", "/additionalItems",
frame, "middle", "/additionalItems",
"https://json-schema.org/draft/2019-09/schema");

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

TEST(URI_canonicalize, example_12) {
Expand Down