-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
HttpHeaders constructor should not be public or should not mutate original HttpHeaders #28336
Comments
Hi @ryoheinagao, Thanks for opening your first issue for the Spring Framework. What is your concrete use case for creating an instance of |
@sbrannen Thank you for your comment. For example, most of header's value are constant but some value(secret token or user's location info) are vary in each request. And constant pairs are common among multiple implementation classes or methods. In this case, I want to create an instance from constant header, and then I want to add these temporary key-value. The pseudo code is below. In the case of sharing key-value between implementation class or there are many constant value, I think instantiation from existing import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
public class SomeRepository {
private static final HttpHeaders STATIC_HEADER = new HttpHeaders();
static {
STATIC_HEADER.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
// please assume this header has many key-value pairs and is shared among methods or classes
}
String findSomePrivateValue(String userToken,){
var header = new HttpHeaders(STATIC_HEADER);
header.add("X-Secret", userToken);
// rest operations etc...
}
String findUserAround(String userLocation){
var header = new HttpHeaders(STATIC_HEADER);
header.add("X-Location", userLocation);
// rest operations etc...
}
} |
What do you think my suggestion? |
Thanks for the suggestion, but the behavior of that constructor is well documented and well defined. Unlike collection-related constructors (like I'm declining this issue as a result. |
Problem
spring-framework/spring-web/src/main/java/org/springframework/http/HttpHeaders.java
Lines 426 to 444 in c28fd91
Suggetion
So I suggest this confusional constructor is package-private not to be accessed from external the library or support immutableness of its hashmap field.
The text was updated successfully, but these errors were encountered: