Skip to content

Commit

Permalink
feat: add support in ConfigurationEvaluatorProcessor for Jackson
Browse files Browse the repository at this point in the history
JsonTypeInfo annotation on sslOptions classes
  • Loading branch information
remibaptistegio committed Jan 6, 2025
1 parent 85d335a commit 96e5b8f
Show file tree
Hide file tree
Showing 22 changed files with 2,004 additions and 45 deletions.
23 changes: 17 additions & 6 deletions gravitee-plugin-annotation-processors/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

=== Goal

This annotation is used to generate an evaluator at compile time for a specific configuration bean. This evaluator can then be used instead of the configuration bean to add the business logic needed to add dynamic evaluation of the properties contained in the configuration bean. The evaluator checks for every property if an attribute is present in the execution context to override its value and if the parameter is a string type, it also tries to evaluate its content using the template engine in case an EL was defined for this parameter.
This annotation is used to generate an evaluator at compile time for a specific configuration bean. This evaluator can then be used instead of the configuration bean to add the business logic needed to add dynamic evaluation of the properties contained in the configuration bean. The evaluator checks for every property if an attribute is present in the execution context to override its value and if the parameter is a string type, it also tries to evaluate its content using the template engine in case an EL was defined for this parameter. It also supports `@Secret` annotation to inject automatically the specified FieldKind and attribute name of the secret in the template engine context before evaluation.
Note that the evaluator only support two external classes annotated with `@JsonTypeInfo` Jackson annotation : "io.gravitee.definition.model.v4.ssl.TrustStore" and "io.gravitee.definition.model.v4.ssl.KeyStore". If you use this annotation in the sources parsed by the processor it will work but if you use an external dependency that contains this annotation it will not except for the classes mentioned above.

=== Parameters

Expand Down Expand Up @@ -85,7 +86,7 @@ Note that to avoid having a dependency on a Jakarta Expression Language implemen
</dependency>
----
[start=2]
. Add Hibernate Validator to your dependencies with a scope `provided` (this dependency was added in the Gateway to avoid duplication in each plugin using this annotation)
. Add Hibernate Validator and Jackson databind to your dependencies with a scope `provided` (Hibernate validator dependency was added in the Gateway to avoid duplication in each plugin using this annotation)

[source, xml]
----
Expand All @@ -95,6 +96,11 @@ Note that to avoid having a dependency on a Jakarta Expression Language implemen
<version>8.0.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>provided</scope>
</dependency>
----
[start=3]
. Add this plugin in your pom.xml to define the generated sources as source file, so you can use them in your code in your IDE)
Expand Down Expand Up @@ -158,10 +164,15 @@ Note that to avoid having a dependency on a Jakarta Expression Language implemen

=== Methods available in evaluator class

The generated evaluator provides 3 methods that you can use :
The generated evaluator support both BaseExecutionContext and DeploymentContext (i.e. for resource) and provides 4 methods that you can use :

* public Single<ConfigurationClass> eval(BaseExecutionContext ctx) : default method to use which parse the original configuration using the given execution context and evaluate it in a reactive way then return the evaluated configuration
* public ConfigurationClass evalNow(BaseExecutionContext ctx) : equivalent to eval but *blocking*

* public Single<ConfigurationClass> eval(DeploymentContext ctx) : default method to use which parse the original configuration using the given deployment context and evaluate it in a reactive way then return the evaluated configuration
* public ConfigurationClass evalNow(DeploymentContext ctx) : equivalent to eval but *blocking*

* public Single<ConfigurationClass> eval(ExecutionContext ctx) : default method to use which parse the original configuration using the given context and evaluate it in a reactive way then return the evaluated configuration
* public ConfigurationClass evalNow() : equivalent to eval but *blocking*
Note that DeploymentContext do not support dynamic configuration using attribute, only field that support EL will be dynamic.

=== Contribute

Expand All @@ -178,4 +189,4 @@ We only call evaluatorHeader and evaluatorFooter templates once, but we loop on
To validate the generated code, unit tests have been developed with two classes:

- ConfigurationEvaluatorProcessorTest: in charge of verifying that the code generated is the one expected by invoking the annotation processor on the class src/test/java/io/gravitee/plugin/annotation/processor/result/TestConfiguration.java and comparing the evaluator generated with the expected result in src/test/resources/test/TestConfigurationEvaluator.java. So if you change the code generated, you need to ensure that you also update this file to reflect the changes you were expected.
- ConfigurationEvaluatorGeneratedTest: in charge of validating the logic of the code generated for the evaluator (for example, validate that if an attribute exist in the context to override a field which is an Enum in the configuration, the configuration evaluated is reflecting this).
- ConfigurationEvaluatorGeneratedTest: in charge of validating the logic of the code generated for the evaluator (for example, validate that if an attribute exist in the context to override a field which is an Enum in the configuration, the configuration evaluated is reflecting this).
1 change: 0 additions & 1 deletion gravitee-plugin-annotation-processors/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
<hibernate-validator.version>8.0.1.Final</hibernate-validator.version>
<mustache.version>0.9.10</mustache.version>
<compile-testing.version>0.21.0</compile-testing.version>
<gravitee-secret-api.version>1.0.0-feat-secret-kind-annotation-SNAPSHOT</gravitee-secret-api.version>
</properties>

<dependencies>
Expand Down
Loading

0 comments on commit 96e5b8f

Please sign in to comment.