Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions mustache-templates/src/main/resources/templates/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}} {

Expand All @@ -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}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}} {

Expand All @@ -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}}

Expand Down
23 changes: 16 additions & 7 deletions mustache-templates/target/classes/templates/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}} {

Expand All @@ -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}}

Expand Down
12 changes: 12 additions & 0 deletions test-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,18 @@
</configOptions>
</configuration>
</execution>
<execution>
<id>generate-spring-useOptional</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<modelPackage>${modelPackage.default}.useOptional</modelPackage>
<configOptions>
<useOptional>true</useOptional>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -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()}.
*
* <p><b>NOTE:</b> 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 + "'");
}
}
Original file line number Diff line number Diff line change
@@ -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<Boolean> field1) {

@JsonCreator
public DeprecatedExampleRecord(
final Boolean field1) {
this(Optional.ofNullable(field1));
}
}
Original file line number Diff line number Diff line change
@@ -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()}.
*
* <p><b>NOTE:</b> 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 + "'");
}
}
Original file line number Diff line number Diff line change
@@ -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()}.
*
* <p><b>NOTE:</b> 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 + "'");
}
}
Loading
Loading