Skip to content

Commit 911a966

Browse files
committed
Make 'resolve' more complex by searching for assignable parameters
Signed-off-by: Ricardo Zanini <[email protected]>
1 parent 3a9b53d commit 911a966

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/template/OpenApiNamespaceResolver.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.quarkiverse.openapi.generator.deployment.template;
22

33
import java.io.File;
4+
import java.lang.reflect.Method;
45
import java.nio.file.Path;
56
import java.util.ArrayList;
67
import java.util.Collections;
@@ -74,34 +75,64 @@ public boolean hasAuthMethods(OperationMap operations) {
7475
* @see "resources/templates/libraries/microprofile/auth/compositeAuthenticationProvider.qute"
7576
* @return The list filtered by unique auth name
7677
*/
77-
public List<CodegenSecurity> getUniqueOAuthOperations(ArrayList<CodegenSecurity> oauthOperations) {
78+
public List<CodegenSecurity> getUniqueOAuthOperations(List<CodegenSecurity> oauthOperations) {
7879
if (oauthOperations != null) {
79-
return oauthOperations.stream()
80+
return new ArrayList<>(oauthOperations.stream()
8081
.collect(Collectors.toMap(security -> security.name, security -> security,
8182
(existing, replacement) -> existing, LinkedHashMap::new))
82-
.values().stream().toList();
83+
.values());
8384
}
8485
return Collections.emptyList();
8586
}
8687

8788
@Override
8889
public CompletionStage<Object> resolve(EvalContext context) {
8990
try {
90-
Class<?>[] classArgs = new Class[context.getParams().size()];
9191
Object[] args = new Object[context.getParams().size()];
92+
Class<?>[] classArgs = new Class[context.getParams().size()];
93+
9294
int i = 0;
9395
for (Expression expr : context.getParams()) {
9496
args[i] = context.evaluate(expr).toCompletableFuture().get();
9597
classArgs[i] = args[i].getClass();
9698
i++;
9799
}
98-
return CompletableFuture
99-
.completedFuture(this.getClass().getMethod(context.getName(), classArgs).invoke(this, args));
100+
101+
Method targetMethod = findCompatibleMethod(context.getName(), classArgs);
102+
if (targetMethod == null) {
103+
throw new NoSuchMethodException("No compatible method found for: " + context.getName());
104+
}
105+
106+
return CompletableFuture.completedFuture(targetMethod.invoke(this, args));
100107
} catch (ReflectiveOperationException | InterruptedException | ExecutionException ex) {
101108
return CompletableFuture.failedStage(ex);
102109
}
103110
}
104111

112+
private Method findCompatibleMethod(String methodName, Class<?>[] argTypes) {
113+
for (Method method : this.getClass().getMethods()) {
114+
if (method.getName().equals(methodName)) {
115+
Class<?>[] paramTypes = method.getParameterTypes();
116+
if (isAssignable(paramTypes, argTypes)) {
117+
return method;
118+
}
119+
}
120+
}
121+
return null;
122+
}
123+
124+
private boolean isAssignable(Class<?>[] paramTypes, Class<?>[] argTypes) {
125+
if (paramTypes.length != argTypes.length) {
126+
return false;
127+
}
128+
for (int i = 0; i < paramTypes.length; i++) {
129+
if (!paramTypes[i].isAssignableFrom(argTypes[i])) {
130+
return false;
131+
}
132+
}
133+
return true;
134+
}
135+
105136
@Override
106137
public String getNamespace() {
107138
return "openapi";

client/deployment/src/main/resources/templates/libraries/microprofile/auth/compositeAuthenticationProvider.qute

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package {apiPackage}.auth;
22

33
@jakarta.annotation.Priority(jakarta.ws.rs.Priorities.AUTHENTICATION)
4-
{#if oauthMethods.orEmpty.size > 0}
5-
{#for auth in openapi:getUniqueOAuthOperations(oauthMethods)}
4+
{#for auth in openapi:getUniqueOAuthOperations(oauthMethods.orEmpty)}
65
@io.quarkiverse.openapi.generator.markers.OauthAuthenticationMarker(name="{auth.name}", openApiSpecId="{quarkus-generator.openApiSpecId}")
76
{/for}
8-
{/if}
97
{#for auth in httpBasicMethods.orEmpty}
108
@io.quarkiverse.openapi.generator.markers.BasicAuthenticationMarker(name="{auth.name}", openApiSpecId="{quarkus-generator.openApiSpecId}")
119
{/for}

0 commit comments

Comments
 (0)