Skip to content

[Java][jersey3] Add error entity deserialization to ApiException#23542

Open
Chhida wants to merge 8 commits intoOpenAPITools:masterfrom
Chhida:feature/jersey3-error-deserialization
Open

[Java][jersey3] Add error entity deserialization to ApiException#23542
Chhida wants to merge 8 commits intoOpenAPITools:masterfrom
Chhida:feature/jersey3-error-deserialization

Conversation

@Chhida
Copy link
Copy Markdown

@Chhida Chhida commented Apr 14, 2026

Pull Request Description

Title

[Java][jersey3] Add error entity deserialization to ApiException


Description

Add support for deserializing error response bodies into the model types defined in the OpenAPI specification for the jersey3 library.

Problem

When an OpenAPI spec defines error response schemas (e.g., ErreurApiWrapper for 400 errors), these models are generated but not used by the generated client code. The ApiException only contains the raw response body as a string, requiring manual deserialization.

Solution

  • Add errorEntity field to ApiException to store the deserialized error object
  • Add getErrorEntity() method to retrieve the deserialized error model
  • Build error types map from API responses in each API method
  • Deserialize error response body based on the HTTP status code

Changes

  • apiException.mustache: Add errorEntity field, constructor, and getErrorEntity() method
  • api.mustache: Build localVarErrorTypes map from API responses
  • ApiClient.mustache: Add deserializeErrorEntity method and update invokeAPI signature

Usage

try {
    api.someOperation();
} catch (ApiException e) {
    Object errorEntity = e.getErrorEntity();
    if (errorEntity instanceof ErreurApiWrapper) {
        ErreurApiWrapper error = (ErreurApiWrapper) errorEntity;
        // Handle error
    }
    // Original response body still available
    String responseBody = e.getResponseBody();
}

Testing

  • All 634 Java tests passed (no regressions)
  • Generated sample code verified to contain the new functionality

Related Issues

Checklist

  • Read the contribution guidelines
  • Run tests locally (634 Java tests passed)
  • Generated samples verified

Summary by cubic

Adds automatic error model deserialization to the jersey3 generator so clients get typed error objects instead of raw strings. ApiException now includes a deserialized error entity while preserving the raw response body.

  • New Features

    • ApiException: added errorEntity and getErrorEntity().
    • ApiClient: added deserializeErrorEntity; buffers the entity and attaches it to ApiException. Backward compatible invokeAPI; new signature accepts an errorTypes map.
    • API methods: generate and pass a status-code → GenericType map for error responses (uses "0" for default).
    • Samples/tests: regenerated jersey3 and jersey3-oneOf samples; added JavaJersey3ErrorEntityTest (8 cases) and JavaJersey3ErrorEntityFunctionalTest. 642 Java tests passed.
  • Bug Fixes

    • Error entity: return null when deserialization fails instead of a synthetic message.
    • Tests: specify UTF-8 charset to satisfy forbidden API checks.
    • Functional tests: corrected template path; all 4 functional tests now pass.

Written for commit 6ac9def. Summary will update on new commits.

- Add errorEntity field and getErrorEntity() method to ApiException
- Add deserializeErrorEntity method to ApiClient
- Pass errorTypes map from API methods to invokeAPI
- Enables automatic deserialization of error response bodies
- Fixes OpenAPITools#4777
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

Chhida added 2 commits April 14, 2026 17:29
- Add JavaJersey3ErrorEntityTest with 8 test cases
- Tests verify template changes for errorEntity feature
- 642 Java tests passed - no regressions
- Fixes OpenAPITools#4777
The test was using String(byte[]) without specifying charset, which
is flagged by forbiddenapis as using the default charset.
@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 14, 2026

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 14, 2026

please follow step 3 to update the samples as well so that CI can verify the change

- Regenerate samples to verify templates work correctly
- ApiException now contains errorEntity and getErrorEntity()
- API methods include localVarErrorTypes for error deserialization
- Fixes OpenAPITools#4777
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 9 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/ApiClient.java">

<violation number="1" location="samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/ApiClient.java:1366">
P2: Error deserialization fallback returns a synthetic String on failure, producing non-null `errorEntity` values instead of `null` and changing error-entity semantics.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@Chhida Chhida force-pushed the feature/jersey3-error-deserialization branch from f411d96 to 3ec7cc4 Compare April 14, 2026 19:50
When deserializeErrorEntity fails, return null instead of a synthetic
String message to maintain correct errorEntity semantics (null on failure).

Fixes P2 issue from cubic-dev-ai review.
@Chhida
Copy link
Copy Markdown
Author

Chhida commented Apr 14, 2026

please follow step 3 to update the samples as well so that CI can verify the change

Hi @wing328,

Thanks for the feedback!

I’ve updated the samples as requested (step 3), and all commits are now correctly linked to my GitHub account.

Everything should be ready for review.

Thanks!

@Chhida
Copy link
Copy Markdown
Author

Chhida commented Apr 15, 2026

please follow step 3 to update the samples as well so that CI can verify the change

Hi @wing328,

Thanks for the feedback!

I’ve updated the samples as requested (step 3), and all commits are now correctly linked to my GitHub account.

Everything should be ready for review.

Thanks!

Hi @wing328,

I've verified that the KotlinReservedWordsTest failure is a pre-existing flaky test - it passes locally (101 tests, 0 failures). This is unrelated to the jersey3 errorEntity changes.

Could you please confirm this and proceed with the review?

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 15, 2026

cc @bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08)

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 15, 2026

is it correct to say that you've been using this fix in your production environment and it has been working fine for your use cases?

Chhida added 2 commits April 15, 2026 15:09
- Add JavaJersey3ErrorEntityFunctionalTest
- Verifies generated templates include errorEntity field and methods
- Verifies deserializeErrorEntity returns null on failure (P2 fix)
- Related to issue OpenAPITools#4777 and PR OpenAPITools#23542
- Correct the JERSEY3_TEMPLATE_DIR path
- All 4 functional tests now pass
@Chhida
Copy link
Copy Markdown
Author

Chhida commented Apr 15, 2026

is it correct to say that you've been using this fix in your production environment and it has been working fine for your use cases?

Hi @wing328,

Yes, the fix has been verified and is working correctly:

  1. Unit tests - 642 Java tests pass with no regressions
  2. Integration tests - 4 tests pass in the generated jersey3 sample, verifying:
    • errorEntity is correctly populated in ApiException
    • Backward compatibility (errorEntity is null when not provided)
    • Returns null on deserialization failure
    • Works with any object type

The functionality has been verified through tests and confirmed by our team.

We're waiting for approval of the PR so we can deploy it in our production environment.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] ApiException does not contains Error object

2 participants