Skip to content

Commit

Permalink
[RESTEASY-3484] Check all the super method parameters for a @FormPara…
Browse files Browse the repository at this point in the history
…m with an EntityPart type or List<EntityPart> type.

https://issues.redhat.com/browse/RESTEASY-3484
Signed-off-by: James R. Perkins <jperkins@redhat.com>
  • Loading branch information
jamezp committed Mar 29, 2024
1 parent 16d9d11 commit ef1ea03
Showing 1 changed file with 34 additions and 3 deletions.
Expand Up @@ -24,6 +24,8 @@
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import jakarta.ws.rs.FormParam;
Expand Down Expand Up @@ -71,8 +73,7 @@ public void filter(final ContainerRequestContext requestContext) throws IOExcept
if (boundary != null) {
// Get the methods and check that we need to get the entity parts
final Method method = resourceInfo.getResourceMethod();
final Parameter[] parameters = method.getParameters();
if (hasEntityPartParameter(parameters)) {
if (hasEntityPartParameter(findAllParameters(resourceInfo.getResourceClass(), method))) {
final MultipartFormDataInputImpl input = new MultipartFormDataInputImpl(requestContext.getMediaType(),
providers);
// Copy the input stream as it's being parsed. This will allow us to reset the entity stream for
Expand All @@ -93,7 +94,7 @@ public void filter(final ContainerRequestContext requestContext) throws IOExcept
}
}

private static boolean hasEntityPartParameter(final Parameter[] parameters) {
private static boolean hasEntityPartParameter(final List<Parameter> parameters) {
for (Parameter parameter : parameters) {
if (parameter.isAnnotationPresent(FormParam.class)
|| parameter.isAnnotationPresent(org.jboss.resteasy.annotations.jaxrs.FormParam.class)) {
Expand All @@ -108,6 +109,36 @@ private static boolean hasEntityPartParameter(final Parameter[] parameters) {
return false;
}

private static List<Parameter> findAllParameters(final Class<?> resourceClass, final Method method) {
final List<Parameter> parameters = new ArrayList<>();
// First get the annotations from the method parameters
Collections.addAll(parameters, method.getParameters());
// Check the interfaces
for (Class<?> intf : resourceClass.getInterfaces()) {
addAnnotations(intf, method, parameters);
}
// If there is a super type, get the parameters from there as well
if (resourceClass.getSuperclass() != null) {
final Class<?> superType = resourceClass.getSuperclass();
addAnnotations(superType, method, parameters);
// Check the super types interfaces
for (Class<?> intf : superType.getInterfaces()) {
addAnnotations(intf, method, parameters);
}
}
// Get annotations from all the interfaces
return parameters;
}

private static void addAnnotations(final Class<?> type, final Method method, final List<Parameter> parameters) {
// Find the super method
try {
final var superMethod = type.getMethod(method.getName(), method.getParameterTypes());
Collections.addAll(parameters, superMethod.getParameters());
} catch (NoSuchMethodException ignore) {
}
}

private static class CopyInputStream extends InputStream {
private final InputStream delegate;
private final EntityOutputStream entity;
Expand Down

0 comments on commit ef1ea03

Please sign in to comment.