Retain entry set order in read-only HttpHeaders

Prior to this commit, the entry set of read-only HttpHeaders lost the
original headers' ordering.

The changes in commit ce7278aaf4 introduced a regression in the read-only
HttpHeaders support. Specifically, the implementation of entrySet() in
the internal ReadOnlyHttpHeaders class converted the original entry set
to an immutable, non-ordered set of immutable entries.

This commit fixes this issue by converting the original entry set to an
immutable, ordered set of immutable entries.

Closes gh-23551
This commit is contained in:
Sam Brannen
2019-08-31 12:26:06 +02:00
parent a496353770
commit 9729b460f1
2 changed files with 25 additions and 4 deletions

View File

@@ -16,9 +16,10 @@
package org.springframework.http;
import java.util.AbstractMap;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -31,6 +32,7 @@ import org.springframework.util.MultiValueMap;
* {@code HttpHeaders} object that can only be read, not written to.
*
* @author Brian Clozel
* @author Sam Brannen
* @since 5.1.1
*/
class ReadOnlyHttpHeaders extends HttpHeaders {
@@ -141,9 +143,10 @@ class ReadOnlyHttpHeaders extends HttpHeaders {
@Override
public Set<Entry<String, List<String>>> entrySet() {
return Collections.unmodifiableSet(this.headers.entrySet().stream()
.map(AbstractMap.SimpleImmutableEntry::new)
.collect(Collectors.toSet()));
return this.headers.entrySet().stream().map(SimpleImmutableEntry::new)
.collect(Collectors.collectingAndThen(
Collectors.toCollection(LinkedHashSet::new), // Retain original ordering of entries
Collections::unmodifiableSet));
}
}