Skip to content

Commit bf04196

Browse files
fkischewski99Sebastian Brunefrederikkischewski
authored
Add initialize empty collection parameter (#1129)
* Add initialize empty collection parameter * Fixed wrong spec name and wrong import in initialize-empty-collection test --------- Co-authored-by: Sebastian Brune <[email protected]> Co-authored-by: Frederik Kischewski <[email protected]>
1 parent 5a397ae commit bf04196

File tree

16 files changed

+851
-1
lines changed

16 files changed

+851
-1
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ enum ConfigName {
6969
PART_FILENAME_VALUE("part-filename-value"),
7070
USE_FIELD_NAME_IN_PART_FILENAME("use-field-name-in-part-filename"),
7171
ADDITIONAL_PROPERTIES_AS_ATTRIBUTE("additional-properties-as-attribute"),
72+
INITIALIZE_EMPTY_COLLECTIONS("initialize-empty-collections"),
7273
ADDITIONAL_REQUEST_ARGS("additional-request-args"),
7374
REMOVE_OPERATION_ID_PREFIX("remove-operation-id-prefix"),
7475
REMOVE_OPERATION_ID_PREFIX_DELIMITER("remove-operation-id-prefix-delimiter"),

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/CommonItemConfig.java

+6
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,10 @@ public interface CommonItemConfig {
183183
*/
184184
@WithName("additional-properties-as-attribute")
185185
Optional<String> additionalPropertiesAsAttribute();
186+
187+
/**
188+
* Initialise collections as empty instead of null
189+
*/
190+
@WithName("initialize-empty-collections")
191+
Optional<String> initializeEmptyCollections();
186192
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ protected void generate(OpenApiGeneratorOptions options) {
335335
.ifPresent(generator::withMutinyReturnTypes);
336336

337337
generator.withAdditionalPropertiesAsAttribute(additionalPropertiesAsAttribute);
338+
339+
Boolean initialiseEmptyCollection = getValues(smallRyeConfig, openApiFilePath,
340+
CodegenConfig.ConfigName.INITIALIZE_EMPTY_COLLECTIONS, Boolean.class)
341+
.orElse(Boolean.FALSE);
342+
generator.withInitializeEmptyCollections(initialiseEmptyCollection);
343+
338344
GlobalSettings.setProperty(
339345
OpenApiClientGeneratorWrapper.SUPPORTS_ADDITIONAL_PROPERTIES_AS_ATTRIBUTE,
340346
additionalPropertiesAsAttribute.toString());

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

+5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ private void setDefaults() {
8989
// Set default values directly here
9090
this.configurator.addAdditionalProperty("additionalApiTypeAnnotations", new String[0]);
9191
this.configurator.addAdditionalProperty("additionalPropertiesAsAttribute", FALSE);
92+
this.configurator.addAdditionalProperty("initializeEmptyCollections", FALSE);
9293
this.configurator.addAdditionalProperty("additionalEnumTypeUnexpectedMember", FALSE);
9394
this.configurator.addAdditionalProperty("additionalEnumTypeUnexpectedMemberName", "");
9495
this.configurator.addAdditionalProperty("additionalEnumTypeUnexpectedMemberStringValue", "");
@@ -368,6 +369,10 @@ public void withAdditionalPropertiesAsAttribute(final Boolean enable) {
368369
this.configurator.addAdditionalProperty("additionalPropertiesAsAttribute", Optional.ofNullable(enable).orElse(FALSE));
369370
}
370371

372+
public void withInitializeEmptyCollections(final Boolean enable) {
373+
this.configurator.addAdditionalProperty("initializeEmptyCollections", Optional.ofNullable(enable).orElse(FALSE));
374+
}
375+
371376
public static String transformToSafeConfigKey(String input) {
372377
return input.replaceAll("[^a-zA-Z0-9\\-_]", "_");
373378
}

client/deployment/src/main/resources/templates/libraries/microprofile/pojo.qute

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class {m.classname} {#if m.parent}extends {m.parent}{/if}{#if serializabl
3939
{/if}
4040
{#if v.hasValidation || v.required}{#include beanValidation.qute p=v/}{/if}
4141
{#if v.isContainer}
42-
private {v.datatypeWithEnum} {v.name}{#if v.required && v.defaultValue} = {v.defaultValue}{/if};
42+
private {v.datatypeWithEnum} {v.name}{#if v.required && v.defaultValue} = {v.defaultValue}{#else if v.isArray && initializeEmptyCollections} = new {#if v.uniqueItems}LinkedHashSet{/if}{#if !v.uniqueItems}ArrayList{/if}<>(){#else if v.isMap && initializeEmptyCollections} = new HashMap<>(){/if};
4343
{#else}
4444
private {v.datatypeWithEnum} {v.name}{#if v.defaultValue} = {v.defaultValue}{/if};
4545
{/if}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<parent>
4+
<artifactId>quarkus-openapi-generator-integration-tests</artifactId>
5+
<groupId>io.quarkiverse.openapi.generator</groupId>
6+
<version>3.0.0-SNAPSHOT</version>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
10+
<artifactId>quarkus-openapi-generator-it-initialize-empty-collections</artifactId>
11+
<name>Quarkus - OpenAPI Generator - Integration Tests - Client - Initialize Empty Collections</name>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>io.quarkiverse.openapi.generator</groupId>
16+
<artifactId>quarkus-openapi-generator</artifactId>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.assertj</groupId>
20+
<artifactId>assertj-core</artifactId>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>io.quarkus</groupId>
25+
<artifactId>quarkus-junit5</artifactId>
26+
<scope>test</scope>
27+
</dependency>
28+
</dependencies>
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<groupId>io.quarkus</groupId>
33+
<artifactId>quarkus-maven-plugin</artifactId>
34+
<extensions>true</extensions>
35+
<executions>
36+
<execution>
37+
<goals>
38+
<goal>build</goal>
39+
<goal>generate-code</goal>
40+
<goal>generate-code-tests</goal>
41+
</goals>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
<profiles>
48+
<profile>
49+
<id>native-image</id>
50+
<activation>
51+
<property>
52+
<name>native</name>
53+
</property>
54+
</activation>
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<artifactId>maven-surefire-plugin</artifactId>
59+
<configuration>
60+
<skipTests>${native.surefire.skip}</skipTests>
61+
</configuration>
62+
</plugin>
63+
<plugin>
64+
<artifactId>maven-failsafe-plugin</artifactId>
65+
<executions>
66+
<execution>
67+
<goals>
68+
<goal>integration-test</goal>
69+
<goal>verify</goal>
70+
</goals>
71+
<configuration>
72+
<systemPropertyVariables>
73+
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
74+
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
75+
<maven.home>${maven.home}</maven.home>
76+
</systemPropertyVariables>
77+
</configuration>
78+
</execution>
79+
</executions>
80+
</plugin>
81+
</plugins>
82+
</build>
83+
<properties>
84+
<quarkus.package.type>native</quarkus.package.type>
85+
</properties>
86+
</profile>
87+
</profiles>
88+
89+
</project>

0 commit comments

Comments
 (0)