Skip to content

Commit 4e13a69

Browse files
committed
Fix spring-websocket test failure after 6c2f60
See gh-32813
1 parent 7f9e541 commit 4e13a69

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.java

+28-12
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ class ReactiveTypeHandler {
9393

9494
private final ContentNegotiationManager contentNegotiationManager;
9595

96-
private final ContextSnapshotFactory contextSnapshotFactory;
96+
@Nullable
97+
private final Object contextSnapshotHelper;
9798

9899

99100
public ReactiveTypeHandler() {
@@ -102,16 +103,23 @@ public ReactiveTypeHandler() {
102103

103104
ReactiveTypeHandler(
104105
ReactiveAdapterRegistry registry, TaskExecutor executor, ContentNegotiationManager manager,
105-
@Nullable ContextSnapshotFactory contextSnapshotFactory) {
106+
@Nullable Object contextSnapshotFactory) {
106107

107108
Assert.notNull(registry, "ReactiveAdapterRegistry is required");
108109
Assert.notNull(executor, "TaskExecutor is required");
109110
Assert.notNull(manager, "ContentNegotiationManager is required");
110111
this.adapterRegistry = registry;
111112
this.taskExecutor = executor;
112113
this.contentNegotiationManager = manager;
113-
this.contextSnapshotFactory = (contextSnapshotFactory != null ?
114-
contextSnapshotFactory : ContextSnapshotFactory.builder().build());
114+
this.contextSnapshotHelper = initContextSnapshotHelper(contextSnapshotFactory);
115+
}
116+
117+
@Nullable
118+
private static Object initContextSnapshotHelper(@Nullable Object snapshotFactory) {
119+
if (isContextPropagationPresent) {
120+
return new ContextSnapshotHelper((ContextSnapshotFactory) snapshotFactory);
121+
}
122+
return null;
115123
}
116124

117125

@@ -140,8 +148,10 @@ public ResponseBodyEmitter handleValue(Object returnValue, MethodParameter retur
140148

141149
TaskDecorator taskDecorator = null;
142150
if (isContextPropagationPresent) {
143-
returnValue = ContextSnapshotHelper.writeReactorContext(returnValue, this.contextSnapshotFactory);
144-
taskDecorator = ContextSnapshotHelper.getTaskDecorator(this.contextSnapshotFactory);
151+
ContextSnapshotHelper helper = (ContextSnapshotHelper) this.contextSnapshotHelper;
152+
Assert.notNull(helper, "No ContextSnapshotHelper");
153+
returnValue = helper.writeReactorContext(returnValue);
154+
taskDecorator = helper.getTaskDecorator();
145155
}
146156

147157
ResolvableType elementType = ResolvableType.forMethodParameter(returnType).getGeneric();
@@ -534,25 +544,31 @@ public ResolvableType getReturnType() {
534544
}
535545

536546

537-
private static class ContextSnapshotHelper {
547+
private static final class ContextSnapshotHelper {
548+
549+
private final ContextSnapshotFactory snapshotFactory;
550+
551+
private ContextSnapshotHelper(@Nullable ContextSnapshotFactory factory) {
552+
this.snapshotFactory = (factory != null ? factory : ContextSnapshotFactory.builder().build());
553+
}
538554

539555
@SuppressWarnings("ReactiveStreamsUnusedPublisher")
540-
public static Object writeReactorContext(Object returnValue, ContextSnapshotFactory snapshotFactory) {
556+
public Object writeReactorContext(Object returnValue) {
541557
if (Mono.class.isAssignableFrom(returnValue.getClass())) {
542-
ContextSnapshot snapshot = snapshotFactory.captureAll();
558+
ContextSnapshot snapshot = this.snapshotFactory.captureAll();
543559
return ((Mono<?>) returnValue).contextWrite(snapshot::updateContext);
544560
}
545561
else if (Flux.class.isAssignableFrom(returnValue.getClass())) {
546-
ContextSnapshot snapshot = snapshotFactory.captureAll();
562+
ContextSnapshot snapshot = this.snapshotFactory.captureAll();
547563
return ((Flux<?>) returnValue).contextWrite(snapshot::updateContext);
548564
}
549565
else {
550566
return returnValue;
551567
}
552568
}
553569

554-
public static TaskDecorator getTaskDecorator(ContextSnapshotFactory snapshotFactory) {
555-
return new ContextPropagatingTaskDecorator(snapshotFactory);
570+
public TaskDecorator getTaskDecorator() {
571+
return new ContextPropagatingTaskDecorator(this.snapshotFactory);
556572
}
557573
}
558574

0 commit comments

Comments
 (0)