Skip to content

Commit 6324a1b

Browse files
committed
Copy cookies and hints in built ServerResponse
Closes gh-22481
1 parent 0ca8428 commit 6324a1b

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

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

Lines changed: 2 additions & 4 deletions
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.
@@ -202,16 +202,14 @@ private static final class DefaultEntityResponse<T>
202202

203203
private final BodyInserter<T, ? super ServerHttpResponse> inserter;
204204

205-
private final Map<String, Object> hints;
206205

207206
public DefaultEntityResponse(int statusCode, HttpHeaders headers,
208207
MultiValueMap<String, ResponseCookie> cookies, T entity,
209208
BodyInserter<T, ? super ServerHttpResponse> inserter, Map<String, Object> hints) {
210209

211-
super(statusCode, headers, cookies);
210+
super(statusCode, headers, cookies, hints);
212211
this.entity = entity;
213212
this.inserter = inserter;
214-
this.hints = hints;
215213
}
216214

217215
@Override

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

Lines changed: 2 additions & 2 deletions
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.
@@ -168,7 +168,7 @@ private static final class DefaultRenderingResponse extends DefaultServerRespons
168168
public DefaultRenderingResponse(int statusCode, HttpHeaders headers,
169169
MultiValueMap<String, ResponseCookie> cookies, String name, Map<String, Object> model) {
170170

171-
super(statusCode, headers, cookies);
171+
super(statusCode, headers, cookies, Collections.emptyMap());
172172
this.name = name;
173173
this.model = Collections.unmodifiableMap(new LinkedHashMap<>(model));
174174
}

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

Lines changed: 19 additions & 8 deletions
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.
@@ -20,6 +20,7 @@
2020
import java.time.Instant;
2121
import java.time.ZonedDateTime;
2222
import java.util.Arrays;
23+
import java.util.Collections;
2324
import java.util.EnumSet;
2425
import java.util.HashMap;
2526
import java.util.LinkedHashSet;
@@ -73,9 +74,16 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
7374

7475
public DefaultServerResponseBuilder(ServerResponse other) {
7576
Assert.notNull(other, "ServerResponse must not be null");
76-
this.statusCode = (other instanceof AbstractServerResponse ?
77-
((AbstractServerResponse) other).statusCode : other.statusCode().value());
7877
this.headers.addAll(other.headers());
78+
this.cookies.addAll(other.cookies());
79+
if (other instanceof AbstractServerResponse) {
80+
AbstractServerResponse abstractOther = (AbstractServerResponse) other;
81+
this.statusCode = abstractOther.statusCode;
82+
this.hints.putAll(abstractOther.hints);
83+
}
84+
else {
85+
this.statusCode = other.statusCode().value();
86+
}
7987
}
8088

8189
public DefaultServerResponseBuilder(HttpStatus status) {
@@ -289,12 +297,17 @@ abstract static class AbstractServerResponse implements ServerResponse {
289297

290298
private final MultiValueMap<String, ResponseCookie> cookies;
291299

300+
final Map<String, Object> hints;
301+
302+
292303
protected AbstractServerResponse(
293-
int statusCode, HttpHeaders headers, MultiValueMap<String, ResponseCookie> cookies) {
304+
int statusCode, HttpHeaders headers, MultiValueMap<String, ResponseCookie> cookies,
305+
Map<String, Object> hints) {
294306

295307
this.statusCode = statusCode;
296308
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
297309
this.cookies = CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>(cookies));
310+
this.hints = hints;
298311
}
299312

300313
@Override
@@ -361,7 +374,7 @@ public WriterFunctionResponse(int statusCode, HttpHeaders headers,
361374
MultiValueMap<String, ResponseCookie> cookies,
362375
BiFunction<ServerWebExchange, Context, Mono<Void>> writeFunction) {
363376

364-
super(statusCode, headers, cookies);
377+
super(statusCode, headers, cookies, Collections.emptyMap());
365378
Assert.notNull(writeFunction, "BiFunction must not be null");
366379
this.writeFunction = writeFunction;
367380
}
@@ -377,16 +390,14 @@ private static final class BodyInserterResponse<T> extends AbstractServerRespons
377390

378391
private final BodyInserter<T, ? super ServerHttpResponse> inserter;
379392

380-
private final Map<String, Object> hints;
381393

382394
public BodyInserterResponse(int statusCode, HttpHeaders headers,
383395
MultiValueMap<String, ResponseCookie> cookies,
384396
BodyInserter<T, ? super ServerHttpResponse> body, Map<String, Object> hints) {
385397

386-
super(statusCode, headers, cookies);
398+
super(statusCode, headers, cookies, hints);
387399
Assert.notNull(body, "BodyInserter must not be null");
388400
this.inserter = body;
389-
this.hints = hints;
390401
}
391402

392403
@Override

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

Lines changed: 9 additions & 3 deletions
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.
@@ -65,11 +65,17 @@ public List<ViewResolver> viewResolvers() {
6565

6666
@Test
6767
public void from() {
68-
ServerResponse other = ServerResponse.ok().header("foo", "bar").build().block();
68+
ResponseCookie cookie = ResponseCookie.from("foo", "bar").build();
69+
ServerResponse other = ServerResponse.ok().header("foo", "bar")
70+
.cookie(cookie)
71+
.hint("foo", "bar")
72+
.build().block();
73+
6974
Mono<ServerResponse> result = ServerResponse.from(other).build();
7075
StepVerifier.create(result)
7176
.expectNextMatches(response -> HttpStatus.OK.equals(response.statusCode()) &&
72-
"bar".equals(response.headers().getFirst("foo")))
77+
"bar".equals(response.headers().getFirst("foo")) &&
78+
cookie.equals(response.cookies().getFirst("foo")))
7379
.expectComplete()
7480
.verify();
7581
}

0 commit comments

Comments
 (0)