Skip to content

Commit 9499094

Browse files
committed
fix(jaxrs): correct add/remove methods for JsonNullable<List<T>> fields #23251
Signed-off-by: TheNevim <puk.dominik212@gmail.com>
1 parent 7ddd693 commit 9499094

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

  • modules/openapi-generator/src/main/resources/JavaJaxRS/spec

modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,36 +173,84 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens
173173

174174
{{#isArray}}
175175
public {{classname}} add{{nameInPascalCase}}Item({{{items.datatypeWithEnum}}} {{name}}Item) {
176+
{{#vendorExtensions.x-is-jackson-optional-nullable}}
177+
if (this.{{name}} == null || !this.{{name}}.isPresent() || this.{{name}}.get() == null) {
178+
this.{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{{defaultValue}}}{{^defaultValue}}new {{#uniqueItems}}LinkedHashSet{{/uniqueItems}}{{^uniqueItems}}ArrayList{{/uniqueItems}}<>(){{/defaultValue}});
179+
}
180+
try {
181+
this.{{name}}.get().add({{name}}Item);
182+
} catch (java.util.NoSuchElementException e) {
183+
// this can never happen, as we make sure above that the value is present
184+
}
185+
return this;
186+
{{/vendorExtensions.x-is-jackson-optional-nullable}}
187+
{{^vendorExtensions.x-is-jackson-optional-nullable}}
176188
if (this.{{name}} == null) {
177189
this.{{name}} = {{{defaultValue}}}{{^defaultValue}}new {{#uniqueItems}}LinkedHashSet{{/uniqueItems}}{{^uniqueItems}}ArrayList{{/uniqueItems}}<>(){{/defaultValue}};
178190
}
179191

180192
this.{{name}}.add({{name}}Item);
181193
return this;
194+
{{/vendorExtensions.x-is-jackson-optional-nullable}}
182195
}
183196

184197
public {{classname}} remove{{nameInPascalCase}}Item({{{items.datatypeWithEnum}}} {{name}}Item) {
198+
{{#vendorExtensions.x-is-jackson-optional-nullable}}
199+
if ({{name}}Item != null && this.{{name}} != null && this.{{name}}.isPresent()) {
200+
try {
201+
this.{{name}}.get().remove({{name}}Item);
202+
} catch (java.util.NoSuchElementException e) {
203+
// this can never happen, as we make sure above that the value is present
204+
}
205+
}
206+
{{/vendorExtensions.x-is-jackson-optional-nullable}}
207+
{{^vendorExtensions.x-is-jackson-optional-nullable}}
185208
if ({{name}}Item != null && this.{{name}} != null) {
186209
this.{{name}}.remove({{name}}Item);
187210
}
211+
{{/vendorExtensions.x-is-jackson-optional-nullable}}
188212

189213
return this;
190214
}
191215
{{/isArray}}
192216
{{#isMap}}
193217
public {{classname}} put{{nameInPascalCase}}Item(String key, {{{items.datatypeWithEnum}}} {{name}}Item) {
218+
{{#vendorExtensions.x-is-jackson-optional-nullable}}
219+
if (this.{{name}} == null || !this.{{name}}.isPresent() || this.{{name}}.get() == null) {
220+
this.{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{{defaultValue}}}{{^defaultValue}}new HashMap<>(){{/defaultValue}});
221+
}
222+
try {
223+
this.{{name}}.get().put(key, {{name}}Item);
224+
} catch (java.util.NoSuchElementException e) {
225+
// this can never happen, as we make sure above that the value is present
226+
}
227+
return this;
228+
{{/vendorExtensions.x-is-jackson-optional-nullable}}
229+
{{^vendorExtensions.x-is-jackson-optional-nullable}}
194230
if (this.{{name}} == null) {
195231
this.{{name}} = {{{defaultValue}}}{{^defaultValue}}new HashMap<>(){{/defaultValue}};
196232
}
197233

198234
this.{{name}}.put(key, {{name}}Item);
199235
return this;
236+
{{/vendorExtensions.x-is-jackson-optional-nullable}}
200237
}
201238

202239
public {{classname}} remove{{nameInPascalCase}}Item(String key) {
240+
{{#vendorExtensions.x-is-jackson-optional-nullable}}
241+
if (this.{{name}} != null && this.{{name}}.isPresent()) {
242+
try {
243+
this.{{name}}.get().remove(key);
244+
} catch (java.util.NoSuchElementException e) {
245+
// this can never happen, as we make sure above that the value is present
246+
}
247+
}
248+
{{/vendorExtensions.x-is-jackson-optional-nullable}}
249+
{{^vendorExtensions.x-is-jackson-optional-nullable}}
203250
if (this.{{name}} != null) {
204251
this.{{name}}.remove(key);
205252
}
253+
{{/vendorExtensions.x-is-jackson-optional-nullable}}
206254

207255
return this;
208256
}
@@ -286,4 +334,4 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens
286334
}
287335
{{/vars}}
288336
}{{/additionalProperties}}{{/generateBuilders}}
289-
}
337+
}

0 commit comments

Comments
 (0)