-
Notifications
You must be signed in to change notification settings - Fork 113
Fix #852 - Process Qute templates in a strict manner #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
984384d
Fix #852 - Process Qute templates in a strict manner
ricardozanini e650d6c
Fix discriminator annotations on pojo.qute
ricardozanini 6ba0f3e
Improve testing on discriminator annotations
ricardozanini 61487e0
Incorporate @hbelmiro review
ricardozanini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...ent/src/main/java/io/quarkiverse/openapi/generator/deployment/template/ExprEvaluator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package io.quarkiverse.openapi.generator.deployment.template; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| import io.quarkus.qute.EvalContext; | ||
| import io.quarkus.qute.Expression; | ||
|
|
||
| final class ExprEvaluator { | ||
|
|
||
| private ExprEvaluator() { | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public static <T> T evaluate(EvalContext context, Expression expression) throws ExecutionException, InterruptedException { | ||
| return (T) context.evaluate(expression).toCompletableFuture().get(); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public static <T> T[] evaluate(EvalContext context, List<Expression> expressions, Class<T> type) | ||
| throws ExecutionException, InterruptedException { | ||
| T[] results = (T[]) java.lang.reflect.Array.newInstance(type, expressions.size()); | ||
|
|
||
| for (int i = 0; i < expressions.size(); i++) { | ||
| Expression expression = expressions.get(i); | ||
| T result = type.cast(context.evaluate(expression).toCompletableFuture().get()); | ||
| results[i] = result; | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
.../main/java/io/quarkiverse/openapi/generator/deployment/template/StrNamespaceResolver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package io.quarkiverse.openapi.generator.deployment.template; | ||
|
|
||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CompletionStage; | ||
|
|
||
| import io.quarkus.qute.EvalContext; | ||
| import io.quarkus.qute.NamespaceResolver; | ||
|
|
||
| /** | ||
| * Namespace resolver to mimic the function of io.quarkus.qute.runtime.extensions.StringTemplateExtensions. | ||
| * This extension is built-in with Qute when used in a context with Quarkus DI. | ||
| * Since these extensions are built in build time by Qute we can't use them because our process also runs in build time without | ||
| * a CDI context. | ||
| * So any namespace resolver auto-generated by Quarkus won't be added to our engine. | ||
| * | ||
| * @see <a href="https://quarkus.io/guides/qute-reference#template_extension_methods">Template Extension Methods</a> | ||
| */ | ||
| public class StrNamespaceResolver implements NamespaceResolver { | ||
|
|
||
| static final StrNamespaceResolver INSTANCE = new StrNamespaceResolver(); | ||
|
|
||
| private StrNamespaceResolver() { | ||
| } | ||
|
|
||
| @Override | ||
| public String getNamespace() { | ||
| return "str"; | ||
| } | ||
|
|
||
| /** | ||
| * @see io.quarkus.qute.runtime.extensions.StringTemplateExtensions#fmt(String, String, Object...) | ||
| */ | ||
| public String fmt(String format, Object... args) { | ||
| return String.format(format, args); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletionStage<Object> resolve(EvalContext context) { | ||
| switch (context.getName()) { | ||
| case "fmt": | ||
| if (context.getParams().size() < 2) { | ||
| throw new IllegalArgumentException( | ||
| "Missing required parameter for 'fmt'. Make sure that the function has at least two parameters"); | ||
| } | ||
| try { | ||
| return CompletableFuture.completedFuture( | ||
| fmt(ExprEvaluator.evaluate(context, context.getParams().get(0)), | ||
| ExprEvaluator.evaluate(context, context.getParams().subList(1, context.getParams().size()), | ||
| Object.class))); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| default: | ||
| throw new IllegalArgumentException("There's no method named '" + context.getName() + "'"); | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.