|
1 | 1 | package io.quarkiverse.openapi.generator.deployment.template; |
2 | 2 |
|
3 | 3 | import java.io.File; |
| 4 | +import java.lang.reflect.Method; |
4 | 5 | import java.nio.file.Path; |
5 | 6 | import java.util.ArrayList; |
6 | 7 | import java.util.Collections; |
@@ -74,34 +75,64 @@ public boolean hasAuthMethods(OperationMap operations) { |
74 | 75 | * @see "resources/templates/libraries/microprofile/auth/compositeAuthenticationProvider.qute" |
75 | 76 | * @return The list filtered by unique auth name |
76 | 77 | */ |
77 | | - public List<CodegenSecurity> getUniqueOAuthOperations(ArrayList<CodegenSecurity> oauthOperations) { |
| 78 | + public List<CodegenSecurity> getUniqueOAuthOperations(List<CodegenSecurity> oauthOperations) { |
78 | 79 | if (oauthOperations != null) { |
79 | | - return oauthOperations.stream() |
| 80 | + return new ArrayList<>(oauthOperations.stream() |
80 | 81 | .collect(Collectors.toMap(security -> security.name, security -> security, |
81 | 82 | (existing, replacement) -> existing, LinkedHashMap::new)) |
82 | | - .values().stream().toList(); |
| 83 | + .values()); |
83 | 84 | } |
84 | 85 | return Collections.emptyList(); |
85 | 86 | } |
86 | 87 |
|
87 | 88 | @Override |
88 | 89 | public CompletionStage<Object> resolve(EvalContext context) { |
89 | 90 | try { |
90 | | - Class<?>[] classArgs = new Class[context.getParams().size()]; |
91 | 91 | Object[] args = new Object[context.getParams().size()]; |
| 92 | + Class<?>[] classArgs = new Class[context.getParams().size()]; |
| 93 | + |
92 | 94 | int i = 0; |
93 | 95 | for (Expression expr : context.getParams()) { |
94 | 96 | args[i] = context.evaluate(expr).toCompletableFuture().get(); |
95 | 97 | classArgs[i] = args[i].getClass(); |
96 | 98 | i++; |
97 | 99 | } |
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)); |
100 | 107 | } catch (ReflectiveOperationException | InterruptedException | ExecutionException ex) { |
101 | 108 | return CompletableFuture.failedStage(ex); |
102 | 109 | } |
103 | 110 | } |
104 | 111 |
|
| 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 | + |
105 | 136 | @Override |
106 | 137 | public String getNamespace() { |
107 | 138 | return "openapi"; |
|
0 commit comments