cloud-sdk-java icon indicating copy to clipboard operation
cloud-sdk-java copied to clipboard

Feature request: Enable convenient API access to customize OpenAPI (de)serialization options.

Open newtork opened this issue 1 year ago • 0 comments

Scenario:

  • My target OpenAPI endpoint gives an error upon receiving null values - total garbage, I know.

Use Case:

  • Currently this is the code, that I need to invoke to stop null serialization on my generated OpenAPI code:
Destination destination;
ApiClient apiClient = new ApiClient(createRestTemplate(destination));
apiClient.setBasePath(destination.getUri().toString());  // argsl... (see side-issue)

new DefaultApi(apiClient); // my service class

with

@SuppressWarnings("UnstableApiUsage")
private static RestTemplate createRestTemplate(Destination destination) {
  var objectMapper = new Jackson2ObjectMapperBuilder()
    .modules(new JavaTimeModule())
    .visibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE)
    .visibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE)
    .serializationInclusion(JsonInclude.Include.NON_NULL)                         // THIS STOPS `null` serialization
    .build();

  var httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
  httpRequestFactory.setHttpClient(ApacheHttpClient5Accessor.getHttpClient(destination));

  var result = new RestTemplate();
  result.getMessageConverters()
    .stream()
    .filter(MappingJackson2HttpMessageConverter.class::isInstance)
    .map(MappingJackson2HttpMessageConverter.class::cast)
    .forEach(converter -> converter.setObjectMapper(objectMapper));
  result.setRequestFactory(new BufferingClientHttpRequestFactory(httpRequestFactory));
  return result;
}

Feature request:

  • Please provide a convenient way to customize (de)serialization options on ApiClient or the generated Service class respectively.
    • It looks like the easiest option would be to expose some existing private methods on ApiClient as protected, such that I can overload them upon class instantiation.

Side issue:

  • When instantiating the ApiClient myself the basePath will not be overwritten by default. Independent whether I use the Destination-based or RestTemplate-based constructor. https://github.com/SAP/cloud-sdk-java/blob/a6a3ec9e61119ed0d23a5761dd2bfa88c8ddde27/datamodel/openapi/openapi-core/src/main/java/com/sap/cloud/sdk/services/openapi/apiclient/ApiClient.java#L108

newtork avatar May 24 '24 09:05 newtork