Skip to content

Commit 73e8021

Browse files
committed
ResponseEntity's headers(HttpHeaders) accepts null value
Issue: SPR-12792
1 parent 5ba7b89 commit 73e8021

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ public BodyBuilder header(String headerName, String... headerValues) {
381381

382382
@Override
383383
public BodyBuilder headers(HttpHeaders headers) {
384-
this.headers.putAll(headers);
384+
if (headers != null) {
385+
this.headers.putAll(headers);
386+
}
385387
return this;
386388
}
387389

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
/**
2828
* @author Arjen Poutsma
2929
* @author Marcel Overdijk
30+
* @author Kazuki Shimizu
3031
*/
3132
public class ResponseEntityTests {
3233

@@ -161,4 +162,31 @@ public void headers() throws URISyntaxException {
161162
assertNull(responseEntity.getBody());
162163
}
163164

165+
@Test
166+
public void headersCopy(){
167+
HttpHeaders customHeaders = new HttpHeaders();
168+
customHeaders.set("X-CustomHeader", "vale");
169+
170+
ResponseEntity<Void> responseEntity = ResponseEntity.ok().headers(customHeaders).build();
171+
HttpHeaders responseHeaders = responseEntity.getHeaders();
172+
173+
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
174+
assertEquals(1, responseHeaders.size());
175+
assertEquals(1, responseHeaders.get("X-CustomHeader").size());
176+
assertEquals("vale", responseHeaders.getFirst("X-CustomHeader"));
177+
178+
}
179+
180+
@Test // SPR-12792
181+
public void headersCopyWithEmptyAndNull(){
182+
ResponseEntity<Void> responseEntityWithEmptyHeaders =
183+
ResponseEntity.ok().headers(new HttpHeaders()).build();
184+
ResponseEntity<Void> responseEntityWithNullHeaders =
185+
ResponseEntity.ok().headers(null).build();
186+
187+
assertEquals(HttpStatus.OK, responseEntityWithEmptyHeaders.getStatusCode());
188+
assertTrue(responseEntityWithEmptyHeaders.getHeaders().isEmpty());
189+
assertEquals(responseEntityWithEmptyHeaders.toString(), responseEntityWithNullHeaders.toString());
190+
}
191+
164192
}

0 commit comments

Comments
 (0)