OpenAPITools / OpenAPITools/openapi-generator
[REQ] [Java] [native] Make generated ApiClient friendlier for extending
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
I am using Java generation with library = "native" and there are some things (ObjectMapper, HttpClient.Builder, ...) I would like to customize.
Describe the solution you'd like
I would like to add a new generated constructor that takes some of the values as parameters.
The default constructor will call the new constructor.
Backward compatibility will be kept.
I will have compilation validation as an advantage in case future changes are made to the auto generated ApiClient.
Describe alternatives you've considered
a. I can use the setters but that issues with this approach are:
- The code running in the constructor is thrown away (ObjectMapper creation is considered expensive).
- If we init variables in one place and override in another, it makes the code less readable.
b. I can use a custom template but than I will need to follow the template changes every time I upgrade openapi-generator so I won't miss any important updates.
Additional context
I am planning something like this:
public ApiClient() { // Existing ctor refactored
this(HttpClient.newBuilder(), createDefaultObjectMapper(), getDefaultBaseUri()); // Using new ctor
interceptor = null;
readTimeout = null;
responseInterceptor = null;
}
public ApiClient(Builder builder, ObjectMapper mapper, String baseUri) { // New ctor
this.builder = builder;
this.mapper = mapper;
updateBaseUri(baseUri);
}
public static ObjectMapper createDefaultObjectMapper() { // New method with old logic
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
mapper.registerModule(new JavaTimeModule());
JsonNullableModule jnm = new JsonNullableModule();
mapper.registerModule(jnm);
return mapper;
}
public static String getDefaultBaseUri() { // New method
return "https://demo-nokia.omnyfy.com/rest/all";
}
public void updateBaseUri(String baseUri) { // New method, old logic
URI uri = URI.create(baseUri);
scheme = uri.getScheme();
host = uri.getHost();
port = uri.getPort();
basePath = uri.getRawPath();
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the Java generator template for the native-library ApiClient and the compilation-validation path; the issue names no files or tests. Done means the default constructor remains backward compatible while a new constructor and reusable default-value methods allow customization of the builder, mapper, and base URI.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100