diff --git a/mustache-templates/src/main/resources/templates/pojo.mustache b/mustache-templates/src/main/resources/templates/pojo.mustache index f1331757..67d1b187 100644 --- a/mustache-templates/src/main/resources/templates/pojo.mustache +++ b/mustache-templates/src/main/resources/templates/pojo.mustache @@ -52,7 +52,7 @@ import java.util.Set; }}{{#vendorExtensions.x-field-extra-annotation}}{{! }}{{{.}}} {{/vendorExtensions.x-field-extra-annotation}}{{! - }}{{>useBeanValidation}}{{{datatypeWithEnum}}} {{name}}{{^-last}}, + }}{{#useOptional}}{{^required}}Optional<{{/required}}{{/useOptional}}{{>useBeanValidation}}{{{datatypeWithEnum}}}{{#useOptional}}{{^required}}>{{/required}}{{/useOptional}} {{name}}{{^-last}}, {{/-last}}{{/vars}}){{#vendorExtensions.x-implements}}{{#-first}} implements {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} { @@ -74,12 +74,21 @@ import java.util.Set; public {{classname}}( {{#vars}}final {{{datatypeWithEnum}}} {{name}}{{^-last}}, {{/-last}}{{/vars}}) { -{{#vars}} this.{{name}} = {{^defaultValue}}{{name}};{{/defaultValue}}{{! - }}{{#defaultValue}}{{! - }}{{#isArray}}Objects.requireNonNullElseGet({{name}}, () -> {{{.}}});{{/isArray}}{{! - }}{{^isArray}}Objects.requireNonNullElse({{name}}, {{{.}}});{{/isArray}}{{! - }}{{/defaultValue}} -{{/vars}} }{{#generateBuilders}}{{>generateBuilders}}{{/generateBuilders}}{{! + {{#useOptional}} + this({{#vars}}{{! + }}{{^required}}Optional.ofNullable({{/required}}{{name}}{{^required}}){{/required}}{{^-last}}, + {{/-last}}{{! + }}{{/vars}}); +{{/useOptional}}{{! +}}{{^useOptional}}{{! +}}{{#vars}}{{! +}} this.{{name}} = {{^defaultValue}}{{name}};{{/defaultValue}}{{! + }}{{#defaultValue}}{{! + }}{{#isArray}}Objects.requireNonNullElseGet({{name}}, () -> {{{.}}});{{/isArray}}{{! + }}{{^isArray}}Objects.requireNonNullElse({{name}}, {{{.}}});{{/isArray}}{{! + }}{{/defaultValue}} +{{/vars}}{{! +}}{{/useOptional}} }{{#generateBuilders}}{{>generateBuilders}}{{/generateBuilders}}{{! Generate inner enum classes }}{{#vars}}{{#isEnum}} diff --git a/mustache-templates/target/classes/spring-templates/pojo.mustache b/mustache-templates/target/classes/spring-templates/pojo.mustache index a7081509..ca2eaf50 100644 --- a/mustache-templates/target/classes/spring-templates/pojo.mustache +++ b/mustache-templates/target/classes/spring-templates/pojo.mustache @@ -52,7 +52,7 @@ import java.util.Set; }}{{#vendorExtensions.x-field-extra-annotation}}{{! }}{{{.}}} {{/vendorExtensions.x-field-extra-annotation}}{{! - }}{{>useBeanValidation}}{{{datatypeWithEnum}}} {{name}}{{^-last}}, + }}{{#useOptional}}{{^required}}Optional<{{/required}}{{/useOptional}}{{>useBeanValidation}}{{{datatypeWithEnum}}}{{#useOptional}}{{^required}}>{{/required}}{{/useOptional}} {{name}}{{^-last}}, {{/-last}}{{/vars}}){{#vendorExtensions.x-implements}}{{#-first}} implements {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} { @@ -74,12 +74,21 @@ import java.util.Set; public {{classname}}( {{#vars}}final {{{datatypeWithEnum}}} {{name}}{{^-last}}, {{/-last}}{{/vars}}) { -{{#vars}} this.{{name}} = {{^defaultValue}}{{name}};{{/defaultValue}}{{! - }}{{#defaultValue}}{{! - }}{{#isArray}}Objects.requireNonNullElseGet({{name}}, () -> {{{.}}});{{/isArray}}{{! - }}{{^isArray}}Objects.requireNonNullElse({{name}}, {{{.}}});{{/isArray}}{{! - }}{{/defaultValue}} -{{/vars}} }{{#generateBuilders}}{{>generateBuilders}}{{/generateBuilders}}{{! + {{#useOptional}} + this({{#vars}}{{! + }}{{^required}}Optional.ofNullable({{/required}}{{name}}{{^required}}){{/required}}{{^-last}}, + {{/-last}}{{! + }}{{/vars}}); +{{/useOptional}}{{! +}}{{^useOptional}}{{! +}}{{#vars}}{{! +}} this.{{name}} = {{^defaultValue}}{{name}};{{/defaultValue}}{{! + }}{{#defaultValue}}{{! + }}{{#isArray}}Objects.requireNonNullElseGet({{name}}, () -> {{{.}}});{{/isArray}}{{! + }}{{^isArray}}Objects.requireNonNullElse({{name}}, {{{.}}});{{/isArray}}{{! + }}{{/defaultValue}} +{{/vars}}{{! +}}{{/useOptional}} }{{#generateBuilders}}{{>generateBuilders}}{{/generateBuilders}}{{! Generate inner enum classes }}{{#vars}}{{#isEnum}} diff --git a/mustache-templates/target/classes/templates/pojo.mustache b/mustache-templates/target/classes/templates/pojo.mustache index a7081509..ca2eaf50 100644 --- a/mustache-templates/target/classes/templates/pojo.mustache +++ b/mustache-templates/target/classes/templates/pojo.mustache @@ -52,7 +52,7 @@ import java.util.Set; }}{{#vendorExtensions.x-field-extra-annotation}}{{! }}{{{.}}} {{/vendorExtensions.x-field-extra-annotation}}{{! - }}{{>useBeanValidation}}{{{datatypeWithEnum}}} {{name}}{{^-last}}, + }}{{#useOptional}}{{^required}}Optional<{{/required}}{{/useOptional}}{{>useBeanValidation}}{{{datatypeWithEnum}}}{{#useOptional}}{{^required}}>{{/required}}{{/useOptional}} {{name}}{{^-last}}, {{/-last}}{{/vars}}){{#vendorExtensions.x-implements}}{{#-first}} implements {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} { @@ -74,12 +74,21 @@ import java.util.Set; public {{classname}}( {{#vars}}final {{{datatypeWithEnum}}} {{name}}{{^-last}}, {{/-last}}{{/vars}}) { -{{#vars}} this.{{name}} = {{^defaultValue}}{{name}};{{/defaultValue}}{{! - }}{{#defaultValue}}{{! - }}{{#isArray}}Objects.requireNonNullElseGet({{name}}, () -> {{{.}}});{{/isArray}}{{! - }}{{^isArray}}Objects.requireNonNullElse({{name}}, {{{.}}});{{/isArray}}{{! - }}{{/defaultValue}} -{{/vars}} }{{#generateBuilders}}{{>generateBuilders}}{{/generateBuilders}}{{! + {{#useOptional}} + this({{#vars}}{{! + }}{{^required}}Optional.ofNullable({{/required}}{{name}}{{^required}}){{/required}}{{^-last}}, + {{/-last}}{{! + }}{{/vars}}); +{{/useOptional}}{{! +}}{{^useOptional}}{{! +}}{{#vars}}{{! +}} this.{{name}} = {{^defaultValue}}{{name}};{{/defaultValue}}{{! + }}{{#defaultValue}}{{! + }}{{#isArray}}Objects.requireNonNullElseGet({{name}}, () -> {{{.}}});{{/isArray}}{{! + }}{{^isArray}}Objects.requireNonNullElse({{name}}, {{{.}}});{{/isArray}}{{! + }}{{/defaultValue}} +{{/vars}}{{! +}}{{/useOptional}} }{{#generateBuilders}}{{>generateBuilders}}{{/generateBuilders}}{{! Generate inner enum classes }}{{#vars}}{{#isEnum}} diff --git a/test-spring/pom.xml b/test-spring/pom.xml index ab58bba4..283296b2 100644 --- a/test-spring/pom.xml +++ b/test-spring/pom.xml @@ -324,6 +324,18 @@ + + generate-spring-useOptional + + generate + + + ${modelPackage.default}.useOptional + + true + + + diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/DeprecatedExampleEnum.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/DeprecatedExampleEnum.java new file mode 100644 index 00000000..0136e1cb --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/DeprecatedExampleEnum.java @@ -0,0 +1,65 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonValue; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * Example of a deprecated Enum + * + * @deprecated + */ +@Deprecated +public enum DeprecatedExampleEnum { + ENUM1("ENUM1"), + ENUM2("ENUM2"), + ENUM3("ENUM3"); + + private final String value; + + DeprecatedExampleEnum(final String value) { + this.value = value; + } + + /** + * Gets the {@code value} of this enum. + * + * @return the value of this enum. + */ + @JsonValue + public String getValue() { + return value; + } + + /** + * Case-sensitively matches the given {@code value} to an enum constant using {@link #getValue()}. + * + *

NOTE: if multiple enum constants have a matching value, the first enum constant is + * returned, by the order they are declared. + * + * @param value of the enum. + * @return a {@link DeprecatedExampleEnum } with the matching value. + * @throws IllegalArgumentException if no enum has a value matching the given value. + */ + @JsonCreator + public static DeprecatedExampleEnum fromValue(final String value) { + for (final DeprecatedExampleEnum constant : DeprecatedExampleEnum.values()) { + if (constant.getValue().equals(value)) { + return constant; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/DeprecatedExampleRecord.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/DeprecatedExampleRecord.java new file mode 100644 index 00000000..c0a27cda --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/DeprecatedExampleRecord.java @@ -0,0 +1,33 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import org.springframework.lang.Nullable; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of a deprecated Record + * + * @deprecated + * @param field1 a boolean field + */ +@Deprecated +public record DeprecatedExampleRecord( + Optional field1) { + + @JsonCreator + public DeprecatedExampleRecord( + final Boolean field1) { + this(Optional.ofNullable(field1)); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleEnum.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleEnum.java new file mode 100644 index 00000000..54d5c810 --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleEnum.java @@ -0,0 +1,71 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonValue; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * Example of an Enum + */ +public enum ExampleEnum { + /** + * Some description of ENUM1 + */ + ENUM1("ENUM1"), + /** + * Some description of ENUM2 + */ + ENUM2("ENUM2"), + /** + * Some description of ENUM3 + */ + ENUM3("ENUM3"); + + private final String value; + + ExampleEnum(final String value) { + this.value = value; + } + + /** + * Gets the {@code value} of this enum. + * + * @return the value of this enum. + */ + @JsonValue + public String getValue() { + return value; + } + + /** + * Case-sensitively matches the given {@code value} to an enum constant using {@link #getValue()}. + * + *

NOTE: if multiple enum constants have a matching value, the first enum constant is + * returned, by the order they are declared. + * + * @param value of the enum. + * @return a {@link ExampleEnum } with the matching value. + * @throws IllegalArgumentException if no enum has a value matching the given value. + */ + @JsonCreator + public static ExampleEnum fromValue(final String value) { + for (final ExampleEnum constant : ExampleEnum.values()) { + if (constant.getValue().equals(value)) { + return constant; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleEnumWithIntegerValues.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleEnumWithIntegerValues.java new file mode 100644 index 00000000..8c1cc5a0 --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleEnumWithIntegerValues.java @@ -0,0 +1,64 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonValue; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * Example of an Enum with integer values + */ +public enum ExampleEnumWithIntegerValues { + NUMBER_100(100), + NUMBER_200(200), + NUMBER_300(300), + NUMBER_400(400), + NUMBER_500(500); + + private final Integer value; + + ExampleEnumWithIntegerValues(final Integer value) { + this.value = value; + } + + /** + * Gets the {@code value} of this enum. + * + * @return the value of this enum. + */ + @JsonValue + public Integer getValue() { + return value; + } + + /** + * Matches the given {@code value} to an enum constant using {@link #getValue()}. + * + *

NOTE: if multiple enum constants have a matching value, the first enum constant is + * returned, by the order they are declared. + * + * @param value of the enum. + * @return a {@link ExampleEnumWithIntegerValues } with the matching value. + * @throws IllegalArgumentException if no enum has a value matching the given value. + */ + @JsonCreator + public static ExampleEnumWithIntegerValues fromValue(final Integer value) { + for (final ExampleEnumWithIntegerValues constant : ExampleEnumWithIntegerValues.values()) { + if (constant.getValue().equals(value)) { + return constant; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleImplementsEnum.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleImplementsEnum.java new file mode 100644 index 00000000..597fd8df --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleImplementsEnum.java @@ -0,0 +1,62 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonValue; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * Example of an Enum which implements an interface. + */ +public enum ExampleImplementsEnum implements io.github.chrimle.o2jrm.interfaces.TestInterfaceOne { + ENUM1("ENUM1"), + ENUM2("ENUM2"), + ENUM3("ENUM3"); + + private final String value; + + ExampleImplementsEnum(final String value) { + this.value = value; + } + + /** + * Gets the {@code value} of this enum. + * + * @return the value of this enum. + */ + @JsonValue + public String getValue() { + return value; + } + + /** + * Case-sensitively matches the given {@code value} to an enum constant using {@link #getValue()}. + * + *

NOTE: if multiple enum constants have a matching value, the first enum constant is + * returned, by the order they are declared. + * + * @param value of the enum. + * @return a {@link ExampleImplementsEnum } with the matching value. + * @throws IllegalArgumentException if no enum has a value matching the given value. + */ + @JsonCreator + public static ExampleImplementsEnum fromValue(final String value) { + for (final ExampleImplementsEnum constant : ExampleImplementsEnum.values()) { + if (constant.getValue().equals(value)) { + return constant; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleNullableEnum.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleNullableEnum.java new file mode 100644 index 00000000..f43b0ec1 --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleNullableEnum.java @@ -0,0 +1,71 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonValue; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * Example of an Enum + */ +public enum ExampleNullableEnum { + /** + * Some description of ENUM1 + */ + ENUM1("ENUM1"), + /** + * Some description of ENUM2 + */ + ENUM2("ENUM2"), + /** + * Some description of ENUM3 + */ + ENUM3("ENUM3"); + + private final String value; + + ExampleNullableEnum(final String value) { + this.value = value; + } + + /** + * Gets the {@code value} of this enum. + * + * @return the value of this enum. + */ + @JsonValue + public String getValue() { + return value; + } + + /** + * Case-sensitively matches the given {@code value} to an enum constant using {@link #getValue()}. + * + *

NOTE: if multiple enum constants have a matching value, the first enum constant is + * returned, by the order they are declared. + * + * @param value of the enum. + * @return a {@link ExampleNullableEnum } with the matching value. + * @throws IllegalArgumentException if no enum has a value matching the given value. + */ + @JsonCreator + public static ExampleNullableEnum fromValue(final String value) { + for (final ExampleNullableEnum constant : ExampleNullableEnum.values()) { + if (constant.getValue().equals(value)) { + return constant; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleNullableRecord.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleNullableRecord.java new file mode 100644 index 00000000..fcb844bf --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleNullableRecord.java @@ -0,0 +1,31 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import org.springframework.lang.Nullable; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of a Record + * + * @param field1 a boolean field + */ +public record ExampleNullableRecord( + Optional field1) { + + @JsonCreator + public ExampleNullableRecord( + final Boolean field1) { + this(Optional.ofNullable(field1)); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecord.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecord.java new file mode 100644 index 00000000..fd29e2de --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecord.java @@ -0,0 +1,31 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import org.springframework.lang.Nullable; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of a Record + * + * @param field1 a boolean field + */ +public record ExampleRecord( + Optional field1) { + + @JsonCreator + public ExampleRecord( + final Boolean field1) { + this(Optional.ofNullable(field1)); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordOneImplements.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordOneImplements.java new file mode 100644 index 00000000..97449f00 --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordOneImplements.java @@ -0,0 +1,32 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import org.springframework.lang.Nullable; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of an Record which implements an interface. + * + * @param field1 a boolean field + */ +public record ExampleRecordOneImplements( + Optional field1) + implements io.github.chrimle.o2jrm.interfaces.TestInterfaceOne { + + @JsonCreator + public ExampleRecordOneImplements( + final Boolean field1) { + this(Optional.ofNullable(field1)); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordTwoImplements.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordTwoImplements.java new file mode 100644 index 00000000..820ea8e0 --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordTwoImplements.java @@ -0,0 +1,32 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import org.springframework.lang.Nullable; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of an Record which implements two interfaces. + * + * @param field1 a boolean field + */ +public record ExampleRecordTwoImplements( + Optional field1) + implements io.github.chrimle.o2jrm.interfaces.TestInterfaceOne, io.github.chrimle.o2jrm.interfaces.TestInterfaceTwo { + + @JsonCreator + public ExampleRecordTwoImplements( + final Boolean field1) { + this(Optional.ofNullable(field1)); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithCollectionsOfRecords.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithCollectionsOfRecords.java new file mode 100644 index 00000000..43ec6f96 --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithCollectionsOfRecords.java @@ -0,0 +1,50 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import io.github.chrimle.o2jrm.useOptional.ExampleRecord; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import org.springframework.lang.Nullable; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of a Record with collections of records. + * + * @param optionalRecordList Optional field + * @param requiredRecordList Required field + * @param optionalRecordSet Another Optional field + * @param requiredRecordSet Another required field + */ +public record ExampleRecordWithCollectionsOfRecords( + Optional> optionalRecordList, + @NotNull List<@Valid ExampleRecord> requiredRecordList, + Optional> optionalRecordSet, + @NotNull Set<@Valid ExampleRecord> requiredRecordSet) { + + @JsonCreator + public ExampleRecordWithCollectionsOfRecords( + final List<@Valid ExampleRecord> optionalRecordList, + final List<@Valid ExampleRecord> requiredRecordList, + final Set<@Valid ExampleRecord> optionalRecordSet, + final Set<@Valid ExampleRecord> requiredRecordSet) { + this(Optional.ofNullable(optionalRecordList), + requiredRecordList, + Optional.ofNullable(optionalRecordSet), + requiredRecordSet); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithDefaultFields.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithDefaultFields.java new file mode 100644 index 00000000..94edb6eb --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithDefaultFields.java @@ -0,0 +1,31 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import org.springframework.lang.Nullable; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of a Record with default fields + * + * @param field1 a String field with a default value + */ +public record ExampleRecordWithDefaultFields( + Optional field1) { + + @JsonCreator + public ExampleRecordWithDefaultFields( + final String field1) { + this(Optional.ofNullable(field1)); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithExtraFieldAnnotations.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithExtraFieldAnnotations.java new file mode 100644 index 00000000..ae2c1bbd --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithExtraFieldAnnotations.java @@ -0,0 +1,65 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import org.springframework.lang.Nullable; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of a Record with fields with `x-field-extra-annotations`. + * + * @param oneAnnotationAsInline Setting ONE annotation, using inlined. + * @param oneAnnotationAsNewLine Setting ONE annotation, using newlines. + * @param oneAnnotationAsList Setting ONE annotation, using list. + * @param oneAnnotationAsArray Setting ONE annotation, using array. + * @param twoAnnotationsAsNewLine Setting TWO annotations, using newlines. + * @param twoAnnotationsAsList Setting TWO annotations, using list. + * @param twoAnnotationsAsArray Setting TWO annotations, using array. + */ +public record ExampleRecordWithExtraFieldAnnotations( + @io.github.chrimle.o2jrm.annotations.TestFieldExtraAnnotationOne + Optional oneAnnotationAsInline, + @io.github.chrimle.o2jrm.annotations.TestFieldExtraAnnotationOne + Optional oneAnnotationAsNewLine, + @io.github.chrimle.o2jrm.annotations.TestFieldExtraAnnotationOne + Optional oneAnnotationAsList, + @io.github.chrimle.o2jrm.annotations.TestFieldExtraAnnotationOne + Optional oneAnnotationAsArray, + @io.github.chrimle.o2jrm.annotations.TestFieldExtraAnnotationOne +@io.github.chrimle.o2jrm.annotations.TestFieldExtraAnnotationTwo + Optional twoAnnotationsAsNewLine, + @io.github.chrimle.o2jrm.annotations.TestFieldExtraAnnotationOne + @io.github.chrimle.o2jrm.annotations.TestFieldExtraAnnotationTwo + Optional twoAnnotationsAsList, + @io.github.chrimle.o2jrm.annotations.TestFieldExtraAnnotationOne + @io.github.chrimle.o2jrm.annotations.TestFieldExtraAnnotationTwo + Optional twoAnnotationsAsArray) { + + @JsonCreator + public ExampleRecordWithExtraFieldAnnotations( + final Boolean oneAnnotationAsInline, + final Boolean oneAnnotationAsNewLine, + final Boolean oneAnnotationAsList, + final Boolean oneAnnotationAsArray, + final Boolean twoAnnotationsAsNewLine, + final Boolean twoAnnotationsAsList, + final Boolean twoAnnotationsAsArray) { + this(Optional.ofNullable(oneAnnotationAsInline), + Optional.ofNullable(oneAnnotationAsNewLine), + Optional.ofNullable(oneAnnotationAsList), + Optional.ofNullable(oneAnnotationAsArray), + Optional.ofNullable(twoAnnotationsAsNewLine), + Optional.ofNullable(twoAnnotationsAsList), + Optional.ofNullable(twoAnnotationsAsArray)); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithOneExtraAnnotation.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithOneExtraAnnotation.java new file mode 100644 index 00000000..c4be128a --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithOneExtraAnnotation.java @@ -0,0 +1,39 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import org.springframework.lang.Nullable; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of a Record with an extra annotation + * + * @param field1 a boolean field with an extra field annotation + * @param field2 a boolean field with two extra field annotations + */ +@io.github.chrimle.o2jrm.annotations.TestExtraAnnotation +public record ExampleRecordWithOneExtraAnnotation( + @io.github.chrimle.o2jrm.annotations.TestFieldExtraAnnotationOne + Optional field1, + @io.github.chrimle.o2jrm.annotations.TestFieldExtraAnnotationOne +@io.github.chrimle.o2jrm.annotations.TestFieldExtraAnnotationTwo + Optional field2) { + + @JsonCreator + public ExampleRecordWithOneExtraAnnotation( + final Boolean field1, + final Boolean field2) { + this(Optional.ofNullable(field1), + Optional.ofNullable(field2)); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithTwoExtraAnnotations.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithTwoExtraAnnotations.java new file mode 100644 index 00000000..e7a6e4cf --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleRecordWithTwoExtraAnnotations.java @@ -0,0 +1,33 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import org.springframework.lang.Nullable; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of a Record with two extra annotations + * + * @param field1 a boolean field + */ +@io.github.chrimle.o2jrm.annotations.TestExtraAnnotation +@io.github.chrimle.o2jrm.annotations.TestExtraAnnotationTwo +public record ExampleRecordWithTwoExtraAnnotations( + Optional field1) { + + @JsonCreator + public ExampleRecordWithTwoExtraAnnotations( + final Boolean field1) { + this(Optional.ofNullable(field1)); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleTwoImplementsEnum.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleTwoImplementsEnum.java new file mode 100644 index 00000000..00f9cb43 --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleTwoImplementsEnum.java @@ -0,0 +1,62 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonValue; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * Example of an Enum which implements two interfaces. + */ +public enum ExampleTwoImplementsEnum implements io.github.chrimle.o2jrm.interfaces.TestInterfaceOne, io.github.chrimle.o2jrm.interfaces.TestInterfaceTwo { + ENUM1("ENUM1"), + ENUM2("ENUM2"), + ENUM3("ENUM3"); + + private final String value; + + ExampleTwoImplementsEnum(final String value) { + this.value = value; + } + + /** + * Gets the {@code value} of this enum. + * + * @return the value of this enum. + */ + @JsonValue + public String getValue() { + return value; + } + + /** + * Case-sensitively matches the given {@code value} to an enum constant using {@link #getValue()}. + * + *

NOTE: if multiple enum constants have a matching value, the first enum constant is + * returned, by the order they are declared. + * + * @param value of the enum. + * @return a {@link ExampleTwoImplementsEnum } with the matching value. + * @throws IllegalArgumentException if no enum has a value matching the given value. + */ + @JsonCreator + public static ExampleTwoImplementsEnum fromValue(final String value) { + for (final ExampleTwoImplementsEnum constant : ExampleTwoImplementsEnum.values()) { + if (constant.getValue().equals(value)) { + return constant; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleUriEnum.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleUriEnum.java new file mode 100644 index 00000000..5fc08c60 --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/ExampleUriEnum.java @@ -0,0 +1,63 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonValue; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +import java.net.URI; + +/** + * Example of an Enum with URI values + */ +public enum ExampleUriEnum { + HTTPS_GITHUB_COM_CHRIMLE_OPENAPI_TO_JAVA_RECORDS_MUSTACHE_TEMPLATES(URI.create("https://github.com/Chrimle/openapi-to-java-records-mustache-templates")), + HTTPS_CHRIMLE_GITHUB_IO_OPENAPI_TO_JAVA_RECORDS_MUSTACHE_TEMPLATES_(URI.create("https://chrimle.github.io/openapi-to-java-records-mustache-templates/")); + + private final URI value; + + ExampleUriEnum(final URI value) { + this.value = value; + } + + /** + * Gets the {@code value} of this enum. + * + * @return the value of this enum. + */ + @JsonValue + public URI getValue() { + return value; + } + + /** + * Matches the given {@code value} to an enum constant using {@link #getValue()}. + * + *

NOTE: if multiple enum constants have a matching value, the first enum constant is + * returned, by the order they are declared. + * + * @param value of the enum. + * @return a {@link ExampleUriEnum } with the matching value. + * @throws IllegalArgumentException if no enum has a value matching the given value. + */ + @JsonCreator + public static ExampleUriEnum fromValue(final URI value) { + for (final ExampleUriEnum constant : ExampleUriEnum.values()) { + if (constant.getValue().equals(value)) { + return constant; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/RecordWithAllConstraints.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/RecordWithAllConstraints.java new file mode 100644 index 00000000..d9a67210 --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/RecordWithAllConstraints.java @@ -0,0 +1,126 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; +import org.openapitools.jackson.nullable.JsonNullable; +import org.springframework.lang.Nullable; +import java.util.NoSuchElementException; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of a Record which has fields with constraints + * + * @param stringStandard String. + * @param stringDefault String. + * @param stringNullable String. + * @param stringRequired String. + * @param stringRequiredNullable String. + * @param stringRequiredPattern String. + * @param stringEmailFormat String. + * @param stringUuidFormat UUID. + * @param stringMinLength String. + * @param stringMaxLength String. + * @param stringMinAndMaxLength String. + * @param arrayMinItems List. + * @param arrayMaxItems List. + * @param arrayMinAndMaxItems List. + * @param intMinimum Integer. + * @param intMaximum Integer. + * @param intMinimumAndMaximum Integer. + * @param longMinimum Long. + * @param longMaximum Long. + * @param longMinimumAndMaximum Long. + * @param bigDecimalMinimum BigDecimal. + * @param bigDecimalMaximum BigDecimal. + * @param bigDecimalMinimumAndMaximum BigDecimal. + */ +public record RecordWithAllConstraints( + Optional stringStandard, + Optional stringDefault, + Optional stringNullable, + @NotNull String stringRequired, + String stringRequiredNullable, + @NotNull @Pattern(regexp = "^\\d{3}-\\d{2}-\\d{4}$") String stringRequiredPattern, + Optional<@Email String> stringEmailFormat, + Optional stringUuidFormat, + Optional<@Size(min = 3) String> stringMinLength, + Optional<@Size(max = 7) String> stringMaxLength, + Optional<@Size(min = 3, max = 7) String> stringMinAndMaxLength, + Optional<@Size(min = 1) List> arrayMinItems, + Optional<@Size(max = 10) List> arrayMaxItems, + Optional<@Size(min = 1, max = 10) List> arrayMinAndMaxItems, + Optional<@Min(18) Integer> intMinimum, + Optional<@Max(100) Integer> intMaximum, + Optional<@Min(0) @Max(100) Integer> intMinimumAndMaximum, + Optional<@Min(18L) Long> longMinimum, + Optional<@Max(100L) Long> longMaximum, + Optional<@Min(0L) @Max(100L) Long> longMinimumAndMaximum, + Optional<@DecimalMin("0") BigDecimal> bigDecimalMinimum, + Optional<@DecimalMax("100") BigDecimal> bigDecimalMaximum, + Optional<@DecimalMin("0") @DecimalMax("100") BigDecimal> bigDecimalMinimumAndMaximum) { + + @JsonCreator + public RecordWithAllConstraints( + final String stringStandard, + final String stringDefault, + final String stringNullable, + final String stringRequired, + final String stringRequiredNullable, + final String stringRequiredPattern, + final String stringEmailFormat, + final UUID stringUuidFormat, + final String stringMinLength, + final String stringMaxLength, + final String stringMinAndMaxLength, + final List arrayMinItems, + final List arrayMaxItems, + final List arrayMinAndMaxItems, + final Integer intMinimum, + final Integer intMaximum, + final Integer intMinimumAndMaximum, + final Long longMinimum, + final Long longMaximum, + final Long longMinimumAndMaximum, + final BigDecimal bigDecimalMinimum, + final BigDecimal bigDecimalMaximum, + final BigDecimal bigDecimalMinimumAndMaximum) { + this(Optional.ofNullable(stringStandard), + Optional.ofNullable(stringDefault), + Optional.ofNullable(stringNullable), + stringRequired, + stringRequiredNullable, + stringRequiredPattern, + Optional.ofNullable(stringEmailFormat), + Optional.ofNullable(stringUuidFormat), + Optional.ofNullable(stringMinLength), + Optional.ofNullable(stringMaxLength), + Optional.ofNullable(stringMinAndMaxLength), + Optional.ofNullable(arrayMinItems), + Optional.ofNullable(arrayMaxItems), + Optional.ofNullable(arrayMinAndMaxItems), + Optional.ofNullable(intMinimum), + Optional.ofNullable(intMaximum), + Optional.ofNullable(intMinimumAndMaximum), + Optional.ofNullable(longMinimum), + Optional.ofNullable(longMaximum), + Optional.ofNullable(longMinimumAndMaximum), + Optional.ofNullable(bigDecimalMinimum), + Optional.ofNullable(bigDecimalMaximum), + Optional.ofNullable(bigDecimalMinimumAndMaximum)); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/RecordWithInnerEnums.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/RecordWithInnerEnums.java new file mode 100644 index 00000000..3c1c57b9 --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/RecordWithInnerEnums.java @@ -0,0 +1,186 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; +import java.net.URI; +import org.springframework.lang.Nullable; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of a Record with inner enum classes + * + * @param exampleInner Example of an inner enum class + * @param exampleInnerTwo Example of another inner enum class with integer values + * @param exampleInnerThree Example of another inner enum class with URI values + */ +public record RecordWithInnerEnums( + Optional exampleInner, + Optional exampleInnerTwo, + Optional exampleInnerThree) { + + @JsonCreator + public RecordWithInnerEnums( + final ExampleInnerEnum exampleInner, + final ExampleInnerTwoEnum exampleInnerTwo, + final ExampleInnerThreeEnum exampleInnerThree) { + this(Optional.ofNullable(exampleInner), + Optional.ofNullable(exampleInnerTwo), + Optional.ofNullable(exampleInnerThree)); + } + + /** + * Example of an inner enum class + */ + public enum ExampleInnerEnum { + /** + * Some description of ENUM1 + */ + ENUM1("ENUM1"), + /** + * Some description of ENUM2 + */ + ENUM2("ENUM2"), + /** + * Some description of ENUM3 + */ + ENUM3("ENUM3"); + + private final String value; + + ExampleInnerEnum(final String value) { + this.value = value; + } + + /** + * Gets the {@code value} of this enum. + * + * @return the value of this enum. + */ + @JsonValue + public String getValue() { + return value; + } + + /** + * Case-sensitively matches the given {@code value} to an enum constant using {@link + * #getValue()}. + * + *

NOTE: if multiple enum constants have a matching value, the first enum constant is + * returned, by the order they are declared. + * + * @param value of the enum. + * @return a {@link ExampleInnerEnum } with the matching value. + * @throws IllegalArgumentException if no enum has a value matching the given value. + */ + @JsonCreator + public static ExampleInnerEnum fromValue(final String value) { + for (final ExampleInnerEnum constant : ExampleInnerEnum.values()) { + if (constant.getValue().equals(value)) { + return constant; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } + } + + /** + * Example of another inner enum class with integer values + */ + public enum ExampleInnerTwoEnum { + NUMBER_404(404), + NUMBER_501(501), + NUMBER_503(503); + + private final Integer value; + + ExampleInnerTwoEnum(final Integer value) { + this.value = value; + } + + /** + * Gets the {@code value} of this enum. + * + * @return the value of this enum. + */ + @JsonValue + public Integer getValue() { + return value; + } + + /** + * Matches the given {@code value} to an enum constant using {@link #getValue()}. + * + *

NOTE: if multiple enum constants have a matching value, the first enum constant is + * returned, by the order they are declared. + * + * @param value of the enum. + * @return a {@link ExampleInnerTwoEnum } with the matching value. + * @throws IllegalArgumentException if no enum has a value matching the given value. + */ + @JsonCreator + public static ExampleInnerTwoEnum fromValue(final Integer value) { + for (final ExampleInnerTwoEnum constant : ExampleInnerTwoEnum.values()) { + if (constant.getValue().equals(value)) { + return constant; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } + } + + /** + * Example of another inner enum class with URI values + */ + public enum ExampleInnerThreeEnum { + HTTPS_GITHUB_COM_CHRIMLE_OPENAPI_TO_JAVA_RECORDS_MUSTACHE_TEMPLATES(URI.create("https://github.com/Chrimle/openapi-to-java-records-mustache-templates")), + HTTPS_CHRIMLE_GITHUB_IO_OPENAPI_TO_JAVA_RECORDS_MUSTACHE_TEMPLATES_(URI.create("https://chrimle.github.io/openapi-to-java-records-mustache-templates/")); + + private final URI value; + + ExampleInnerThreeEnum(final URI value) { + this.value = value; + } + + /** + * Gets the {@code value} of this enum. + * + * @return the value of this enum. + */ + @JsonValue + public URI getValue() { + return value; + } + + /** + * Case-sensitively matches the given {@code value} to an enum constant using {@link + * #getValue()}. + * + *

NOTE: if multiple enum constants have a matching value, the first enum constant is + * returned, by the order they are declared. + * + * @param value of the enum. + * @return a {@link ExampleInnerThreeEnum } with the matching value. + * @throws IllegalArgumentException if no enum has a value matching the given value. + */ + @JsonCreator + public static ExampleInnerThreeEnum fromValue(final URI value) { + for (final ExampleInnerThreeEnum constant : ExampleInnerThreeEnum.values()) { + if (constant.getValue().equals(value)) { + return constant; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/RecordWithNullableFieldsOfEachType.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/RecordWithNullableFieldsOfEachType.java new file mode 100644 index 00000000..4b9e3975 --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/RecordWithNullableFieldsOfEachType.java @@ -0,0 +1,70 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; +import io.github.chrimle.o2jrm.useOptional.ExampleNullableEnum; +import io.github.chrimle.o2jrm.useOptional.ExampleNullableRecord; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import org.openapitools.jackson.nullable.JsonNullable; +import org.springframework.lang.Nullable; +import java.util.NoSuchElementException; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of a Record with fields of each type + * + * @param field1 a Boolean field + * @param field2 a String field + * @param field3 an Integer field + * @param field4 a Number field + * @param field5 an Array of Boolean field + * @param field6 a Set field + * @param field7 ExampleNullableRecord. + * @param field8 ExampleNullableEnum. + */ +public record RecordWithNullableFieldsOfEachType( + Boolean field1, + String field2, + Integer field3, + BigDecimal field4, + List field5, + Set field6, + Optional<@Valid ExampleNullableRecord> field7, + Optional field8) { + + @JsonCreator + public RecordWithNullableFieldsOfEachType( + final Boolean field1, + final String field2, + final Integer field3, + final BigDecimal field4, + final List field5, + final Set field6, + final ExampleNullableRecord field7, + final ExampleNullableEnum field8) { + this(field1, + field2, + field3, + field4, + field5, + field6, + Optional.ofNullable(field7), + Optional.ofNullable(field8)); + } +} diff --git a/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/RecordWithRequiredFieldsOfEachType.java b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/RecordWithRequiredFieldsOfEachType.java new file mode 100644 index 00000000..6e6e063f --- /dev/null +++ b/test-spring/target/generated-sources/src/gen/java/main/io/github/chrimle/o2jrm/useOptional/RecordWithRequiredFieldsOfEachType.java @@ -0,0 +1,69 @@ +package io.github.chrimle.o2jrm.useOptional; + +import java.net.URI; +import java.util.Objects; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import io.github.chrimle.o2jrm.useOptional.ExampleEnum; +import io.github.chrimle.o2jrm.useOptional.ExampleRecord; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import org.springframework.lang.Nullable; +import org.openapitools.jackson.nullable.JsonNullable; +import java.time.OffsetDateTime; +import jakarta.validation.Valid; +import jakarta.validation.constraints.*; +import io.swagger.v3.oas.annotations.media.Schema; + + +import java.util.*; +import jakarta.annotation.Generated; + +/** + * Example of a Record with fields of each type + * + * @param field1 a Boolean field + * @param field2 a String field + * @param field3 an Integer field + * @param field4 a Number field + * @param field5 an Array of Boolean field + * @param field6 a Set field + * @param field7 ExampleRecord. + * @param field8 ExampleEnum. + */ +public record RecordWithRequiredFieldsOfEachType( + @NotNull Boolean field1, + @NotNull String field2, + @NotNull Integer field3, + @NotNull BigDecimal field4, + @NotNull List field5, + @NotNull Set field6, + @Valid @NotNull ExampleRecord field7, + @NotNull ExampleEnum field8) { + + @JsonCreator + public RecordWithRequiredFieldsOfEachType( + final Boolean field1, + final String field2, + final Integer field3, + final BigDecimal field4, + final List field5, + final Set field6, + final ExampleRecord field7, + final ExampleEnum field8) { + this(field1, + field2, + field3, + field4, + field5, + field6, + field7, + field8); + } +}