Skip to content

Commit c152d24

Browse files
committed
Copy cookies and hints to ServerResponse builders
Closes gh-22351
1 parent 6324a1b commit c152d24

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ public EntityResponse.Builder<T> hint(String key, Object value) {
157157
return this;
158158
}
159159

160+
@Override
161+
public EntityResponse.Builder<T> hints(Consumer<Map<String, Object>> hintsConsumer) {
162+
hintsConsumer.accept(this.hints);
163+
return this;
164+
}
165+
160166
@Override
161167
public EntityResponse.Builder<T> lastModified(ZonedDateTime lastModified) {
162168
this.headers.setLastModified(lastModified);

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ public ServerResponse.BodyBuilder hint(String key, Object value) {
165165
return this;
166166
}
167167

168+
@Override
169+
public ServerResponse.BodyBuilder hints(Consumer<Map<String, Object>> hintsConsumer) {
170+
hintsConsumer.accept(this.hints);
171+
return this;
172+
}
173+
168174
@Override
169175
public ServerResponse.BodyBuilder lastModified(ZonedDateTime lastModified) {
170176
this.headers.setLastModified(lastModified);
@@ -222,8 +228,10 @@ public <T, P extends Publisher<T>> Mono<ServerResponse> body(P publisher, Class<
222228

223229
return new DefaultEntityResponseBuilder<>(publisher,
224230
BodyInserters.fromPublisher(publisher, elementClass))
225-
.headers(this.headers)
226231
.status(this.statusCode)
232+
.headers(this.headers)
233+
.cookies(cookies -> cookies.addAll(this.cookies))
234+
.hints(hints -> hints.putAll(this.hints))
227235
.build()
228236
.map(entityResponse -> entityResponse);
229237
}
@@ -237,8 +245,10 @@ public <T, P extends Publisher<T>> Mono<ServerResponse> body(P publisher,
237245

238246
return new DefaultEntityResponseBuilder<>(publisher,
239247
BodyInserters.fromPublisher(publisher, typeReference))
240-
.headers(this.headers)
241248
.status(this.statusCode)
249+
.headers(this.headers)
250+
.cookies(cookies -> cookies.addAll(this.cookies))
251+
.hints(hints -> hints.putAll(this.hints))
242252
.build()
243253
.map(entityResponse -> entityResponse);
244254
}
@@ -251,8 +261,10 @@ public Mono<ServerResponse> syncBody(Object body) {
251261

252262
return new DefaultEntityResponseBuilder<>(body,
253263
BodyInserters.fromObject(body))
254-
.headers(this.headers)
255264
.status(this.statusCode)
265+
.headers(this.headers)
266+
.cookies(cookies -> cookies.addAll(this.cookies))
267+
.hints(hints -> hints.putAll(this.hints))
256268
.build()
257269
.map(entityResponse -> entityResponse);
258270
}
@@ -266,8 +278,9 @@ public Mono<ServerResponse> body(BodyInserter<?, ? super ServerHttpResponse> ins
266278
@Override
267279
public Mono<ServerResponse> render(String name, Object... modelAttributes) {
268280
return new DefaultRenderingResponseBuilder(name)
269-
.headers(this.headers)
270281
.status(this.statusCode)
282+
.headers(this.headers)
283+
.cookies(cookies -> cookies.addAll(this.cookies))
271284
.modelAttributes(modelAttributes)
272285
.build()
273286
.map(renderingResponse -> renderingResponse);
@@ -276,8 +289,9 @@ public Mono<ServerResponse> render(String name, Object... modelAttributes) {
276289
@Override
277290
public Mono<ServerResponse> render(String name, Map<String, ?> model) {
278291
return new DefaultRenderingResponseBuilder(name)
279-
.headers(this.headers)
280292
.status(this.statusCode)
293+
.headers(this.headers)
294+
.cookies(cookies -> cookies.addAll(this.cookies))
281295
.modelAttributes(model)
282296
.build()
283297
.map(renderingResponse -> renderingResponse);

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/EntityResponse.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -19,6 +19,7 @@
1919
import java.net.URI;
2020
import java.time.Instant;
2121
import java.time.ZonedDateTime;
22+
import java.util.Map;
2223
import java.util.Set;
2324
import java.util.function.Consumer;
2425

@@ -262,6 +263,15 @@ interface Builder<T> {
262263
*/
263264
Builder<T> hint(String key, Object value);
264265

266+
/**
267+
* Manipulate serialization hint with the given consumer.
268+
*
269+
* @param hintsConsumer a function that consumes the hints
270+
* @return this builder
271+
* @since 5.1.6
272+
*/
273+
Builder<T> hints(Consumer<Map<String, Object>> hintsConsumer);
274+
265275
/**
266276
* Build the response.
267277
* @return the built response

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -376,6 +376,15 @@ interface BodyBuilder extends HeadersBuilder<BodyBuilder> {
376376
*/
377377
BodyBuilder hint(String key, Object value);
378378

379+
/**
380+
* Manipulate serialization hint with the given consumer.
381+
*
382+
* @param hintsConsumer a function that consumes the hints
383+
* @return this builder
384+
* @since 5.1.6
385+
*/
386+
BodyBuilder hints(Consumer<Map<String, Object>> hintsConsumer);
387+
379388
/**
380389
* Set the body of the response to the given asynchronous {@code Publisher} and return it.
381390
* This convenience method combines {@link #body(BodyInserter)} and

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.mock.web.test.server.MockServerWebExchange;
4242
import org.springframework.util.LinkedMultiValueMap;
4343
import org.springframework.util.MultiValueMap;
44+
import org.springframework.web.reactive.function.BodyInserters;
4445
import org.springframework.web.reactive.result.view.ViewResolver;
4546

4647
import static org.junit.Assert.*;
@@ -302,6 +303,23 @@ public void cookies() {
302303
.verify();
303304
}
304305

306+
@Test
307+
public void copyCookies() {
308+
Mono<ServerResponse> serverResponse = ServerResponse.ok()
309+
.cookie(ResponseCookie.from("foo", "bar").build())
310+
.syncBody("body");
311+
312+
assertFalse(serverResponse.block().cookies().isEmpty());
313+
314+
serverResponse = ServerResponse.ok()
315+
.cookie(ResponseCookie.from("foo", "bar").build())
316+
.body(BodyInserters.fromObject("body"));
317+
318+
319+
assertFalse(serverResponse.block().cookies().isEmpty());
320+
}
321+
322+
305323
@Test
306324
public void build() {
307325
ResponseCookie cookie = ResponseCookie.from("name", "value").build();

0 commit comments

Comments
 (0)