Skip to content

Commit

Permalink
Use cached MessageBodyReader for reading the body (#10186)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov authored Dec 4, 2023
1 parent 77c198c commit da0a274
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.value.ConvertibleValues;
import io.micronaut.core.execution.ExecutionFlow;
import io.micronaut.http.HttpAttributes;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.bind.binders.DefaultBodyAnnotationBinder;
Expand All @@ -33,6 +34,7 @@
import io.micronaut.http.server.netty.FormDataHttpContentProcessor;
import io.micronaut.http.server.netty.NettyHttpRequest;
import io.micronaut.http.server.netty.body.ImmediateByteBody;
import io.micronaut.web.router.RouteInfo;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -135,14 +137,20 @@ Optional<T> transform(NettyHttpRequest<?> nhr, ArgumentConversionContext<T> cont
.convert(conversionService, context)
.map(o -> (T) o.claimForExternal());
}

MessageBodyReader<T> reader = null;
final RouteInfo<?> routeInfo = nhr.getAttribute(HttpAttributes.ROUTE_INFO, RouteInfo.class).orElse(null);
if (routeInfo != null) {
reader = (MessageBodyReader<T>) routeInfo.getMessageBodyReader();
}
MediaType mediaType = nhr.getContentType().orElse(null);
if (mediaType != null) {
Optional<MessageBodyReader<T>> reader = bodyHandlerRegistry.findReader(context.getArgument(), List.of(mediaType));
if (reader.isPresent()) {
if (reader == null) {
reader = bodyHandlerRegistry.findReader(context.getArgument(), List.of(mediaType)).orElse(null);
}
if (reader != null) {
try {
//noinspection unchecked
return Optional.ofNullable((T) imm.processSingle(httpServerConfiguration, reader.get(), context.getArgument(), mediaType, nhr.getHeaders()).claimForExternal());
return Optional.ofNullable((T) imm.processSingle(httpServerConfiguration, reader, context.getArgument(), mediaType, nhr.getHeaders()).claimForExternal());
} catch (CodecException ce) {
if (ce.getCause() instanceof Exception e) {
context.reject(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.bind.ArgumentBinder;
import io.micronaut.core.bind.annotation.Bindable;
import io.micronaut.core.convert.value.ConvertibleValues;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.bind.RequestBinderRegistry;
import io.micronaut.http.bind.binders.RequestArgumentBinder;
import io.micronaut.http.body.MessageBodyHandlerRegistry;
Expand Down Expand Up @@ -52,7 +54,6 @@ sealed class DefaultMethodBasedRouteInfo<T, R> extends DefaultRouteInfo<R> imple
private static final RequestArgumentBinder[] ZERO_BINDERS = new RequestArgumentBinder[0];
private final MethodExecutionHandle<T, R> targetMethod;
private final String[] argumentNames;
private final Map<String, Argument<?>> requiredInputs;
private final boolean isVoid;
private final Optional<Argument<?>> optionalBodyArgument;
private final Optional<Argument<?>> optionalFullBodyArgument;
Expand All @@ -76,17 +77,18 @@ public DefaultMethodBasedRouteInfo(MethodExecutionHandle<T, R> targetMethod,

Argument<?>[] arguments = targetMethod.getArguments();
argumentNames = new String[arguments.length];
Map<String, Argument<?>> requiredInputs;
if (arguments.length > 0) {
Map<String, Argument<?>> requiredInputs = CollectionUtils.newLinkedHashMap(arguments.length);
Map<String, Argument<?>> ri = CollectionUtils.newLinkedHashMap(arguments.length);
for (int i = 0; i < arguments.length; i++) {
Argument<?> requiredArgument = arguments[i];
String inputName = resolveInputName(requiredArgument);
requiredInputs.put(inputName, requiredArgument);
ri.put(inputName, requiredArgument);
argumentNames[i] = inputName;
}
this.requiredInputs = Collections.unmodifiableMap(requiredInputs);
requiredInputs = ri;
} else {
this.requiredInputs = Collections.emptyMap();
requiredInputs = Collections.emptyMap();
}
if (returnType.isVoid()) {
isVoid = true;
Expand All @@ -104,7 +106,10 @@ public DefaultMethodBasedRouteInfo(MethodExecutionHandle<T, R> targetMethod,
}
optionalFullBodyArgument = super.getFullRequestBodyType();
this.messageBodyReader = optionalBodyArgument.flatMap(b -> {
if (b.isAsyncOrReactive() || b.isOptional()) {
if (b.getAnnotationMetadata().stringValue(Body.class).isPresent() || !b.getAnnotationMetadata().hasAnnotation(Body.class)) {
// Special case for `@Body("myProperty")`
b = Argument.of(ConvertibleValues.class);
} else if (b.isAsyncOrReactive() || b.isOptional()) {
b = b.getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT);
}
return messageBodyHandlerRegistry.findReader(b, consumesMediaTypes);
Expand Down

0 comments on commit da0a274

Please sign in to comment.