Skip to content

fix(uri): make PathAndQuery PartialEq and Hash consistent with as_str and PartialOrd - #896

Open
xgbtxy wants to merge 1 commit into
hyperium:masterfrom
xgbtxy:fix/path-and-query-eq-hash
Open

xgbtxy wants to merge 1 commit into
hyperium:masterfrom
xgbtxy:fix/path-and-query-eq-hash

Conversation

@xgbtxy

@xgbtxy xgbtxy commented Dec 2, 2026

Copy link
Copy Markdown

Summary

PathAndQuery::as_str(), PartialEq<str>, Display, and PartialOrd all normalize an empty internal buffer (self.data == "") to "/". However, PartialEq<PathAndQuery> and Hash previously compared and hashed self.data directly.

Because an empty PathAndQuery can be constructed via Uri::builder().path_and_query("") or fragment-only inputs like "#frag", comparing it with PathAndQuery::from_static("/") violated the PartialOrd/PartialEq and Eq/Hash contracts:

  • a.as_str() == b.as_str() ("/")
  • a.partial_cmp(&b) == Some(Ordering::Equal)
  • yet a == b returned false and hash(a) != hash(b).

Changes

  • Update PartialEq for PathAndQuery and Hash for PathAndQuery in src/uri/path.rs to use self.as_str(), matching PartialEq<str> and PartialOrd.
  • Add unit regression test empty_and_slash_eq_hash_ord_consistent covering PathAndQuery::empty(), fragment-only parsing ("#frag"), and Uri::builder().path_and_query("") vs Uri::from_static("http://example.com/").

Verification

  • Verified the new regression test failed before the fix (assertion left == right failed: left: /, right: /) and passes after.
  • cargo test (150 unit tests, integration tests, and 229 doctests) and cargo fmt -- --check pass.

… and PartialOrd

PathAndQuery::as_str(), PartialEq<str>, and PartialOrd already normalize
an empty internal buffer to "/", while PartialEq<PathAndQuery> and Hash
compared and hashed self.data directly.

As a result, an empty PathAndQuery (such as from
Uri::builder().path_and_query("") or fragment-only inputs like "#frag")
compared PartialOrd::Equal to PathAndQuery::from_static("/") yet returned
false for == and produced a different Hash, violating the
PartialOrd/PartialEq and Eq/Hash contracts.

Use self.as_str() in both PartialEq and Hash implementations and add
regression tests covering empty, fragment-only, and builder-constructed
values.

Signed-off-by: Yan Chen <yan8925298@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant