Skip to content

Commit

Permalink
feat: [OpenAPI Generator] Allow for wildcard nullable=false enum de…
Browse files Browse the repository at this point in the history
…serialization (#572)
  • Loading branch information
newtork authored Sep 2, 2024
1 parent d78b8ab commit a636732
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 6 deletions.
1 change: 1 addition & 0 deletions datamodel/openapi/openapi-api-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<pojoBuilderMethodName>create</pojoBuilderMethodName>
<pojoBuildMethodName />
<pojoConstructorVisibility>protected</pojoConstructorVisibility>
<enumUnknownDefaultCase>true</enumUnknownDefaultCase>
</additionalProperties>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;

/**
* Soda
Expand All @@ -46,6 +48,85 @@ public class Soda
@JsonProperty( "quantity" )
private Integer quantity;

/**
* Gets or Sets packaging
*/
public enum PackagingEnum
{
/**
* The GLASS option of this Soda
*/
GLASS("glass"),

/**
* The CARTON option of this Soda
*/
CARTON("carton"),

/**
* The CAN option of this Soda
*/
CAN("can"),

/**
* The UNKNOWN_DEFAULT_OPEN_API option of this Soda
*/
UNKNOWN_DEFAULT_OPEN_API("unknown_default_open_api");

private String value;

PackagingEnum( String value )
{
this.value = value;
}

/**
* Get the value of the enum
*
* @return The enum value
*/
@JsonValue
@Nonnull
public String getValue()
{
return value;
}

/**
* Get the String value of the enum value.
*
* @return The enum value as String
*/
@Override
@Nonnull
public String toString()
{
return String.valueOf(value);
}

/**
* Get the enum value from a String value
*
* @param value
* The String value
* @return The enum value of type Soda
*/
@JsonCreator
@Nonnull
public static PackagingEnum fromValue( @Nonnull final String value )
{
for( PackagingEnum b : PackagingEnum.values() ) {
if( b.value.equals(value) ) {
return b;
}
}
return UNKNOWN_DEFAULT_OPEN_API;
}
}

@JsonProperty( "packaging" )
private PackagingEnum packaging;

@JsonProperty( "price" )
private Float price;

Expand Down Expand Up @@ -165,6 +246,42 @@ public void setQuantity( @Nonnull final Integer quantity )
this.quantity = quantity;
}

/**
* Set the packaging of this {@link Soda} instance and return the same instance.
*
* @param packaging
* The packaging of this {@link Soda}
* @return The same instance of this {@link Soda} class
*/
@Nonnull
public Soda packaging( @Nullable final PackagingEnum packaging )
{
this.packaging = packaging;
return this;
}

/**
* Get packaging
*
* @return packaging The packaging of this {@link Soda} instance.
*/
@Nonnull
public PackagingEnum getPackaging()
{
return packaging;
}

/**
* Set the packaging of this {@link Soda} instance.
*
* @param packaging
* The packaging of this {@link Soda}
*/
public void setPackaging( @Nullable final PackagingEnum packaging )
{
this.packaging = packaging;
}

/**
* Set the price of this {@link Soda} instance and return the same instance.
*
Expand Down Expand Up @@ -261,13 +378,14 @@ public boolean equals( @Nullable final java.lang.Object o )
&& Objects.equals(this.name, soda.name)
&& Objects.equals(this.brand, soda.brand)
&& Objects.equals(this.quantity, soda.quantity)
&& Objects.equals(this.packaging, soda.packaging)
&& Objects.equals(this.price, soda.price);
}

@Override
public int hashCode()
{
return Objects.hash(name, brand, quantity, price, cloudSdkCustomFields);
return Objects.hash(name, brand, quantity, packaging, price, cloudSdkCustomFields);
}

@Override
Expand All @@ -279,6 +397,7 @@ public String toString()
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append(" brand: ").append(toIndentedString(brand)).append("\n");
sb.append(" quantity: ").append(toIndentedString(quantity)).append("\n");
sb.append(" packaging: ").append(toIndentedString(packaging)).append("\n");
sb.append(" price: ").append(toIndentedString(price)).append("\n");
cloudSdkCustomFields
.forEach(( k, v ) -> sb.append(" ").append(k).append(": ").append(toIndentedString(v)).append("\n"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;

/**
* SodaWithId
Expand All @@ -46,6 +48,85 @@ public class SodaWithId
@JsonProperty( "quantity" )
private Integer quantity;

/**
* Gets or Sets packaging
*/
public enum PackagingEnum
{
/**
* The GLASS option of this SodaWithId
*/
GLASS("glass"),

/**
* The CARTON option of this SodaWithId
*/
CARTON("carton"),

/**
* The CAN option of this SodaWithId
*/
CAN("can"),

/**
* The UNKNOWN_DEFAULT_OPEN_API option of this SodaWithId
*/
UNKNOWN_DEFAULT_OPEN_API("unknown_default_open_api");

private String value;

PackagingEnum( String value )
{
this.value = value;
}

/**
* Get the value of the enum
*
* @return The enum value
*/
@JsonValue
@Nonnull
public String getValue()
{
return value;
}

/**
* Get the String value of the enum value.
*
* @return The enum value as String
*/
@Override
@Nonnull
public String toString()
{
return String.valueOf(value);
}

/**
* Get the enum value from a String value
*
* @param value
* The String value
* @return The enum value of type SodaWithId
*/
@JsonCreator
@Nonnull
public static PackagingEnum fromValue( @Nonnull final String value )
{
for( PackagingEnum b : PackagingEnum.values() ) {
if( b.value.equals(value) ) {
return b;
}
}
return UNKNOWN_DEFAULT_OPEN_API;
}
}

@JsonProperty( "packaging" )
private PackagingEnum packaging;

@JsonProperty( "price" )
private Float price;

Expand Down Expand Up @@ -168,6 +249,42 @@ public void setQuantity( @Nonnull final Integer quantity )
this.quantity = quantity;
}

/**
* Set the packaging of this {@link SodaWithId} instance and return the same instance.
*
* @param packaging
* The packaging of this {@link SodaWithId}
* @return The same instance of this {@link SodaWithId} class
*/
@Nonnull
public SodaWithId packaging( @Nullable final PackagingEnum packaging )
{
this.packaging = packaging;
return this;
}

/**
* Get packaging
*
* @return packaging The packaging of this {@link SodaWithId} instance.
*/
@Nonnull
public PackagingEnum getPackaging()
{
return packaging;
}

/**
* Set the packaging of this {@link SodaWithId} instance.
*
* @param packaging
* The packaging of this {@link SodaWithId}
*/
public void setPackaging( @Nullable final PackagingEnum packaging )
{
this.packaging = packaging;
}

/**
* Set the price of this {@link SodaWithId} instance and return the same instance.
*
Expand Down Expand Up @@ -300,14 +417,15 @@ public boolean equals( @Nullable final java.lang.Object o )
&& Objects.equals(this.name, sodaWithId.name)
&& Objects.equals(this.brand, sodaWithId.brand)
&& Objects.equals(this.quantity, sodaWithId.quantity)
&& Objects.equals(this.packaging, sodaWithId.packaging)
&& Objects.equals(this.price, sodaWithId.price)
&& Objects.equals(this.id, sodaWithId.id);
}

@Override
public int hashCode()
{
return Objects.hash(name, brand, quantity, price, id, cloudSdkCustomFields);
return Objects.hash(name, brand, quantity, packaging, price, id, cloudSdkCustomFields);
}

@Override
Expand All @@ -319,6 +437,7 @@ public String toString()
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append(" brand: ").append(toIndentedString(brand)).append("\n");
sb.append(" quantity: ").append(toIndentedString(quantity)).append("\n");
sb.append(" packaging: ").append(toIndentedString(packaging)).append("\n");
sb.append(" price: ").append(toIndentedString(price)).append("\n");
sb.append(" id: ").append(toIndentedString(id)).append("\n");
cloudSdkCustomFields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
"format": "int32",
"example": 100
},
"packaging": {
"type": "string",
"enum": ["glass", "carton", "can"],
"nullable": false
},
"price": {
"type": "number",
"format": "float",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@ void testFullResponse()
"name": "Cola",
"brand": "Coca-Cola",
"quantity": 100,
"packaging" : "new-value",
"price": 1.5,
"id": 0
}
""";
stub(responseBody);

final SodaWithId expected =
SodaWithId.create().name("Cola").brand("Coca-Cola").quantity(100).price(1.5f).id(0L);
SodaWithId
.create()
.name("Cola")
.brand("Coca-Cola")
.quantity(100)
.price(1.5f)
.id(0L)
.packaging(SodaWithId.PackagingEnum.UNKNOWN_DEFAULT_OPEN_API);

final SodaWithId actual = sut.sodasIdGet(1L);

Expand Down
Loading

0 comments on commit a636732

Please sign in to comment.