From 4418b59b4759107f5abd4f39c5d4ff725659f15f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophane=20Charbonnier?= <1663420+hightea@users.noreply.github.com> Date: Tue, 5 Sep 2023 04:15:10 +0200 Subject: [PATCH] [java] Microprofile - fix optional array property's default value (#16500) --- .../resources/Java/libraries/microprofile/pojo.mustache | 6 ++++++ .../src/main/java/org/openapitools/client/model/Pet.java | 6 ++++++ .../src/main/java/org/openapitools/client/model/Pet.java | 6 ++++++ .../src/main/java/org/openapitools/client/model/Pet.java | 6 ++++++ .../src/main/java/org/openapitools/client/model/Pet.java | 6 ++++++ 5 files changed, 30 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache index 1c618e90299..65da41f7420 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache @@ -126,6 +126,9 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}}{{#vendorExtensi {{#isArray}} public {{classname}} add{{nameInCamelCase}}Item({{{items.datatypeWithEnum}}} {{name}}Item) { + if (this.{{name}} == null) { + this.{{name}} = {{{defaultValue}}}{{^defaultValue}}new {{#uniqueItems}}LinkedHashSet{{/uniqueItems}}{{^uniqueItems}}ArrayList{{/uniqueItems}}<>(){{/defaultValue}}; + } this.{{name}}.add({{name}}Item); return this; } @@ -133,6 +136,9 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}}{{#vendorExtensi {{#isMap}} public {{classname}} put{{nameInCamelCase}}Item(String key, {{{items.datatypeWithEnum}}} {{name}}Item) { + if (this.{{name}} == null) { + this.{{name}} = {{{defaultValue}}}{{^defaultValue}}new HashMap<>(){{/defaultValue}}; + } this.{{name}}.put(key, {{name}}Item); return this; } diff --git a/samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/Pet.java b/samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/Pet.java index 74baeba53b1..76f7cf9ad73 100644 --- a/samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/Pet.java +++ b/samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml/src/main/java/org/openapitools/client/model/Pet.java @@ -217,6 +217,9 @@ public class Pet { } public Pet addPhotoUrlsItem(String photoUrlsItem) { + if (this.photoUrls == null) { + this.photoUrls = new ArrayList<>(); + } this.photoUrls.add(photoUrlsItem); return this; } @@ -252,6 +255,9 @@ public class Pet { } public Pet addTagsItem(Tag tagsItem) { + if (this.tags == null) { + this.tags = new ArrayList<>(); + } this.tags.add(tagsItem); return this; } diff --git a/samples/client/petstore/java/microprofile-rest-client-3.0-jackson/src/main/java/org/openapitools/client/model/Pet.java b/samples/client/petstore/java/microprofile-rest-client-3.0-jackson/src/main/java/org/openapitools/client/model/Pet.java index 5777e28a7ea..79e2385dcf6 100644 --- a/samples/client/petstore/java/microprofile-rest-client-3.0-jackson/src/main/java/org/openapitools/client/model/Pet.java +++ b/samples/client/petstore/java/microprofile-rest-client-3.0-jackson/src/main/java/org/openapitools/client/model/Pet.java @@ -195,6 +195,9 @@ public class Pet { } public Pet addPhotoUrlsItem(String photoUrlsItem) { + if (this.photoUrls == null) { + this.photoUrls = new ArrayList<>(); + } this.photoUrls.add(photoUrlsItem); return this; } @@ -224,6 +227,9 @@ public class Pet { } public Pet addTagsItem(Tag tagsItem) { + if (this.tags == null) { + this.tags = new ArrayList<>(); + } this.tags.add(tagsItem); return this; } diff --git a/samples/client/petstore/java/microprofile-rest-client-3.0/src/main/java/org/openapitools/client/model/Pet.java b/samples/client/petstore/java/microprofile-rest-client-3.0/src/main/java/org/openapitools/client/model/Pet.java index 50c276ac70a..50d982f0be7 100644 --- a/samples/client/petstore/java/microprofile-rest-client-3.0/src/main/java/org/openapitools/client/model/Pet.java +++ b/samples/client/petstore/java/microprofile-rest-client-3.0/src/main/java/org/openapitools/client/model/Pet.java @@ -178,6 +178,9 @@ public class Pet { } public Pet addPhotoUrlsItem(String photoUrlsItem) { + if (this.photoUrls == null) { + this.photoUrls = new ArrayList<>(); + } this.photoUrls.add(photoUrlsItem); return this; } @@ -203,6 +206,9 @@ public class Pet { } public Pet addTagsItem(Tag tagsItem) { + if (this.tags == null) { + this.tags = new ArrayList<>(); + } this.tags.add(tagsItem); return this; } diff --git a/samples/client/petstore/java/microprofile-rest-client/src/main/java/org/openapitools/client/model/Pet.java b/samples/client/petstore/java/microprofile-rest-client/src/main/java/org/openapitools/client/model/Pet.java index f3d73bffa7a..c0d7cc69efa 100644 --- a/samples/client/petstore/java/microprofile-rest-client/src/main/java/org/openapitools/client/model/Pet.java +++ b/samples/client/petstore/java/microprofile-rest-client/src/main/java/org/openapitools/client/model/Pet.java @@ -178,6 +178,9 @@ public class Pet { } public Pet addPhotoUrlsItem(String photoUrlsItem) { + if (this.photoUrls == null) { + this.photoUrls = new ArrayList<>(); + } this.photoUrls.add(photoUrlsItem); return this; } @@ -203,6 +206,9 @@ public class Pet { } public Pet addTagsItem(Tag tagsItem) { + if (this.tags == null) { + this.tags = new ArrayList<>(); + } this.tags.add(tagsItem); return this; }