Skip to content

fix(href): copy values before appending in with() - #1842

Open
Inf1mum wants to merge 1 commit into
yegor256:masterfrom
Inf1mum:fix/1834-href-with-immutability
Open

Inf1mum wants to merge 1 commit into
yegor256:masterfrom
Inf1mum:fix/1834-href-with-immutability

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

What

Href.with() no longer mutates the value list of the original Href. It copies the list of the given key into a fresh ArrayList, appends the value to the copy, and puts the copy back into the params map. Regression tests were added to HrefTest.

Why

When the key already existed, the old implementation called map.get(key).add(...) on the list shared with the original object, so the query of the original changed after with() — a violation of the immutability contract of the class. See #1834:

final Href original = new Href("/search?tag=one");
final Href extended = original.with("tag", "two");
original.toString(); // was: "/search?tag=one&tag=two"
                     // now: "/search?tag=one"

How

The containsKey/put/get().add() sequence in Href.with() is replaced with a single getOrDefault(..., Collections.emptyList()) wrapped into a new ArrayList, followed by values.add(...) and map.put(...). Other value lists stay untouched.

Four tests added: the original stays unchanged when the key exists or is fresh, the returned Href contains both values for an existing key, and the new parameter for a fresh one.

Full gate passed locally: mvn clean install -Pqulice.

DCO: Signed-off-by: Inf1mum vadimlazuk5@gmail.com

What: Href.with() now appends the new value to a copy of the key's
value list, not to the list owned by the original Href.

Why: the previous implementation added the value straight into the
shared list, silently changing the query of the original Href when
the key already existed (yegor256#1834), which violated the immutability
contract of the class.

How: we fetch the existing values with getOrDefault() into a fresh
ArrayList, add the value to it, and put the copied list back into
the map before building the new Href.

Signed-off-by: Inf1mum <vadimlazuk5@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