Skip to content

Commit d5664ba

Browse files
committed
Ensure old attributes are not removed by accident
This commit ensures that a copy is made of old attributes before replacing it with new attributes. Because new attributes can be composed of old, clearing the old would also remove entries from the new. See gh-32245
1 parent 3f3995f commit d5664ba

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,9 +1265,13 @@ public Mono<HandlerFunction<T>> route(ServerRequest serverRequest) {
12651265
return this.routerFunction.route(nestedRequest)
12661266
.doOnNext(match -> {
12671267
if (nestedRequest != serverRequest) {
1268-
serverRequest.attributes().clear();
1269-
serverRequest.attributes()
1270-
.putAll(nestedRequest.attributes());
1268+
// new attributes map from nestedRequest.attributes() can be composed of the old attributes,
1269+
// which means that clearing the old attributes will remove those values from new attributes as well
1270+
// so let's make a copy
1271+
Map<String, Object> newAttributes = new LinkedHashMap<>(nestedRequest.attributes());
1272+
Map<String, Object> oldAttributes = serverRequest.attributes();
1273+
oldAttributes.clear();
1274+
oldAttributes.putAll(newAttributes);
12711275
}
12721276
});
12731277
}

spring-webmvc/src/main/java/org/springframework/web/servlet/function/RouterFunctions.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,8 +1181,13 @@ public Optional<HandlerFunction<T>> route(ServerRequest serverRequest) {
11811181
Optional<HandlerFunction<T>> result =
11821182
this.routerFunction.route(nestedRequest);
11831183
if (result.isPresent() && nestedRequest != serverRequest) {
1184-
serverRequest.attributes().clear();
1185-
serverRequest.attributes().putAll(nestedRequest.attributes());
1184+
// new attributes map from nestedRequest.attributes() can be composed of the old attributes,
1185+
// which means that clearing the old attributes will remove those values from new attributes as well
1186+
// so let's make a copy
1187+
Map<String, Object> newAttributes = new LinkedHashMap<>(nestedRequest.attributes());
1188+
Map<String, Object> oldAttributes = serverRequest.attributes();
1189+
oldAttributes.clear();
1190+
oldAttributes.putAll(newAttributes);
11861191
}
11871192
return result;
11881193
}

0 commit comments

Comments
 (0)