diff --git a/datamodel/openapi/openapi-api-sample/pom.xml b/datamodel/openapi/openapi-api-sample/pom.xml index 8c469af81..a7b9a728d 100644 --- a/datamodel/openapi/openapi-api-sample/pom.xml +++ b/datamodel/openapi/openapi-api-sample/pom.xml @@ -119,6 +119,7 @@ create protected + true diff --git a/datamodel/openapi/openapi-api-sample/src/main/java/com/sap/cloud/sdk/datamodel/openapi/sample/model/Soda.java b/datamodel/openapi/openapi-api-sample/src/main/java/com/sap/cloud/sdk/datamodel/openapi/sample/model/Soda.java index b09e9bac8..3cef9af06 100644 --- a/datamodel/openapi/openapi-api-sample/src/main/java/com/sap/cloud/sdk/datamodel/openapi/sample/model/Soda.java +++ b/datamodel/openapi/openapi-api-sample/src/main/java/com/sap/cloud/sdk/datamodel/openapi/sample/model/Soda.java @@ -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 @@ -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; @@ -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. * @@ -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 @@ -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")); diff --git a/datamodel/openapi/openapi-api-sample/src/main/java/com/sap/cloud/sdk/datamodel/openapi/sample/model/SodaWithId.java b/datamodel/openapi/openapi-api-sample/src/main/java/com/sap/cloud/sdk/datamodel/openapi/sample/model/SodaWithId.java index 8977aba2d..9d9731fb5 100644 --- a/datamodel/openapi/openapi-api-sample/src/main/java/com/sap/cloud/sdk/datamodel/openapi/sample/model/SodaWithId.java +++ b/datamodel/openapi/openapi-api-sample/src/main/java/com/sap/cloud/sdk/datamodel/openapi/sample/model/SodaWithId.java @@ -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 @@ -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; @@ -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. * @@ -300,6 +417,7 @@ 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); } @@ -307,7 +425,7 @@ public boolean equals( @Nullable final java.lang.Object o ) @Override public int hashCode() { - return Objects.hash(name, brand, quantity, price, id, cloudSdkCustomFields); + return Objects.hash(name, brand, quantity, packaging, price, id, cloudSdkCustomFields); } @Override @@ -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 diff --git a/datamodel/openapi/openapi-api-sample/src/main/resources/sodastore.json b/datamodel/openapi/openapi-api-sample/src/main/resources/sodastore.json index acafba783..2f2bd0412 100644 --- a/datamodel/openapi/openapi-api-sample/src/main/resources/sodastore.json +++ b/datamodel/openapi/openapi-api-sample/src/main/resources/sodastore.json @@ -54,6 +54,11 @@ "format": "int32", "example": 100 }, + "packaging": { + "type": "string", + "enum": ["glass", "carton", "can"], + "nullable": false + }, "price": { "type": "number", "format": "float", diff --git a/datamodel/openapi/openapi-api-sample/src/test/java/com/sap/cloud/sdk/datamodel/openapi/sample/api/DeserializationTest.java b/datamodel/openapi/openapi-api-sample/src/test/java/com/sap/cloud/sdk/datamodel/openapi/sample/api/DeserializationTest.java index 8f52104e1..78bb58b43 100644 --- a/datamodel/openapi/openapi-api-sample/src/test/java/com/sap/cloud/sdk/datamodel/openapi/sample/api/DeserializationTest.java +++ b/datamodel/openapi/openapi-api-sample/src/test/java/com/sap/cloud/sdk/datamodel/openapi/sample/api/DeserializationTest.java @@ -37,6 +37,7 @@ void testFullResponse() "name": "Cola", "brand": "Coca-Cola", "quantity": 100, + "packaging" : "new-value", "price": 1.5, "id": 0 } @@ -44,7 +45,14 @@ void testFullResponse() 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); diff --git a/datamodel/openapi/openapi-api-sample/src/test/java/com/sap/cloud/sdk/datamodel/openapi/sample/api/SerializationTest.java b/datamodel/openapi/openapi-api-sample/src/test/java/com/sap/cloud/sdk/datamodel/openapi/sample/api/SerializationTest.java index d554bcbcd..32458aab0 100644 --- a/datamodel/openapi/openapi-api-sample/src/test/java/com/sap/cloud/sdk/datamodel/openapi/sample/api/SerializationTest.java +++ b/datamodel/openapi/openapi-api-sample/src/test/java/com/sap/cloud/sdk/datamodel/openapi/sample/api/SerializationTest.java @@ -38,6 +38,7 @@ void testPutPayload() "name": "Cola", "brand": "Coca-Cola", "quantity": 100, + "packaging" : null, "price": 1.5, "id": 0 } @@ -59,12 +60,21 @@ void testJacksonSerialization() "name": "Cola", "brand": "Coca-Cola", "quantity": 100, + "packaging" : "can", "price": 1.5, "id": 0 } """; - final SodaWithId obj = SodaWithId.create().name("Cola").brand("Coca-Cola").quantity(100).price(1.5f).id(0L); + final SodaWithId obj = + SodaWithId + .create() + .name("Cola") + .brand("Coca-Cola") + .quantity(100) + .price(1.5f) + .id(0L) + .packaging(SodaWithId.PackagingEnum.CAN); assertThat(new ObjectMapper().writeValueAsString(obj)).isEqualToIgnoringWhitespace(expected); } diff --git a/datamodel/openapi/openapi-generator/src/main/resources/openapi-generator/mustache-templates/modelEnum.mustache b/datamodel/openapi/openapi-generator/src/main/resources/openapi-generator/mustache-templates/modelEnum.mustache index cd72f448b..6de2249ab 100644 --- a/datamodel/openapi/openapi-generator/src/main/resources/openapi-generator/mustache-templates/modelEnum.mustache +++ b/datamodel/openapi/openapi-generator/src/main/resources/openapi-generator/mustache-templates/modelEnum.mustache @@ -59,7 +59,7 @@ import com.fasterxml.jackson.annotation.JsonValue; return b; } } - {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}} + {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}{{#enumUnknownDefaultCase}}{{#allowableValues}}{{#enumVars}}{{#-last}}return {{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}{{/enumUnknownDefaultCase}}{{^enumUnknownDefaultCase}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/enumUnknownDefaultCase}}{{/isNullable}} } {{#supportUrlQuery}} diff --git a/datamodel/openapi/openapi-generator/src/main/resources/openapi-generator/mustache-templates/modelInnerEnum.mustache b/datamodel/openapi/openapi-generator/src/main/resources/openapi-generator/mustache-templates/modelInnerEnum.mustache index 7911032d3..7785a086a 100644 --- a/datamodel/openapi/openapi-generator/src/main/resources/openapi-generator/mustache-templates/modelInnerEnum.mustache +++ b/datamodel/openapi/openapi-generator/src/main/resources/openapi-generator/mustache-templates/modelInnerEnum.mustache @@ -64,6 +64,6 @@ return b; } } - {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}} + {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}{{#enumUnknownDefaultCase}}{{#allowableValues}}{{#enumVars}}{{#-last}}return {{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}{{/enumUnknownDefaultCase}}{{^enumUnknownDefaultCase}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/enumUnknownDefaultCase}}{{/isNullable}} } } diff --git a/release_notes.md b/release_notes.md index 04a978435..4af227b8a 100644 --- a/release_notes.md +++ b/release_notes.md @@ -26,6 +26,7 @@ Instead, only the `apiPackage`- and `apiPackage`-related directories will be cleaned. This reduces the risk of deleting files unexpectedly and allows for reusing the same `outputDirectory` for multiple generator plugin invocations. - \[OpenAPI Generator\] The property accessors of generated model classes now have consistent `@Nullable` and `@Nonnull` annotation. +- \[OpenAPI Generator\] Enable the option `` that allows for lenient handling of unknown enum values coming from a server. - Upgrade to version `1.66.0` of `gRPC` dependencies coming in transitively when using `connectivity-ztis` - Improve the error handling for OData batch requests. In case an OData error is given within a batch response it will now be parsed and returned as `ODataServiceErrorException`.