Skip to content

Commit

Permalink
Generate enums not for parent vars, fix #432 (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
sschnabe authored Dec 11, 2024
1 parent 219eb96 commit 63447f6
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 2 deletions.
93 changes: 93 additions & 0 deletions gen/main/java/issue/_432/Child.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package issue._432;

@jakarta.annotation.Generated("org.openapitools.codegen.languages.MicronautCodegen")
@io.micronaut.serde.annotation.Serdeable
public final class Child implements Parent {

@com.fasterxml.jackson.annotation.JsonProperty("id")
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
private java.lang.String id;

@com.fasterxml.jackson.annotation.JsonProperty("state")
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
private State state;

@com.fasterxml.jackson.annotation.JsonProperty("name")
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
private java.lang.String name;

// methods

@Override
public boolean equals(Object object) {
if (object == this) {
return true;
}
if (object == null || getClass() != object.getClass()) {
return false;
}
Child other = (Child) object;
return java.util.Objects.equals(id, other.id)
&& java.util.Objects.equals(state, other.state)
&& java.util.Objects.equals(name, other.name);
}

@Override
public int hashCode() {
return java.util.Objects.hash(id, state, name);
}

@Override
public java.lang.String toString() {
return new java.lang.StringBuilder()
.append("Child[")
.append("id=").append(id).append(",")
.append("state=").append(state).append(",")
.append("name=").append(name)
.append("]")
.toString();
}

// fluent

public Child id(java.lang.String newId) {
this.id = newId;
return this;
}

public Child state(State newState) {
this.state = newState;
return this;
}

public Child name(java.lang.String newName) {
this.name = newName;
return this;
}

// getter/setter

public java.lang.String getId() {
return id;
}

public void setId(java.lang.String newId) {
this.id = newId;
}

public State getState() {
return state;
}

public void setState(State newState) {
this.state = newState;
}

public java.lang.String getName() {
return name;
}

public void setName(java.lang.String newName) {
this.name = newName;
}
}
54 changes: 54 additions & 0 deletions gen/main/java/issue/_432/Parent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package issue._432;

@jakarta.annotation.Generated("org.openapitools.codegen.languages.MicronautCodegen")
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(as = ParentDefault.class)
public sealed interface Parent permits Child, ParentDefault {


@com.fasterxml.jackson.annotation.JsonProperty("id")
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
java.lang.String getId();

@com.fasterxml.jackson.annotation.JsonProperty("id")
void setId(java.lang.String newId);

@com.fasterxml.jackson.annotation.JsonProperty("state")
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
State getState();

@com.fasterxml.jackson.annotation.JsonProperty("state")
void setState(State newState);

@io.micronaut.serde.annotation.Serdeable
public enum State {

ON("on"),
OFF("off");

public static final java.lang.String ON_VALUE = "on";
public static final java.lang.String OFF_VALUE = "off";

private final java.lang.String value;

private State(java.lang.String value) {
this.value = value;
}

@com.fasterxml.jackson.annotation.JsonCreator
public static State toEnum(java.lang.String value) {
return toOptional(value).orElseThrow(() -> new IllegalArgumentException("Unknown value '" + value + "'."));
}

public static java.util.Optional<State> toOptional(java.lang.String value) {
return java.util.Arrays
.stream(values())
.filter(e -> e.value.equals(value))
.findAny();
}

@com.fasterxml.jackson.annotation.JsonValue
public java.lang.String getValue() {
return value;
}
}
}
74 changes: 74 additions & 0 deletions gen/main/java/issue/_432/ParentDefault.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package issue._432;

@jakarta.annotation.Generated("org.openapitools.codegen.languages.MicronautCodegen")
@io.micronaut.serde.annotation.Serdeable
public final class ParentDefault implements Parent {

@com.fasterxml.jackson.annotation.JsonProperty("id")
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
private java.lang.String id;

@com.fasterxml.jackson.annotation.JsonProperty("state")
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
private State state;

// methods

@Override
public boolean equals(Object object) {
if (object == this) {
return true;
}
if (object == null || getClass() != object.getClass()) {
return false;
}
ParentDefault other = (ParentDefault) object;
return java.util.Objects.equals(id, other.id)
&& java.util.Objects.equals(state, other.state);
}

@Override
public int hashCode() {
return java.util.Objects.hash(id, state);
}

@Override
public java.lang.String toString() {
return new java.lang.StringBuilder()
.append("ParentDefault[")
.append("id=").append(id).append(",")
.append("state=").append(state)
.append("]")
.toString();
}

// fluent

public ParentDefault id(java.lang.String newId) {
this.id = newId;
return this;
}

public ParentDefault state(State newState) {
this.state = newState;
return this;
}

// getter/setter

public java.lang.String getId() {
return id;
}

public void setId(java.lang.String newId) {
this.id = newId;
}

public State getState() {
return state;
}

public void setState(State newState) {
this.state = newState;
}
}
4 changes: 2 additions & 2 deletions src/main/resources/Micronaut/modelPojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public{{#sealed}}{{#interfaceModels}} final{{/interfaceModels}}{{/sealed}} class

public void {{setter}}({{>modelPropertyType}} new{{nameInPascalCase}}) {
this.{{name}} = new{{nameInPascalCase}};
}{{/allVars}}{{#allVars}}{{#isEnum}}
}{{/allVars}}{{#vars}}{{#isEnum}}

{{>modelEnum}}{{/isEnum}}{{/allVars}}
{{>modelEnum}}{{/isEnum}}{{/vars}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ void modelWithPrefix() {
.addAdditionalProperty(CodegenConstants.MODEL_NAME_SUFFIX, "ModelSuffix"));
}

@DisplayName("model parent has inner eum, should not created for childs")
@Test
void modelWithInnerEnum() {
generate(configurator("src/test/resources/openapi/issue-432.yaml", "issue._432"));
}

static void generate(CodegenConfigurator configurator) {
var gen = new DefaultGenerator();
gen.setGenerateMetadata(false);
Expand Down
21 changes: 21 additions & 0 deletions src/test/resources/openapi/issue-432.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
openapi: 3.0.1
info:
version: 0.0.1
title: EnumTest
paths: {}
components:
schemas:
Parent:
type: object
properties:
id:
type: string
state:
enum: ["on", "off"]
Child:
allOf:
- type: object
properties:
name:
type: string
- $ref: "#/components/schemas/Parent"

0 comments on commit 63447f6

Please sign in to comment.