Skip to content

Commit

Permalink
* Uses Optional.ofNullable() at the fluent setters to prevent NPE
Browse files Browse the repository at this point in the history
* Fixes issue #17538
  • Loading branch information
slobodator authored and aslobodyanyk-wio committed Jan 7, 2025
1 parent cba756f commit a5f66c9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{! begin feature: fluent setter methods }}
public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) {
{{#openApiNullable}}
this.{{name}} = {{#isNullable}}JsonNullable.of({{/isNullable}}{{#useOptional}}{{^required}}{{^isNullable}}{{^isContainer}}Optional.of({{/isContainer}}{{/isNullable}}{{/required}}{{/useOptional}}{{name}}{{#isNullable}}){{/isNullable}}{{#useOptional}}{{^required}}{{^isNullable}}{{^isContainer}}){{/isContainer}}{{/isNullable}}{{/required}}{{/useOptional}};
this.{{name}} = {{#isNullable}}JsonNullable.of({{/isNullable}}{{#useOptional}}{{^required}}{{^isNullable}}{{^isContainer}}Optional.ofNullable({{/isContainer}}{{/isNullable}}{{/required}}{{/useOptional}}{{name}}{{#isNullable}}){{/isNullable}}{{#useOptional}}{{^required}}{{^isNullable}}{{^isContainer}}){{/isContainer}}{{/isNullable}}{{/required}}{{/useOptional}};
{{/openApiNullable}}
{{^openApiNullable}}
this.{{name}} = {{name}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public void doAnnotateDatesOnModelParametersWithOptionalAndJsonNullable() throws
.containsWithNameAndAttributes("DateTimeFormat", ImmutableMap.of("iso", "DateTimeFormat.ISO.DATE_TIME"))
.toProperty().toType()
.assertMethod("born", "LocalDate")
.bodyContainsLines("this.born = Optional.of(born)")
.bodyContainsLines("this.born = Optional.ofNullable(born)")
.doesNotHaveComment();
}

Expand Down Expand Up @@ -4397,9 +4397,10 @@ private void assertJsonNullableMethod(JavaFileAssert javaFileAssert, String type

private void assertWrapperMethod(JavaFileAssert javaFileAssert, String wrapperType, String type, String expectedName, String getterReturnType){
String methodName = StringUtils.capitalize(expectedName);
var of = wrapperType.equals("Optional") ? "ofNullable" : "of";
javaFileAssert.assertMethod(expectedName)
.hasReturnType("Animal")
.bodyContainsLines("this."+expectedName+" = "+wrapperType+".of("+expectedName+");", "return this;")
.bodyContainsLines("this." + expectedName + " = "+wrapperType+ "." + of + "(" +expectedName+");", "return this;")
.assertParameter(expectedName)
.hasType(type)
.toMethod()
Expand Down

0 comments on commit a5f66c9

Please sign in to comment.