spring-framework
spring-framework copied to clipboard
Allow null argument for `ResponseEntity.DefaultBuilder.eTag(String etag)`
Affects: v5.3.22
Description
Building a response with etags can result in a NullPointerException
if the argument for etag
is null
. Surprisingly, HttpHeaders.setETag(@Nullable String etag)
handles null
arguments and is called in ResponseEntity.DefaultBuilder.eTag(String etag)
.
Sample Code
var response = ResponseEntity
.ok()
.eTag(null)
.body(body);
Effected Code https://github.com/spring-projects/spring-framework/blob/b72ee5f0345efd281716ce95b63bfb492df1ddbd/spring-web/src/main/java/org/springframework/http/ResponseEntity.java#L565-L574
https://github.com/spring-projects/spring-framework/blob/b72ee5f0345efd281716ce95b63bfb492df1ddbd/spring-web/src/main/java/org/springframework/http/HttpHeaders.java#L1042-L1052
Solution
- Allow
null
strings - remove etag for
null
strings (likeHttpHeaders.setETag()
already does)
Therefore it should be safe just to call HttpHeaders.setETag()
for null
values:
public BodyBuilder eTag(String etag) {
if (etag != null && !etag.startsWith("\"") && !etag.startsWith("W/\"")) {
etag = "\"" + etag;
}
if (etag != null && !etag.endsWith("\"")) {
etag = etag + "\"";
}
this.headers.setETag(etag);
return this;
}