Skip to content

Commit 59c2f4c

Browse files
committed
Allow null for ResponseEntity.HeadersBuilder::eTag
Closes gh-28947
1 parent ef178d2 commit 59c2f4c

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

spring-web/src/main/java/org/springframework/http/ResponseEntity.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public interface HeadersBuilder<B extends HeadersBuilder<B>> {
404404
* @return this builder
405405
* @see HttpHeaders#setETag(String)
406406
*/
407-
B eTag(String etag);
407+
B eTag(@Nullable String etag);
408408

409409
/**
410410
* Set the time the resource was last changed, as specified by the
@@ -562,12 +562,14 @@ public BodyBuilder contentType(MediaType contentType) {
562562
}
563563

564564
@Override
565-
public BodyBuilder eTag(String etag) {
566-
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
567-
etag = "\"" + etag;
568-
}
569-
if (!etag.endsWith("\"")) {
570-
etag = etag + "\"";
565+
public BodyBuilder eTag(@Nullable String etag) {
566+
if (etag != null) {
567+
if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) {
568+
etag = "\"" + etag;
569+
}
570+
if (!etag.endsWith("\"")) {
571+
etag = etag + "\"";
572+
}
571573
}
572574
this.headers.setETag(etag);
573575
return this;

spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ void Etagheader() throws URISyntaxException {
207207

208208
responseEntity = ResponseEntity.ok().eTag("W/\"foo\"").build();
209209
assertThat(responseEntity.getHeaders().getETag()).isEqualTo("W/\"foo\"");
210+
211+
responseEntity = ResponseEntity.ok().eTag(null).build();
212+
assertThat(responseEntity.getHeaders().getETag()).isNull();
210213
}
211214

212215
@Test

0 commit comments

Comments
 (0)