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

Removing a key from HttpHeaders' key set leaves it in an inconsistent state #23633

Closed
wilkinsona opened this issue Sep 12, 2019 · 7 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue type: regression A bug that is also a regression

Comments

@wilkinsona
Copy link
Member

Affects: 5.1.10 snapshots

The problem is illustrated by the following test:

package example;

import java.util.Arrays;

import org.junit.Test;

import org.springframework.http.HttpHeaders;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

public class HttpHeadersTests {

	@Test
	public void removeFromKeySet() {
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.set("Alpha", "apple");
		httpHeaders.set("Bravo", "banana");
		httpHeaders.keySet().remove("Alpha");
		assertThat(httpHeaders).containsOnly(entry("Bravo", Arrays.asList("banana")));
		assertThat(httpHeaders).doesNotContainKey("Alpha");
	}

}

The first assertion passes but the second fails:

java.lang.AssertionError: 
Expecting:
  <{"Bravo"=["banana"]}>
not to contain key:
  <"Alpha">
	at example.HttpHeadersTests.removeFromKeySet(HttpHeadersTests.java:21)
@wilkinsona wilkinsona changed the title Removing a key from HttpHeader's key set leaves it in an inconsistent state Removing a key from HttpHeaders' key set leaves it in an inconsistent state Sep 12, 2019
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 12, 2019
@sbrannen
Copy link
Member

Interestingly, the provided test case passes on master.

We'll look into it!

@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Sep 12, 2019
@sbrannen sbrannen self-assigned this Sep 13, 2019
@sbrannen sbrannen added this to the 5.1.10 milestone Sep 13, 2019
@sbrannen sbrannen added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 13, 2019
@sbrannen
Copy link
Member

I have confirmed that the following test fails when added to org.springframework.http.HttpHeadersTests in 5.1.x.

@Test
public void removeFromKeySet() {
	headers.add("Alpha", "apple");
	headers.add("Bravo", "banana");
	assertEquals(2, headers.size());

	headers.keySet().remove("Alpha");
	assertEquals(1, headers.size());
	assertEquals(Collections.singletonMap("Bravo", Arrays.asList("banana")).entrySet(), headers.entrySet());

	// Fails!
	assertFalse("Alpha should have been removed", headers.containsKey("Alpha"));
}

@sbrannen
Copy link
Member

It turns out that the underlying cause is a bug in the anonymous inner LinkedHashMap subclass in org.springframework.util.LinkedCaseInsensitiveMap<V>. Specifically, that anonymous subclass must override keySet() with a custom implementation. I've got a prototype working locally and will push changes once I've tested more thoroughly.

@sbrannen
Copy link
Member

My local fix works, but it turns out that @philwebb already addressed this issue plus additional related issues in #22821.

I am therefore closing this as a duplicate of #22821. Rationale provided by @jhoeller in #22821 (comment).

@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) status: duplicate A duplicate of another issue labels Sep 14, 2019
@sbrannen sbrannen removed this from the 5.1.10 milestone Sep 14, 2019
@wilkinsona
Copy link
Member Author

I think it's worth noting that this is a regression. The supplied test works in 5.1.0 but fails with 5.1.1 and later.

@sbrannen
Copy link
Member

sbrannen commented Sep 15, 2019

I think it's worth noting that this is a regression. The supplied test works in 5.1.0 but fails with 5.1.1 and later.

That's a very valid point. I'll raise that with the team.

@sbrannen sbrannen added type: regression A bug that is also a regression and removed type: bug A general bug labels Sep 15, 2019
@sbrannen
Copy link
Member

Update: #22821 will backported to 5.1.10 in #23644.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

3 participants