Fix S2095 resource leaks in camel-test-infra-openai-mock#24359
Conversation
Close InputStream from HttpExchange.getRequestBody() using try-with-resources in AudioTranscriptionRequestHandler, EmbeddingRequestHandler, and RequestHandler. Close HttpClient instances in test classes using a finally block with an instanceof AutoCloseable guard (compatible with --release 17 while correctly closing on Java 21+ runtimes where HttpClient implements AutoCloseable). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
🌟 Thank you for your contribution to the Apache Camel project! 🌟 🐫 Apache Camel Committers, please review the following items:
|
|
🧪 CI tested the following changed modules:
All tested modules (13 modules)
|
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR addresses SonarCloud S2095 “resources should be closed” findings in camel-test-infra-openai-mock by fixing real HttpExchange request-body InputStream leaks in mock handlers and suppressing known-false-positive HttpClient warnings in the module’s test classes (Java 17 target).
Changes:
- Close
HttpExchange.getRequestBody()InputStreams via try-with-resources in the OpenAI mock request handlers. - Add method-level
@SuppressWarnings("java:S2095")(with rationale comments) forHttpClient.newHttpClient()usage in tests whereHttpClientis not closeable on the project’s Java target. - Apply minor formatting adjustments to keep code style consistent after the additions.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/RequestHandler.java | Wraps exchange.getRequestBody() in try-with-resources to ensure the request stream is closed. |
| test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/EmbeddingRequestHandler.java | Wraps exchange.getRequestBody() in try-with-resources to close the embedding request stream. |
| test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/AudioTranscriptionRequestHandler.java | Wraps request-body consumption in try-with-resources to avoid leaking the request stream. |
| test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockTest.java | Suppresses S2095 false positives for HttpClient construction in tests (method-level). |
| test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockSimpleAssertionTest.java | Suppresses S2095 false positives for HttpClient construction in the test method. |
| test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockReplyWithToolContentTest.java | Suppresses S2095 false positives for HttpClient construction; keeps formatting tidy. |
| test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockReplyWithAfterToolTest.java | Suppresses S2095 false positives for HttpClient construction in the test method. |
| test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockMultipleToolsTest.java | Suppresses S2095 false positives for HttpClient construction and adjusts line wrapping. |
| test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockFailuresTest.java | Suppresses S2095 false positives for HttpClient construction in failure-path tests. |
| test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockEmbeddingTest.java | Suppresses S2095 false positives for HttpClient construction across embedding tests; minor formatting. |
| test-infra/camel-test-infra-openai-mock/src/main/java/org/apache/camel/test/infra/openai/mock/OpenAIMockConversationHistoryTest.java | Suppresses S2095 false positives for HttpClient construction in the history test. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…loud Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Claude Code on behalf of Guillaume Nodet — Changes incorporated into #24358. |
Summary
Claude Code on behalf of Guillaume Nodet
Fix SonarCloud S2095 (Resources should be closed) in the
camel-langchain4j-chatcomponent.Real resource leak fixes
AudioTranscriptionRequestHandler.java: CloseInputStreamfromHttpExchange.getRequestBody()using try-with-resourcesEmbeddingRequestHandler.java: CloseInputStreamfromHttpExchange.getRequestBody()using try-with-resourcesRequestHandler.java: CloseInputStreamfromHttpExchange.getRequestBody()using try-with-resourcesHttpClient false positives (marked Won't Fix in SonarCloud)
HttpClientdoes not implementAutoCloseablebefore Java 21 (JDK-8304165). On the project's Java 17 target, there is nothing to close. These 17 issues across 8 test files are marked as Won't Fix directly in SonarCloud instead of suppressing in code.Test plan
🤖 Generated with Claude Code