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
This commit is contained in:
Arjen Poutsma 2024-05-01 12:23:04 +02:00
parent 3f3995f846
commit d5664ba01a
2 changed files with 14 additions and 5 deletions

View File

@ -1265,9 +1265,13 @@ public abstract class RouterFunctions {
return this.routerFunction.route(nestedRequest) return this.routerFunction.route(nestedRequest)
.doOnNext(match -> { .doOnNext(match -> {
if (nestedRequest != serverRequest) { if (nestedRequest != serverRequest) {
serverRequest.attributes().clear(); // new attributes map from nestedRequest.attributes() can be composed of the old attributes,
serverRequest.attributes() // which means that clearing the old attributes will remove those values from new attributes as well
.putAll(nestedRequest.attributes()); // so let's make a copy
Map<String, Object> newAttributes = new LinkedHashMap<>(nestedRequest.attributes());
Map<String, Object> oldAttributes = serverRequest.attributes();
oldAttributes.clear();
oldAttributes.putAll(newAttributes);
} }
}); });
} }

View File

@ -1181,8 +1181,13 @@ public abstract class RouterFunctions {
Optional<HandlerFunction<T>> result = Optional<HandlerFunction<T>> result =
this.routerFunction.route(nestedRequest); this.routerFunction.route(nestedRequest);
if (result.isPresent() && nestedRequest != serverRequest) { if (result.isPresent() && nestedRequest != serverRequest) {
serverRequest.attributes().clear(); // new attributes map from nestedRequest.attributes() can be composed of the old attributes,
serverRequest.attributes().putAll(nestedRequest.attributes()); // which means that clearing the old attributes will remove those values from new attributes as well
// so let's make a copy
Map<String, Object> newAttributes = new LinkedHashMap<>(nestedRequest.attributes());
Map<String, Object> oldAttributes = serverRequest.attributes();
oldAttributes.clear();
oldAttributes.putAll(newAttributes);
} }
return result; return result;
} }