Skip to content

Commit c507203

Browse files
authored
Add capability to use additionalModelTypeAnnotations (#89)
1 parent 4257933 commit c507203

File tree

7 files changed

+58
-4
lines changed

7 files changed

+58
-4
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ To fine tune the configuration for each spec file, add the following entry to yo
5353

5454
```properties
5555
quarkus.openapi-generator.codegen.spec.petstore_json.base-package=org.acme.openapi
56+
quarkus.openapi-generator.codegen.spec.petstore_json.additional-model-type-annotations[email protected];@org.test.Bar
5657
```
5758

5859
If a base package name is not provided, it will be used the default `org.openapi.quarkus.<filename>`. For example, `org.openapi.quarkus.petstore_json`.
5960

61+
Configuring `additional-model-type-annotations` will add all annotations to the generated model files (extra details can be found in [OpenApi Generator Doc](https://openapi-generator.tech/docs/generators/java/#config-options)).
62+
6063
> **⚠️** Note that the file name`petstore_json`is used to configure the specific information for each spec. We follow the [Environment Variables Mapping Rules](https://github.com/eclipse/microprofile-config/blob/master/spec/src/main/asciidoc/configsources.asciidoc#environment-variables-mapping-rules) from Microprofile Configuration to sanitize the OpenAPI spec filename. Any non-alphabetic characters are replaced by an underscore `_`.
6164
6265
Run `mvn compile` to generate your classes in `target/generated-sources/open-api-json` path:
@@ -270,14 +273,14 @@ public class PetResource {
270273
@Consumes(MediaType.APPLICATION_JSON)
271274
public Response customUpdatePet(@PathParam("id") long id, PetData petData) {
272275

273-
// Create a new instance of the Pet class generated by the quarkus-openapi-generator and
276+
// Create a new instance of the Pet class generated by the quarkus-openapi-generator and
274277
// populate accordingly.
275278
Pet pet = new Pet();
276279
pet.setId(id);
277280
applyDataToPet(pet, petData);
278281

279282
// Execute the rest call using the generated client.
280-
// The petstore.json open api spec stays that the "updatePet" operation is secured with the
283+
// The petstore.json open api spec stays that the "updatePet" operation is secured with the
281284
// security scheme "petstore_auth".
282285
petApi.updatePet(pet);
283286

@@ -308,7 +311,7 @@ However, there are scenarios where we want to propagate the authorization token
308311
1) The user service `customUpdatePet` operation is invoked, and an authorization token is passed by the third party typically by using the HTTP `Authorization` header.
309312
2) The incoming authorization token is automatically passed along the PetApi `updatePet` operation execution according to the user-provided configuration.
310313

311-
> **⚠️** When configured, the token propagation applies to all the operations secured with the same `securityScheme` in the same specification file.
314+
> **⚠️** When configured, the token propagation applies to all the operations secured with the same `securityScheme` in the same specification file.
312315
313316
### Propagation flow configuration
314317
The token propagation can be used with type "oauth2" or "bearer" security schemes. Finally, considering that a given security scheme might be configured on a set of operations in the same specification file when configured, it'll apply to all these operations.

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/CodegenConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class CodegenConfig {
2222
static final String BUILD_TIME_SPEC_PREFIX_FORMAT = "quarkus." + CODEGEN_TIME_CONFIG_PREFIX + ".spec.%s";
2323
private static final String BASE_PACKAGE_PROP_FORMAT = "%s.base-package";
2424
private static final String SKIP_FORM_MODEL_PROP_FORMAT = "%s.skip-form-model";
25+
private static final String ADDITIONAL_MODEL_TYPE_ANNOTATIONS_PROP_FORMAT = "%s.additional-model-type-annotations";
2526

2627
/**
2728
* OpenAPI Spec details for codegen configuration.
@@ -56,6 +57,10 @@ public static String getSkipFormModelPropertyName(final Path openApiFilePath) {
5657
return String.format(SKIP_FORM_MODEL_PROP_FORMAT, getBuildTimeSpecPropertyPrefix(openApiFilePath));
5758
}
5859

60+
public static String getAdditionalModelTypeAnnotationsPropertyName(final Path openApiFilePath) {
61+
return String.format(ADDITIONAL_MODEL_TYPE_ANNOTATIONS_PROP_FORMAT, getBuildTimeSpecPropertyPrefix(openApiFilePath));
62+
}
63+
5964
/**
6065
* Gets the config prefix for a given OpenAPI file in the path.
6166
* For example, given a path like /home/luke/projects/petstore.json, the returned value is

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java

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

33
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.VERBOSE_PROPERTY_NAME;
4+
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getAdditionalModelTypeAnnotationsPropertyName;
45
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getBasePackagePropertyName;
56
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getSanitizedFileName;
67
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getSkipFormModelPropertyName;
@@ -79,6 +80,9 @@ protected void generate(final Config config, final Path openApiFilePath, final P
7980
config.getOptionalValue(getSkipFormModelPropertyName(openApiFilePath), String.class)
8081
.ifPresent(generator::withSkipFormModelConfig);
8182

83+
config.getOptionalValue(getAdditionalModelTypeAnnotationsPropertyName(openApiFilePath), String.class)
84+
.ifPresent(generator::withAdditionalModelTypeAnnotationsConfig);
85+
8286
generator.generate(basePackage);
8387
}
8488

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapper.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static java.lang.Boolean.FALSE;
77
import static java.lang.Boolean.TRUE;
88
import static java.util.Objects.requireNonNull;
9+
import static org.openapitools.codegen.languages.AbstractJavaCodegen.ADDITIONAL_MODEL_TYPE_ANNOTATIONS;
910

1011
import java.io.File;
1112
import java.nio.file.Path;
@@ -103,6 +104,19 @@ public OpenApiClientGeneratorWrapper withSkipFormModelConfig(final String skipFo
103104
return this;
104105
}
105106

107+
/**
108+
* Sets the global 'additionalModelTypeAnnotations' setting. If not set this setting will default to empty.
109+
*
110+
* @param additionalModelTypeAnnotations the list of extra additional annotations to be included in a model
111+
* @return this wrapper
112+
*/
113+
public OpenApiClientGeneratorWrapper withAdditionalModelTypeAnnotationsConfig(final String additionalModelTypeAnnotations) {
114+
if (additionalModelTypeAnnotations != null) {
115+
this.configurator.addAdditionalProperty(ADDITIONAL_MODEL_TYPE_ANNOTATIONS, additionalModelTypeAnnotations);
116+
}
117+
return this;
118+
}
119+
106120
public List<File> generate(final String basePackage) {
107121
this.basePackage = basePackage;
108122
this.consolidatePackageNames();
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
{#for annon in m.additionalModelTypeAnnotations.orEmpty}{annon}{/for}
1+
{#for annon in additionalModelTypeAnnotations.orEmpty}
2+
{annon}
3+
{/for}

deployment/src/test/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapperTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,31 @@ void verifyMultipartPojoGeneratedAndFieldsHaveAnnotations() throws URISyntaxExce
235235
});
236236
}
237237

238+
@Test
239+
void verifyAdditionalModelTypeAnnotations() throws URISyntaxException {
240+
List<File> generatedFiles = createGeneratorWrapper("petstore-openapi.json")
241+
.withAdditionalModelTypeAnnotationsConfig("@org.test.Foo;@org.test.Bar")
242+
.generate("org.additionalmodeltypeannotations");
243+
assertFalse(generatedFiles.isEmpty());
244+
245+
generatedFiles.stream()
246+
.filter(file -> file.getPath().matches(".*/model/.*.java"))
247+
.forEach(file -> verifyModelAdditionalAnnotations(file));
248+
}
249+
250+
private void verifyModelAdditionalAnnotations(File file) {
251+
try {
252+
CompilationUnit compilationUnit = StaticJavaParser.parse(file);
253+
compilationUnit.findAll(ClassOrInterfaceDeclaration.class)
254+
.forEach(c -> {
255+
assertThat(c.getAnnotationByName("Foo")).isPresent();
256+
assertThat(c.getAnnotationByName("Bar")).isPresent();
257+
});
258+
} catch (FileNotFoundException e) {
259+
throw new RuntimeException(e.getMessage());
260+
}
261+
}
262+
238263
private List<File> generateRestClientFiles() throws URISyntaxException {
239264
OpenApiClientGeneratorWrapper generatorWrapper = createGeneratorWrapper("simple-openapi.json")
240265
.withCircuitBreakerConfig(Map.of(

integration-tests/example-project/src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ org.acme.openapi.simple.api.DefaultApi/byeGet/CircuitBreaker/successThreshold =
2525

2626

2727
quarkus.openapi-generator.codegen.spec.multipart_requests_yml.base-package=org.acme.openapi.multipart
28+
quarkus.openapi-generator.codegen.spec.multipart_requests_yml.additional-model-type-annotations=@io.quarkus.runtime.annotations.RegisterForReflection
2829
# By default the openapi-generator doesn't generate models for multipart requests
2930
quarkus.openapi-generator.codegen.spec.multipart_requests_yml.skip-form-model=false

0 commit comments

Comments
 (0)