Repository navigation
[java][native] make generated APIs AutoCloseable and close the owned HttpClient #24960
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -65,7 +65,7 @@ import java.util.concurrent.CompletableFuture; | |||||||||||||||||||||||||||||||||
| {{>generatedAnnotation}} | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| {{#operations}} | ||||||||||||||||||||||||||||||||||
| public class {{classname}} { | ||||||||||||||||||||||||||||||||||
| public class {{classname}} implements AutoCloseable { | ||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Utility class for extending HttpRequest.Builder functionality. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
|
|
@@ -955,5 +955,20 @@ public class {{classname}} { | |||||||||||||||||||||||||||||||||
| {{/hasParams}} | ||||||||||||||||||||||||||||||||||
| {{/vendorExtensions.x-group-parameters}} | ||||||||||||||||||||||||||||||||||
| {{/operation}} | ||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Closes the {@link HttpClient} that this instance created from the {@link ApiClient} | ||||||||||||||||||||||||||||||||||
| * it was constructed with. {@code ApiClient.getHttpClient()} builds a new client on | ||||||||||||||||||||||||||||||||||
| * every call, so the client held here is owned exclusively by this instance and is not | ||||||||||||||||||||||||||||||||||
| * shared with any other API instance. | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * <p>{@link HttpClient} only became {@link AutoCloseable} in Java 21, hence the guard; | ||||||||||||||||||||||||||||||||||
| * on earlier runtimes there is no way to release the client's threads early. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||
| public void close() throws Exception { | ||||||||||||||||||||||||||||||||||
| if (memberVarHttpClient instanceof AutoCloseable) { | ||||||||||||||||||||||||||||||||||
| ((AutoCloseable) memberVarHttpClient).close(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+968
to
+972
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Generated APIs force try-with-resources callers to handle checked Prompt for AI agents
Suggested change
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| {{/operations}} | ||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,7 +45,7 @@ | |
| import java.util.function.Consumer; | ||
|
|
||
| @javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.26.0-SNAPSHOT") | ||
| public class AuthApi { | ||
| public class AuthApi implements AutoCloseable { | ||
| /** | ||
| * Utility class for extending HttpRequest.Builder functionality. | ||
| */ | ||
|
|
@@ -367,4 +367,19 @@ private HttpRequest.Builder testAuthHttpBearerRequestBuilder(Map<String, String> | |
| return localVarRequestBuilder; | ||
| } | ||
|
|
||
| /** | ||
| * Closes the {@link HttpClient} that this instance created from the {@link ApiClient} | ||
| * it was constructed with. {@code ApiClient.getHttpClient()} builds a new client on | ||
| * every call, so the client held here is owned exclusively by this instance and is not | ||
| * shared with any other API instance. | ||
| * | ||
| * <p>{@link HttpClient} only became {@link AutoCloseable} in Java 21, hence the guard; | ||
| * on earlier runtimes there is no way to release the client's threads early. | ||
| */ | ||
| @Override | ||
| public void close() throws Exception { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: The new close()-based resource lifecycle has no test coverage in this sample: AuthApiTest.java never calls close() or exercises the AutoCloseable path. Since this sample compiles with maven.compiler.source/target = 11, the Prompt for AI agents |
||
| if (memberVarHttpClient instanceof AutoCloseable) { | ||
| ((AutoCloseable) memberVarHttpClient).close(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: When a specification has a parameterless operationId
close, this template emits both the endpoint method and this lifecycle method with the same Java signature, so generation produces uncompilable code. Reserve or rename colliding operation IDs before rendering the API methods.Prompt for AI agents