Skip to content

[main-lts] Add initialize empty collection parameter #1131

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

Open
wants to merge 1 commit into
base: main-lts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ enum ConfigName {
PART_FILENAME_VALUE("part-filename-value"),
USE_FIELD_NAME_IN_PART_FILENAME("use-field-name-in-part-filename"),
ADDITIONAL_PROPERTIES_AS_ATTRIBUTE("additional-properties-as-attribute"),
INITIALIZE_EMPTY_COLLECTIONS("initialize-empty-collections"),
ADDITIONAL_REQUEST_ARGS("additional-request-args"),
REMOVE_OPERATION_ID_PREFIX("remove-operation-id-prefix"),
REMOVE_OPERATION_ID_PREFIX_DELIMITER("remove-operation-id-prefix-delimiter"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,10 @@ public interface CommonItemConfig {
*/
@WithName("additional-properties-as-attribute")
Optional<String> additionalPropertiesAsAttribute();

/**
* Initialise collections as empty instead of null
*/
@WithName("initialize-empty-collections")
Optional<String> initializeEmptyCollections();
}
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ protected void generate(OpenApiGeneratorOptions options) {
.ifPresent(generator::withMutinyReturnTypes);

generator.withAdditionalPropertiesAsAttribute(additionalPropertiesAsAttribute);

Boolean initialiseEmptyCollection = getValues(smallRyeConfig, openApiFilePath,
CodegenConfig.ConfigName.INITIALIZE_EMPTY_COLLECTIONS, Boolean.class)
.orElse(Boolean.FALSE);
generator.withInitializeEmptyCollections(initialiseEmptyCollection);

GlobalSettings.setProperty(
OpenApiClientGeneratorWrapper.SUPPORTS_ADDITIONAL_PROPERTIES_AS_ATTRIBUTE,
additionalPropertiesAsAttribute.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ private void setDefaults() {
// Set default values directly here
this.configurator.addAdditionalProperty("additionalApiTypeAnnotations", new String[0]);
this.configurator.addAdditionalProperty("additionalPropertiesAsAttribute", FALSE);
this.configurator.addAdditionalProperty("initializeEmptyCollections", FALSE);
this.configurator.addAdditionalProperty("additionalEnumTypeUnexpectedMember", FALSE);
this.configurator.addAdditionalProperty("additionalEnumTypeUnexpectedMemberName", "");
this.configurator.addAdditionalProperty("additionalEnumTypeUnexpectedMemberStringValue", "");
Expand Down Expand Up @@ -368,6 +369,10 @@ public void withAdditionalPropertiesAsAttribute(final Boolean enable) {
this.configurator.addAdditionalProperty("additionalPropertiesAsAttribute", Optional.ofNullable(enable).orElse(FALSE));
}

public void withInitializeEmptyCollections(final Boolean enable) {
this.configurator.addAdditionalProperty("initializeEmptyCollections", Optional.ofNullable(enable).orElse(FALSE));
}

public static String transformToSafeConfigKey(String input) {
return input.replaceAll("[^a-zA-Z0-9\\-_]", "_");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class {m.classname} {#if m.parent}extends {m.parent}{/if}{#if serializabl
{/if}
{#if v.hasValidation || v.required}{#include beanValidation.qute p=v/}{/if}
{#if v.isContainer}
private {v.datatypeWithEnum} {v.name}{#if v.required && v.defaultValue} = {v.defaultValue}{/if};
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};
{#else}
private {v.datatypeWithEnum} {v.name}{#if v.defaultValue} = {v.defaultValue}{/if};
{/if}
Expand Down
89 changes: 89 additions & 0 deletions client/integration-tests/initialize-empty-collections/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>quarkus-openapi-generator-integration-tests</artifactId>
<groupId>io.quarkiverse.openapi.generator</groupId>
<version>3.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-openapi-generator-it-initialize-empty-collections</artifactId>
<name>Quarkus - OpenAPI Generator - Integration Tests - Client - Initialize Empty Collections</name>

<dependencies>
<dependency>
<groupId>io.quarkiverse.openapi.generator</groupId>
<artifactId>quarkus-openapi-generator</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${native.surefire.skip}</skipTests>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>

</project>
Loading
Loading