Skip to content

Commit

Permalink
Merge pull request #8420 from gsmet/rest-client-provider-fix
Browse files Browse the repository at this point in the history
Revert how readers and writers are registered for REST clients
  • Loading branch information
gsmet authored Apr 14, 2020
2 parents 6b40da8 + 62c4bf3 commit 0769017
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import io.quarkus.restclient.runtime.RestClientBase;
import io.quarkus.restclient.runtime.RestClientRecorder;
import io.quarkus.resteasy.common.deployment.JaxrsProvidersToRegisterBuildItem;
import io.quarkus.resteasy.common.deployment.RestClientBuildItem;
import io.quarkus.resteasy.common.deployment.ResteasyDotNames;
import io.quarkus.resteasy.common.deployment.ResteasyInjectionReadyBuildItem;

Expand Down Expand Up @@ -145,6 +146,7 @@ void processInterfaces(CombinedIndexBuildItem combinedIndexBuildItem,
BuildProducer<BeanRegistrarBuildItem> beanRegistrars,
BuildProducer<ExtensionSslNativeSupportBuildItem> extensionSslNativeSupport,
BuildProducer<ServiceProviderBuildItem> serviceProvider,
BuildProducer<RestClientBuildItem> restClient,
RestClientRecorder restClientRecorder) {

// According to the spec only rest client interfaces annotated with RegisterRestClient are registered as beans
Expand All @@ -160,6 +162,10 @@ void processInterfaces(CombinedIndexBuildItem combinedIndexBuildItem,
return;
}

for (DotName interfaze : interfaces.keySet()) {
restClient.produce(new RestClientBuildItem(interfaze.toString()));
}

for (Map.Entry<DotName, ClassInfo> entry : interfaces.entrySet()) {
String iName = entry.getKey().toString();
// the native image proxy definitions have to be separate because
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.resteasy.common.deployment;

import io.quarkus.builder.item.MultiBuildItem;

/**
* Used to mark a class as a potential REST client interface consumed by the MicroProfile REST client.
* <p>
* Useful when you want to apply different behaviors to REST resources and REST clients.
*/
public final class RestClientBuildItem extends MultiBuildItem {

private String interfaceName;

public RestClientBuildItem(String interfaceName) {
this.interfaceName = interfaceName;
}

public String getInterfaceName() {
return interfaceName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ ResteasyInjectionReadyBuildItem setupResteasyInjection(List<ProxyUnwrapperBuildI
JaxrsProvidersToRegisterBuildItem setupProviders(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
CombinedIndexBuildItem indexBuildItem,
BeanArchiveIndexBuildItem beanArchiveIndexBuildItem,
List<ResteasyJaxrsProviderBuildItem> contributedProviderBuildItems, Capabilities capabilities) throws Exception {
List<ResteasyJaxrsProviderBuildItem> contributedProviderBuildItems,
List<RestClientBuildItem> restClients, Capabilities capabilities) throws Exception {

Set<String> contributedProviders = new HashSet<>();
for (ResteasyJaxrsProviderBuildItem contributedProviderBuildItem : contributedProviderBuildItems) {
Expand Down Expand Up @@ -197,8 +198,8 @@ JaxrsProvidersToRegisterBuildItem setupProviders(BuildProducer<ReflectiveClassBu
IndexView beansIndex = beanArchiveIndexBuildItem.getIndex();

// find the providers declared in our services
boolean useBuiltinProviders = collectDeclaredProviders(providersToRegister, categorizedReaders, categorizedWriters,
categorizedContextResolvers, index, beansIndex);
boolean useBuiltinProviders = collectDeclaredProviders(restClients, providersToRegister, categorizedReaders,
categorizedWriters, categorizedContextResolvers, index, beansIndex);

if (useBuiltinProviders) {
providersToRegister = new HashSet<>(contributedProviders);
Expand Down Expand Up @@ -398,23 +399,41 @@ public static void categorizeProviders(Set<String> availableProviders, MediaType
}
}

private static boolean collectDeclaredProviders(Set<String> providersToRegister,
private static boolean collectDeclaredProviders(List<RestClientBuildItem> restClients,
Set<String> providersToRegister,
MediaTypeMap<String> categorizedReaders, MediaTypeMap<String> categorizedWriters,
MediaTypeMap<String> categorizedContextResolvers, IndexView... indexes) {
Set<String> restClientNames = restClients.stream()
.map(RestClientBuildItem::getInterfaceName)
.collect(Collectors.toSet());

for (IndexView index : indexes) {
for (ProviderDiscoverer providerDiscoverer : PROVIDER_DISCOVERERS) {
Collection<AnnotationInstance> getMethods = index.getAnnotations(providerDiscoverer.getMethodAnnotation());
for (AnnotationInstance getMethod : getMethods) {
MethodInfo methodTarget = getMethod.target().asMethod();
if (collectDeclaredProvidersForMethodAndMediaTypeAnnotation(providersToRegister, categorizedReaders,
methodTarget, ResteasyDotNames.CONSUMES, providerDiscoverer.noConsumesDefaultsToAll())) {
return true;
}
if (collectDeclaredProvidersForMethodAndMediaTypeAnnotation(providersToRegister, categorizedWriters,
methodTarget, ResteasyDotNames.PRODUCES, providerDiscoverer.noProducesDefaultsToAll())) {
return true;
if (restClientNames.contains(methodTarget.declaringClass().name().toString())) {
// when dealing with a REST client, we need to collect @Consumes as writers and @Produces as readers
if (collectDeclaredProvidersForMethodAndMediaTypeAnnotation(providersToRegister, categorizedWriters,
methodTarget, ResteasyDotNames.CONSUMES, providerDiscoverer.noConsumesDefaultsToAll())) {
return true;
}
if (collectDeclaredProvidersForMethodAndMediaTypeAnnotation(providersToRegister, categorizedReaders,
methodTarget, ResteasyDotNames.PRODUCES, providerDiscoverer.noProducesDefaultsToAll())) {
return true;
}
} else {
// for JAX-RS resources, we do the opposite
if (collectDeclaredProvidersForMethodAndMediaTypeAnnotation(providersToRegister, categorizedReaders,
methodTarget, ResteasyDotNames.CONSUMES, providerDiscoverer.noConsumesDefaultsToAll())) {
return true;
}
if (collectDeclaredProvidersForMethodAndMediaTypeAnnotation(providersToRegister, categorizedWriters,
methodTarget, ResteasyDotNames.PRODUCES, providerDiscoverer.noProducesDefaultsToAll())) {
return true;
}
}

if (collectDeclaredProvidersForMethodAndMediaTypeAnnotation(providersToRegister,
categorizedContextResolvers, methodTarget, ResteasyDotNames.CONSUMES,
providerDiscoverer.noConsumesDefaultsToAll())) {
Expand Down

0 comments on commit 0769017

Please sign in to comment.