Skip to content

Commit

Permalink
Don't use DynamicMessageBodyWriter (#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov authored Aug 5, 2024
1 parent cb31fb9 commit b5f5176
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.micronaut.core.execution.ExecutionFlow;
import io.micronaut.core.io.Writable;
import io.micronaut.core.propagation.PropagatedContext;
import io.micronaut.core.reflect.ClassUtils;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.ArrayUtils;
import io.micronaut.http.HttpAttributes;
Expand Down Expand Up @@ -380,7 +381,7 @@ private void encodeResponse(ServletExchange<REQ, RES> exchange,

if (body != null && !isVoid) {
Class<?> bodyType = body.getClass();
if (bodyArgument == null || !bodyArgument.isInstance(body)) {
if (bodyArgument == null || !bodyArgument.isInstance(body) || bodyArgument.getType().equals(Object.class)) {
bodyArgument = (Argument<Object>) Argument.of(bodyType);
}
ServletResponseEncoder<Object> responseEncoder = (ServletResponseEncoder<Object>) responseEncoders.get(bodyType);
Expand Down Expand Up @@ -421,7 +422,15 @@ private void encodeResponse(ServletExchange<REQ, RES> exchange,
if (!(body instanceof HttpStatus)) {
messageBodyWriter = routeInfoAttribute.map(RouteInfo::getMessageBodyWriter).orElse(null);
if (messageBodyWriter == null) {
messageBodyWriter = new DynamicMessageBodyWriter(messageBodyHandlerRegistry, List.of(mediaType));
MediaType finalMediaType = mediaType;
Argument<Object> finalBodyArgument = bodyArgument;
Optional<MessageBodyWriter<Object>> writer = messageBodyHandlerRegistry.findWriter(bodyArgument, List.of(mediaType));
if (writer.isEmpty() && mediaType.equals(MediaType.TEXT_PLAIN_TYPE) && ClassUtils.isJavaBasicType(body.getClass())) {
// TODO: remove after Core 4.6
writer = (Optional) messageBodyHandlerRegistry.findWriter(Argument.STRING, List.of(MediaType.TEXT_PLAIN_TYPE));
}
messageBodyWriter = writer
.orElseThrow(() -> new CodecException("Cannot encode value of argument [" + finalBodyArgument + "]. No possible encoders found for media type: " + finalMediaType));
}
}

Expand Down

0 comments on commit b5f5176

Please sign in to comment.