Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(openapi-generator): Suppress faulty additionalProperties: true #689

Merged
merged 12 commits into from
Jan 29, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ components:
example: 123
Order:
type: object
additionalProperties: true
required:
- productId
- quantity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination;
import com.sap.cloud.sdk.datamodel.openapi.sample.model.Order;
import com.sap.cloud.sdk.datamodel.openapi.sample.model.SodaWithId;

@WireMockTest
Expand Down Expand Up @@ -48,7 +49,7 @@ void testPutPayload()
}

@Test
void testJacksonSerialization()
void testJacksonSerializeSodaWithId()
throws JsonProcessingException
{
expected = """
Expand All @@ -75,6 +76,26 @@ void testJacksonSerialization()
assertThat(new ObjectMapper().writeValueAsString(obj)).isEqualToIgnoringWhitespace(expected);
}

@Test
void testJacksonSerializeOrder()
newtork marked this conversation as resolved.
Show resolved Hide resolved
throws JsonProcessingException
{
expected = """
{
"productId": 100,
"quantity": 5,
"totalPrice": 6.0,
"typelessProperty":null,
"nullableProperty":null,
"shoesize": 44
}
""";
final Order order = Order.create().productId(100L).quantity(5).totalPrice(6.0f);
order.setCustomField("shoesize", 44);
assertThat(new ObjectMapper().writeValueAsString(order)).isEqualToIgnoringWhitespace(expected);
Copy link
Contributor Author

@newtork newtork Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Comment)

Without the enabled feature toggle this test assertion fails:

Expecting actual:
  "{}"
to be equal to:
  "{
  "productId": 100,
  "quantity": 5,
  "totalPrice": 6.0,
  "packaging" : "bottle",
  "shoesize": 44
}

assertThat(new ObjectMapper().readValue(expected, Order.class)).isEqualTo(order);
}

private void verify( String requestBody )
{
WireMock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.config.GlobalSettings;
import org.openapitools.codegen.languages.JavaClientCodegen;
Expand All @@ -23,6 +24,7 @@

import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.parser.core.models.AuthorizationValue;
import io.swagger.v3.parser.core.models.ParseOptions;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -89,6 +91,16 @@ public OperationsMap postProcessOperationsWithModels(
}
return super.postProcessOperationsWithModels(ops, allModels);
}

@SuppressWarnings( { "rawtypes", "RedundantSuppression" } )
@Override
protected void updateModelForObject( @Nonnull final CodegenModel m, @Nonnull final Schema schema )
{
// Disable additional attributes to prevent model classes from extending "HashMap"
// SAP Cloud SDK offers custom field APIs to handle additional attributes already
schema.setAdditionalProperties(Boolean.FALSE);
super.updateModelForObject(m, schema);
}
};
}

Expand Down
3 changes: 2 additions & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@

### 🐛 Fixed Issues

-
- Fix non-compilable code using OpenAPI generator with schema definitions having `additionalProperties: true`.
Previously they would result in model classes extending `HashMap`, which disabled proper deserialization and serialization.
Loading