Skip to content

Commit d5e21cf

Browse files
boyi01ch02behhbelmiro
authored
[issue: #651] Adding initialization for non requiered containers in add/put method (#654)
* Adding initialization for non requiered containers in add/put method * Adding test * Fix typo. --------- Co-authored-by: ch02beh <[email protected]> Co-authored-by: Helber Belmiro <[email protected]>
1 parent 1c41fa9 commit d5e21cf

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,18 @@ public class {m.classname} {#if m.parent}extends {m.parent}{/if}{#if m.serializa
107107
}
108108
{#if v.isArray}
109109
public {m.classname} add{v.nameInCamelCase}Item({v.items.datatypeWithEnum} {v.name}Item) {
110+
if (this.{v.name} == null){
111+
{v.name} = {#if v.defaultValue}{v.defaultValue}{#else}new {#if v.uniqueItems}LinkedHashSet{/if}{#if !v.uniqueItems}ArrayList{/if}<>(){/if};
112+
}
110113
this.{v.name}.add({v.name}Item);
111114
return this;
112115
}
113116
{/if}
114117
{#if v.isMap}
115118
public {m.classname} put{v.nameInCamelCase}Item(String key, {v.items.datatypeWithEnum} {v.name}Item) {
119+
if (this.{v.name} == null){
120+
{v.name} = {#if v.defaultValue}{v.defaultValue}{#else}new HashMap<>(){/if};
121+
}
116122
this.{v.name}.put(key, {v.name}Item);
117123
return this;
118124
}

integration-tests/array-enum/src/main/openapi/array-enum.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ paths:
3030
components:
3131
schemas:
3232
webhook_create_update_payload:
33+
required: [url, subscriptions]
3334
type: object
3435
properties:
3536
url:
@@ -46,4 +47,13 @@ components:
4647
- message_created
4748
- message_updated
4849
- webwidget_triggered
49-
description: The events you want to subscribe to.
50+
description: The events you want to subscribe to.
51+
message:
52+
type: array
53+
items:
54+
type: string
55+
description: Non required Field to safe messages
56+
messageMap:
57+
type: object
58+
additionalProperties: true
59+
description: Non required Map Field to safe messages

integration-tests/array-enum/src/test/java/io/quarkiverse/openapi/generator/it/ArrayEnumTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.eclipse.microprofile.rest.client.inject.RestClient;
88
import org.junit.jupiter.api.Test;
99
import org.openapi.quarkus.array_enum_yaml.api.ArrayEnumResourceApi;
10+
import org.openapi.quarkus.array_enum_yaml.model.WebhookCreateUpdatePayload;
1011

1112
import io.quarkus.test.junit.QuarkusTest;
1213

@@ -21,4 +22,18 @@ class ArrayEnumTest {
2122
void apiIsBeingGenerated() {
2223
assertThat(api).isNotNull();
2324
}
25+
26+
@Test
27+
void modelNonRequiredFieldTest() {
28+
WebhookCreateUpdatePayload webhookCreateUpdatePayload = new WebhookCreateUpdatePayload();
29+
30+
assertThat(webhookCreateUpdatePayload.getMessage()).isNull();
31+
assertThat(webhookCreateUpdatePayload.getMessageMap()).isNull();
32+
33+
webhookCreateUpdatePayload.addMessageItem("Test");
34+
webhookCreateUpdatePayload.putMessageMapItem("Test", "Test");
35+
36+
assertThat(webhookCreateUpdatePayload.getMessage()).isNotNull();
37+
assertThat(webhookCreateUpdatePayload.getMessageMap()).isNotNull();
38+
}
2439
}

0 commit comments

Comments
 (0)