From 242a1cdcdbfe5273470c1aa328d641aa7026a17e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophane=20CHARBONNIER?= Date: Mon, 4 Sep 2023 15:57:47 +0200 Subject: [PATCH] [java] Microprofile - fix optional array property's default value --- .../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 1c618e90299d..65da41f74208 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 74baeba53b11..76f7cf9ad73c 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 Pet photoUrls(List photoUrls) { } public Pet addPhotoUrlsItem(String photoUrlsItem) { + if (this.photoUrls == null) { + this.photoUrls = new ArrayList<>(); + } this.photoUrls.add(photoUrlsItem); return this; } @@ -252,6 +255,9 @@ public Pet tags(List tags) { } 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 5777e28a7ea6..79e2385dcf60 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 Pet photoUrls(List photoUrls) { } public Pet addPhotoUrlsItem(String photoUrlsItem) { + if (this.photoUrls == null) { + this.photoUrls = new ArrayList<>(); + } this.photoUrls.add(photoUrlsItem); return this; } @@ -224,6 +227,9 @@ public Pet tags(List tags) { } 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 50c276ac70af..50d982f0be7f 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 Pet photoUrls(List photoUrls) { } public Pet addPhotoUrlsItem(String photoUrlsItem) { + if (this.photoUrls == null) { + this.photoUrls = new ArrayList<>(); + } this.photoUrls.add(photoUrlsItem); return this; } @@ -203,6 +206,9 @@ public Pet tags(List tags) { } 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 f3d73bffa7a2..c0d7cc69efaa 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 Pet photoUrls(List photoUrls) { } public Pet addPhotoUrlsItem(String photoUrlsItem) { + if (this.photoUrls == null) { + this.photoUrls = new ArrayList<>(); + } this.photoUrls.add(photoUrlsItem); return this; } @@ -203,6 +206,9 @@ public Pet tags(List tags) { } public Pet addTagsItem(Tag tagsItem) { + if (this.tags == null) { + this.tags = new ArrayList<>(); + } this.tags.add(tagsItem); return this; }