diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/dataTypeGetter.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/dataTypeGetter.mustache new file mode 100644 index 000000000000..7fc5bdee2436 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/JavaSpring/dataTypeGetter.mustache @@ -0,0 +1 @@ +{{^useOptional}}{{>nullableDataType}}{{/useOptional}}{{#useOptional}}{{>optionalDataTypeGetter}}{{/useOptional}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/dataTypePojo.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/dataTypePojo.mustache new file mode 100644 index 000000000000..55277f3d0994 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/JavaSpring/dataTypePojo.mustache @@ -0,0 +1 @@ +{{^useOptional}}{{>nullableDataType}}{{/useOptional}}{{#useOptional}}{{>optionalPojoDataType}}{{/useOptional}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/defaultValuePojo.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/defaultValuePojo.mustache new file mode 100644 index 000000000000..e4158c69ee57 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/JavaSpring/defaultValuePojo.mustache @@ -0,0 +1 @@ +{{^useOptional}}{{>nullableDefaultValue}}{{/useOptional}}{{#useOptional}}{{>optionalDefaultValue}}{{/useOptional}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-cloud/pom.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-cloud/pom.mustache index 4e1f61b8ae35..929a02a2d8f3 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-cloud/pom.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-cloud/pom.mustache @@ -81,11 +81,13 @@ 2.6.4 {{/threetenbp}} + {{^useOptional}} org.openapitools jackson-databind-nullable 0.1.0 + {{/useOptional}} org.springframework.boot spring-boot-starter-test diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/model.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/model.mustache index 60d7d41fe48d..ce9b211fdaeb 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/model.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/model.mustache @@ -3,7 +3,9 @@ package {{package}}; import java.util.Objects; {{#imports}}import {{import}}; {{/imports}} +{{^useOptional}} import org.openapitools.jackson.nullable.JsonNullable; +{{/useOptional}} {{#serializableModel}} import java.io.Serializable; {{/serializableModel}} @@ -14,6 +16,9 @@ import javax.validation.constraints.*; {{#performBeanValidation}} import org.hibernate.validator.constraints.*; {{/performBeanValidation}} +{{#useOptional}} +import java.util.Optional; +{{/useOptional}} {{#jackson}} {{#withXml}} import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/nullableDefaultValue.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/nullableDefaultValue.mustache new file mode 100644 index 000000000000..01f8e2b1131e --- /dev/null +++ b/modules/openapi-generator/src/main/resources/JavaSpring/nullableDefaultValue.mustache @@ -0,0 +1 @@ +{{#isNullable}} = JsonNullable.undefined(){{/isNullable}}{{^isNullable}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}{{/isNullable}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/optionalBeanValidation.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/optionalBeanValidation.mustache new file mode 100644 index 000000000000..8a2d2337ca7c --- /dev/null +++ b/modules/openapi-generator/src/main/resources/JavaSpring/optionalBeanValidation.mustache @@ -0,0 +1 @@ +{{^isContainer}}{{^isPrimitiveType}}@Valid {{/isPrimitiveType}}{{/isContainer}}{{>beanValidationCore}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/optionalDataTypeGetter.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/optionalDataTypeGetter.mustache new file mode 100644 index 000000000000..cacb6acd2143 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/JavaSpring/optionalDataTypeGetter.mustache @@ -0,0 +1 @@ +{{#isContainer}}{{{datatypeWithEnum}}}{{/isContainer}}{{^isContainer}}{{#required}}{{{dataType}}}{{/required}}{{^required}}{{#useBeanValidation}}Optional<{{>optionalBeanValidation}}{{{dataType}}}>{{/useBeanValidation}}{{^useBeanValidation}}Optional<{{{dataType}}}>{{/useBeanValidation}}{{/required}}{{/isContainer}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/optionalDefaultValue.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/optionalDefaultValue.mustache new file mode 100644 index 000000000000..a110b11a6d7c --- /dev/null +++ b/modules/openapi-generator/src/main/resources/JavaSpring/optionalDefaultValue.mustache @@ -0,0 +1 @@ +{{^isContainer}}{{^required}} = Optional.empty(){{/required}}{{/isContainer}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/optionalPojoDataType.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/optionalPojoDataType.mustache new file mode 100644 index 000000000000..206ffcc571f3 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/JavaSpring/optionalPojoDataType.mustache @@ -0,0 +1 @@ +{{#isContainer}}{{{datatypeWithEnum}}}{{/isContainer}}{{^isContainer}}{{>optionalDataType}}{{/isContainer}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache index 29c35fbc1d73..456474d3039e 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache @@ -28,15 +28,15 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}}{{^parent}} {{/gson}} {{#isContainer}} {{#useBeanValidation}}@Valid{{/useBeanValidation}} - private {{>nullableDataType}} {{name}} = {{#isNullable}}JsonNullable.undefined(){{/isNullable}}{{^isNullable}}{{#required}}{{{defaultValue}}}{{/required}}{{^required}}null{{/required}}{{/isNullable}}; + private {{>dataTypePojo}} {{name}} = {{#isNullable}}JsonNullable.undefined(){{/isNullable}}{{^isNullable}}{{#required}}{{{defaultValue}}}{{/required}}{{^required}}null{{/required}}{{/isNullable}}; {{/isContainer}} {{^isContainer}} - private {{>nullableDataType}} {{name}}{{#isNullable}} = JsonNullable.undefined(){{/isNullable}}{{^isNullable}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}{{/isNullable}}; + private {{>dataTypePojo}} {{name}}{{>defaultValuePojo}}; {{/isContainer}} {{/vars}} {{#vars}} - public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) { + public {{classname}} {{name}}({{>dataTypePojo}} {{name}}) { this.{{name}} = {{#isNullable}}JsonNullable.of({{name}}){{/isNullable}}{{^isNullable}}{{name}}{{/isNullable}}; return this; } @@ -84,11 +84,11 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}}{{^parent}} {{{vendorExtensions.extraAnnotation}}} {{/vendorExtensions.extraAnnotation}} @ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}") -{{#useBeanValidation}}{{>beanValidation}}{{/useBeanValidation}} public {{>nullableDataType}} {{getter}}() { +{{#useBeanValidation}}{{#useOptional}}{{#required}}{{>beanValidation}}{{/required}}{{/useOptional}}{{/useBeanValidation}}{{#useBeanValidation}}{{^useOptional}}{{>beanValidation}}{{/useOptional}}{{/useBeanValidation}} public {{>dataTypeGetter}} {{getter}}() { return {{name}}; } - public void {{setter}}({{>nullableDataType}} {{name}}) { + public void {{setter}}({{>dataTypePojo}} {{name}}) { this.{{name}} = {{name}}; }