Skip to content

Commit

Permalink
Refactor according to code review
Browse files Browse the repository at this point in the history
  • Loading branch information
aureamunoz committed Dec 3, 2024
1 parent 6897774 commit c01469f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.RuntimeType;
Expand Down Expand Up @@ -171,6 +172,7 @@
import io.quarkus.jaxrs.client.reactive.runtime.ToObjectArray;
import io.quarkus.jaxrs.client.reactive.runtime.impl.MultipartResponseDataBase;
import io.quarkus.resteasy.reactive.common.deployment.ApplicationResultBuildItem;
import io.quarkus.resteasy.reactive.common.deployment.EndpointValidationPredicatesBuildItem;
import io.quarkus.resteasy.reactive.common.deployment.ParameterContainersBuildItem;
import io.quarkus.resteasy.reactive.common.deployment.QuarkusFactoryCreator;
import io.quarkus.resteasy.reactive.common.deployment.QuarkusResteasyReactiveDotNames;
Expand Down Expand Up @@ -289,7 +291,8 @@ void setupClientProxies(JaxrsClientReactiveRecorder recorder,
List<RestClientDefaultConsumesBuildItem> defaultProduces,
List<RestClientDisableSmartDefaultProduces> disableSmartDefaultProduces,
List<RestClientDisableRemovalTrailingSlashBuildItem> disableRemovalTrailingSlashProduces,
List<ParameterContainersBuildItem> parameterContainersBuildItems) {
List<ParameterContainersBuildItem> parameterContainersBuildItems,
List<EndpointValidationPredicatesBuildItem> validationPredicatesBuildItems) {

String defaultConsumesType = defaultMediaType(defaultConsumes, MediaType.APPLICATION_OCTET_STREAM);
String defaultProducesType = defaultMediaType(defaultProduces, MediaType.TEXT_PLAIN);
Expand Down Expand Up @@ -343,6 +346,8 @@ public boolean test(Map<DotName, AnnotationInstance> anns) {
return anns.containsKey(NOT_BODY) || anns.containsKey(URL);
}
})
.setValidateEndpoint(validationPredicatesBuildItems.stream().map(item -> item.getPredicate())
.collect(Collectors.toUnmodifiableList()))
.setResourceMethodCallback(new Consumer<>() {
@Override
public void accept(EndpointIndexer.ResourceMethodCallbackEntry entry) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.resteasy.reactive.common.deployment;

import java.util.function.Predicate;

import org.jboss.jandex.ClassInfo;

import io.quarkus.builder.item.MultiBuildItem;

public final class EndpointValidationPredicatesBuildItem extends MultiBuildItem {

private final Predicate<ClassInfo> predicate;

public EndpointValidationPredicatesBuildItem(Predicate<ClassInfo> predicate) {
this.predicate = predicate;
}

public Predicate<ClassInfo> getPredicate() {
return predicate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.ArrayList;
Expand Down Expand Up @@ -161,6 +162,7 @@
import io.quarkus.netty.deployment.MinNettyAllocatorMaxOrderBuildItem;
import io.quarkus.resteasy.reactive.common.deployment.AggregatedParameterContainersBuildItem;
import io.quarkus.resteasy.reactive.common.deployment.ApplicationResultBuildItem;
import io.quarkus.resteasy.reactive.common.deployment.EndpointValidationPredicatesBuildItem;
import io.quarkus.resteasy.reactive.common.deployment.FactoryUtils;
import io.quarkus.resteasy.reactive.common.deployment.ParameterContainersBuildItem;
import io.quarkus.resteasy.reactive.common.deployment.QuarkusFactoryCreator;
Expand Down Expand Up @@ -468,7 +470,8 @@ public void setupEndpoints(ApplicationIndexBuildItem applicationIndexBuildItem,
CompiledJavaVersionBuildItem compiledJavaVersionBuildItem,
ResourceInterceptorsBuildItem resourceInterceptorsBuildItem,
Capabilities capabilities,
Optional<AllowNotRestParametersBuildItem> allowNotRestParametersBuildItem) {
Optional<AllowNotRestParametersBuildItem> allowNotRestParametersBuildItem,
List<EndpointValidationPredicatesBuildItem> validationPredicatesBuildItems) {

if (!resourceScanningResultBuildItem.isPresent()) {
// no detected @Path, bail out
Expand Down Expand Up @@ -640,6 +643,8 @@ private boolean hasAnnotation(MethodInfo method, short paramPosition, DotName an
})
.setResteasyReactiveRecorder(recorder)
.setApplicationClassPredicate(applicationClassPredicate)
.setValidateEndpoint(validationPredicatesBuildItems.stream().map(item -> item.getPredicate())
.collect(Collectors.toUnmodifiableList()))
.setTargetJavaVersion(new TargetJavaVersion() {

private final Status result;
Expand Down Expand Up @@ -849,6 +854,18 @@ private FilterClassIntrospector createFilterClassIntrospector() {
return ab.get();
}

@BuildStep
EndpointValidationPredicatesBuildItem createSpringRestControllerPredicate() {
Predicate<ClassInfo> predicate = new Predicate<ClassInfo>() {
@Override
public boolean test(ClassInfo classInfo) {
return Modifier.isInterface(classInfo.flags())
|| Modifier.isAbstract(classInfo.flags());
}
};
return new EndpointValidationPredicatesBuildItem(predicate);
}

// We want to add @Typed to resources, beanparams and providers so that they can be resolved as CDI bean using purely their
// class as a bean type. This removes any ambiguity that potential subclasses may have.
@BuildStep
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

import jakarta.ws.rs.Priorities;
import jakarta.ws.rs.core.HttpHeaders;
Expand Down Expand Up @@ -35,6 +36,7 @@
import io.quarkus.gizmo.ClassOutput;
import io.quarkus.jaxrs.spi.deployment.AdditionalJaxRsResourceMethodAnnotationsBuildItem;
import io.quarkus.resteasy.common.spi.ResteasyJaxrsProviderBuildItem;
import io.quarkus.resteasy.reactive.common.deployment.EndpointValidationPredicatesBuildItem;
import io.quarkus.resteasy.reactive.spi.ExceptionMapperBuildItem;

public class SpringWebProcessor {
Expand Down Expand Up @@ -86,6 +88,18 @@ public AdditionalJaxRsResourceMethodAnnotationsBuildItem additionalJaxRsResource
return new AdditionalJaxRsResourceMethodAnnotationsBuildItem(MAPPING_ANNOTATIONS);
}

@BuildStep
EndpointValidationPredicatesBuildItem createSpringRestControllerPredicate() {
Predicate<ClassInfo> predicate = new Predicate<>() {
@Override
public boolean test(ClassInfo classInfo) {
return classInfo
.declaredAnnotation(REST_CONTROLLER_ANNOTATION) == null;
}
};
return new EndpointValidationPredicatesBuildItem(predicate);
}

@BuildStep
public void ignoreReflectionHierarchy(BuildProducer<ReflectiveHierarchyIgnoreWarningBuildItem> ignore) {
ignore.produce(new ReflectiveHierarchyIgnoreWarningBuildItem(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -240,6 +239,7 @@ public abstract class EndpointIndexer<T extends EndpointIndexer<T, PARAM, METHOD
private final Function<ClassInfo, Supplier<Boolean>> isDisabledCreator;

private final Predicate<Map<DotName, AnnotationInstance>> skipMethodParameter;
private final List<Predicate<ClassInfo>> validateEndpoint;
private final boolean skipNotRestParameters;

private SerializerScanningResult serializerScanningResult;
Expand Down Expand Up @@ -268,6 +268,7 @@ protected EndpointIndexer(Builder<T, ?, METHOD> builder) {
this.isDisabledCreator = builder.isDisabledCreator;
this.skipMethodParameter = builder.skipMethodParameter;
this.skipNotRestParameters = builder.skipNotRestParameters;
this.validateEndpoint = builder.validateEndpoint;
}

public Optional<ResourceClass> createEndpoints(ClassInfo classInfo, boolean considerApplication) {
Expand Down Expand Up @@ -325,21 +326,19 @@ public Optional<ResourceClass> createEndpoints(ClassInfo classInfo, boolean cons

return Optional.of(clazz);
} catch (Exception e) {
if (isEndpointValid(classInfo)) {
//kinda bogus, but we just ignore failed interfaces for now
//they can have methods that are not valid until they are actually extended by a concrete type
log.debug("Ignoring interface " + classInfo.name(), e);
return Optional.empty();
for (Predicate<ClassInfo> predicate : validateEndpoint) {
if (predicate.test(classInfo)) {
//kinda bogus, but we just ignore failed interfaces for now
//they can have methods that are not valid until they are actually extended by a concrete type
log.debug("Ignoring interface " + classInfo.name(), e);
return Optional.empty();
} else {
throw new RuntimeException(e);
}

}
throw new RuntimeException(e);
}
}

private boolean isEndpointValid(ClassInfo classInfo) {
EndpointAnnotationHandler defaultEndpointAnnotationHandler = new DefaultEndpointAnnotationHandler();
return ServiceLoader.load(EndpointAnnotationHandler.class).findFirst()
.orElse(defaultEndpointAnnotationHandler).isEndpointAnnotationValid(classInfo);

return Optional.empty();
}

private String sanitizePath(String path) {
Expand Down Expand Up @@ -1716,6 +1715,7 @@ public boolean handleMultipartForReturnType(AdditionalWriters additionalWriters,
private Function<ClassInfo, Supplier<Boolean>> isDisabledCreator = null;

private Predicate<Map<DotName, AnnotationInstance>> skipMethodParameter = null;
private List<Predicate<ClassInfo>> validateEndpoint = null;

private boolean skipNotRestParameters = false;

Expand Down Expand Up @@ -1852,6 +1852,11 @@ public B setSkipMethodParameter(
return (B) this;
}

public B setValidateEndpoint(List<Predicate<ClassInfo>> validateEndpoint) {
this.validateEndpoint = validateEndpoint;
return (B) this;
}

public B skipNotRestParameters(boolean skipNotRestParameters) {
this.skipNotRestParameters = skipNotRestParameters;
return (B) this;
Expand Down

0 comments on commit c01469f

Please sign in to comment.