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

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

Open
newtork opened this issue May 24, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@newtork
Copy link
Contributor

newtork commented May 24, 2024

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:

@newtork newtork added the enhancement New feature or request label May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant