Skip to content

[BUG] [JAVA] HttpClient memberVarHttpClient not closed #24942

Description

@jglick

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I noticed that the XxxApi.java file generated when using library=native calls apiClient.getHttpClient() and stores the resulting HttpClient in a private field, but never calls .close() on it. (This class was made AutoCloseable in Java 21, and closing it is important to release resources.) Clients of the library could not easily work around this; they would have to pass in a special HttpClient.Builder which tracked all the times .build() was called, and retain weak references so they could be closed later.

openapi-generator version

openapi-generator-maven-plugin version 7.25.0

OpenAPI declaration file content or url

(see steps to reproduce)

Generation Details

(see steps to reproduce)

Steps to reproduce
git clone https://github.com/jenkinsci/cursor-origin-branch-source-plugin .
git checkout 2b2e1c5beaac428488aa5406e0d38721a572d57b
mvn -Pquick-build package
fgrep memberVarHttpClient target/generated-sources/openapi/io/jenkins/plugins/cursor_origin_branch_source/origin_openapi/api/OriginServiceApi.java

Expected: some call to memberVarHttpClient.close(). Actual: none, just created and then used to make requests.

NOTE: I tried to reproduce in trunk (f99cf3b) but compilation failed for some apparently unrelated reason: missing Enum.java. (Bisected this to #24812.) OriginServiceApi.java was still generated the same way, still compilable, still missing .close().

Related issues/PRs

(none found)

Suggest a fix

Whatever template change would effect

116,116c116,116
< public class OriginServiceApi {
---
> public class OriginServiceApi implements AutoCloseable {
158a159,162
>   @Override
>   public void close() {
>      memberVarHttpClient.close();
>   }

when configured for Java 21+.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions