Skip to content

Commit bd8d71b

Browse files
committed
canWrite uses actual return value type if known
Closes gh-22803
1 parent 07c9a0e commit bd8d71b

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParame
124124

125125
Publisher<?> publisher;
126126
ResolvableType elementType;
127+
ResolvableType actualElementType;
127128
if (adapter != null) {
128129
publisher = adapter.toPublisher(body);
129130
boolean isUnwrapped = KotlinDetector.isKotlinReflectPresent() &&
@@ -132,11 +133,12 @@ protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParame
132133
!COROUTINES_FLOW_CLASS_NAME.equals(bodyType.toClass().getName());
133134
ResolvableType genericType = isUnwrapped ? bodyType : bodyType.getGeneric();
134135
elementType = getElementType(adapter, genericType);
136+
actualElementType = elementType;
135137
}
136138
else {
137139
publisher = Mono.justOrEmpty(body);
138-
elementType = (bodyType.toClass() == Object.class && body != null ?
139-
ResolvableType.forInstance(body) : bodyType);
140+
actualElementType = body != null ? ResolvableType.forInstance(body) : bodyType;
141+
elementType = (bodyType.toClass() == Object.class && body != null ? actualElementType : bodyType);
140142
}
141143

142144
if (elementType.resolve() == void.class || elementType.resolve() == Void.class) {
@@ -151,7 +153,7 @@ protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParame
151153
(publisher instanceof Mono ? "0..1" : "0..N") + " [" + elementType + "]");
152154
}
153155
for (HttpMessageWriter<?> writer : getMessageWriters()) {
154-
if (writer.canWrite(elementType, bestMediaType)) {
156+
if (writer.canWrite(actualElementType, bestMediaType)) {
155157
return writer.write((Publisher) publisher, actualType, elementType,
156158
bestMediaType, exchange.getRequest(), exchange.getResponse(),
157159
Hints.from(Hints.LOG_PREFIX_HINT, logPrefix));

0 commit comments

Comments
 (0)