diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache index 60f469641ce0..20cf583a8085 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache @@ -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}}; diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java index 82b92f0082e7..0645b61eafb5 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java @@ -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(); } @@ -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()