Copy cookies in RenderingResponse.from()

Prior to this commit, RenderingResponse.from() did not copy the
cookies of the given response, unlike ServerResponse.from().

See gh-22481
Closes gh-37378

Signed-off-by: Hyunwoo Jung <hyunwoojung@kakao.com>
This commit is contained in:
Hyunwoo Jung
2026-10-03 14:14:09 +02:00
committed by GitHub
parent d5a7fc9a9c
commit f03b76a53d
6 changed files with 49 additions and 2 deletions
@@ -71,6 +71,7 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder
this.status = other.statusCode();
this.headers.putAll(other.headers());
this.model.putAll(other.model());
this.cookies.addAll(other.cookies());
}
public DefaultRenderingResponseBuilder(String name) {
@@ -51,7 +51,8 @@ public interface RenderingResponse extends ServerResponse {
// Builder
/**
* Create a builder with the template name, status code, headers and model of the given response.
* Create a builder with the template name, status code, headers, cookies,
* and model of the given response.
* @param other the response to copy the values from
* @return the created builder
*/
@@ -273,5 +273,26 @@ class DefaultRenderingResponseTests {
.verify();
}
@Test // gh-37378
void from() {
ResponseCookie cookie = ResponseCookie.from("currency", "USD").build();
RenderingResponse other = RenderingResponse.create("foo")
.status(HttpStatus.CREATED)
.header("foo", "bar")
.cookie(cookie)
.modelAttribute("model", "value")
.build().block();
Mono<RenderingResponse> result = RenderingResponse.from(other).build();
StepVerifier.create(result)
.assertNext(response -> {
assertThat(response.name()).isEqualTo("foo");
assertThat(response.statusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(response.headers().getFirst("foo")).isEqualTo("bar");
assertThat(response.cookies()).containsEntry("currency", List.of(cookie));
assertThat(response.model()).containsEntry("model", "value");
})
.verifyComplete();
}
}
@@ -62,6 +62,7 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder
this.status = other.statusCode();
this.headers.putAll(other.headers());
this.model.putAll(other.model());
this.cookies.addAll(other.cookies());
}
public DefaultRenderingResponseBuilder(String name) {
@@ -49,7 +49,8 @@ public interface RenderingResponse extends ServerResponse {
// Builder
/**
* Create a builder with the template name, status code, headers and model of the given response.
* Create a builder with the template name, status code, headers, cookies,
* and model of the given response.
* @param other the response to copy the values from
* @return the created builder
*/
@@ -179,5 +179,27 @@ class DefaultRenderingResponseTests {
assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_MODIFIED.value());
}
@Test // gh-37378
void from() throws Exception {
Cookie cookie = new Cookie("currency", "USD");
RenderingResponse other = RenderingResponse.create("foo")
.status(HttpStatus.CREATED)
.header("foo", "bar")
.cookie(cookie)
.modelAttribute("model", "value")
.build();
RenderingResponse result = RenderingResponse.from(other).build();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mav = result.writeTo(request, response, EMPTY_CONTEXT);
assertThat(mav).isNotNull();
assertThat(mav.getViewName()).isEqualTo("foo");
assertThat(mav.getModel()).containsEntry("model", "value");
assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value());
assertThat(response.getHeader("foo")).isEqualTo("bar");
assertThat(response.getCookies()).containsExactly(cookie);
}
}