-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
[Java][jersey3] Add error entity deserialization to ApiException #23542
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
Open
Chhida
wants to merge
15
commits into
OpenAPITools:master
Choose a base branch
from
Chhida:feature/jersey3-error-deserialization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8791c1b
[Java][jersey3] Add error entity deserialization to ApiException
Chhida b959b91
Add unit tests for errorEntity deserialization feature
Chhida be83c27
Fix forbidden API check: specify UTF-8 charset
Chhida 3ec7cc4
chore: regenerate Java jersey3 samples with errorEntity feature
Chhida 1acccd6
fix: return null instead of error message on deserialization failure
Chhida c43dea8
chore: regenerate jersey3-oneOf sample with errorEntity feature
Chhida 48ad522
test: add functional test for errorEntity deserialization
Chhida 6ac9def
test: fix path issue in functional test
Chhida 4c9242f
fix: address review comments from wing328
Chhida 370875b
chore: regenerate jersey3 samples after adding docstring to deseriali…
Chhida 3dbdad6
chore: regenerate samples without generation timestamp
Chhida f719489
fix: add missing dependencies for jersey3 samples
Chhida 5bc693d
feat(jersey3): add error entity deserialization support
Chhida a967b51
feat(jersey3): add error entity deserialization support
Chhida 5dedcc8
feat(jersey3): add error entity deserialization support
Chhida File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -195,12 +195,20 @@ public class {{classname}} { | |
| {{#hasAuthMethods}} | ||
| String[] localVarAuthNames = {{=% %=}}new String[] {%#authMethods%"%name%"%^-last%, %/-last%%/authMethods%};%={{ }}=% | ||
| {{/hasAuthMethods}} | ||
| final Map<String, GenericType> localVarErrorTypes = new HashMap<String, GenericType>(); | ||
| {{#responses}} | ||
| {{^-first}} | ||
|
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: Error-type map population skips the first response unconditionally, which can omit a valid error/default schema and break ApiException error deserialization. Prompt for AI agents |
||
| {{#dataType}} | ||
| localVarErrorTypes.put("{{code}}", new GenericType<{{{dataType}}}>() {}); | ||
| {{/dataType}} | ||
| {{/-first}} | ||
| {{/responses}} | ||
| {{#returnType}} | ||
| GenericType<{{{returnType}}}> localVarReturnType = new GenericType<{{{returnType}}}>() {}; | ||
| {{/returnType}} | ||
| return apiClient.invokeAPI("{{classname}}.{{operationId}}", {{#hasPathParams}}localVarPath{{/hasPathParams}}{{^hasPathParams}}"{{{path}}}"{{/hasPathParams}}, "{{httpMethod}}", {{#queryParams}}{{#-first}}localVarQueryParams{{/-first}}{{/queryParams}}{{^queryParams}}new ArrayList<>(){{/queryParams}}, {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}, | ||
| {{#headerParams}}{{#-first}}localVarHeaderParams{{/-first}}{{/headerParams}}{{^headerParams}}new LinkedHashMap<>(){{/headerParams}}, {{#cookieParams}}{{#-first}}localVarCookieParams{{/-first}}{{/cookieParams}}{{^cookieParams}}new LinkedHashMap<>(){{/cookieParams}}, {{#formParams}}{{#-first}}localVarFormParams{{/-first}}{{/formParams}}{{^formParams}}new LinkedHashMap<>(){{/formParams}}, localVarAccept, localVarContentType, | ||
| {{#hasAuthMethods}}localVarAuthNames{{/hasAuthMethods}}{{^hasAuthMethods}}null{{/hasAuthMethods}}, {{#returnType}}localVarReturnType{{/returnType}}{{^returnType}}null{{/returnType}}, {{#bodyParam}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}{{/bodyParam}}{{^bodyParam}}false{{/bodyParam}}); | ||
| {{#hasAuthMethods}}localVarAuthNames{{/hasAuthMethods}}{{^hasAuthMethods}}null{{/hasAuthMethods}}, {{#returnType}}localVarReturnType{{/returnType}}{{^returnType}}null{{/returnType}}, {{#bodyParam}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}{{/bodyParam}}{{^bodyParam}}false{{/bodyParam}}, localVarErrorTypes); | ||
| } | ||
| {{#vendorExtensions.x-group-parameters}} | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...test/java/org/openapitools/codegen/java/jersey3/JavaJersey3ErrorEntityFunctionalTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.openapitools.codegen.java.jersey3; | ||
|
|
||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static org.testng.Assert.*; | ||
|
|
||
| /** | ||
| * Functional test for errorEntity deserialization feature. | ||
| * | ||
| * This test verifies that the generated code includes the errorEntity | ||
| * field and getErrorEntity() method by examining the generated templates. | ||
| * | ||
| * Full integration tests would require: | ||
| * 1. A running mock HTTP server | ||
| * 2. Generated client code compiled and executed | ||
| * 3. Actual API calls to verify runtime behavior | ||
| * | ||
| * The client's original project (BudgetApiTest) provides this type of | ||
| * functional testing. This test verifies the template structure is correct. | ||
| */ | ||
| public class JavaJersey3ErrorEntityFunctionalTest { | ||
|
|
||
| private static final String JERSEY3_TEMPLATE_DIR = | ||
| "src/main/resources/Java/libraries/jersey3/"; | ||
|
|
||
| /** | ||
| * Verify generated code includes errorEntity field in ApiException | ||
| */ | ||
| @Test | ||
| public void testGeneratedApiExceptionHasErrorEntity() throws Exception { | ||
| String template = readTemplate("apiException.mustache"); | ||
| assertNotNull(template); | ||
|
|
||
| // Verify errorEntity field exists | ||
| assertTrue(template.contains("private Object errorEntity = null"), | ||
| "Generated ApiException should have errorEntity field"); | ||
|
|
||
| // Verify getErrorEntity() method exists | ||
| assertTrue(template.contains("public Object getErrorEntity()"), | ||
| "Generated ApiException should have getErrorEntity() method"); | ||
|
|
||
| // Verify constructor with errorEntity parameter | ||
| assertTrue(template.contains("Object errorEntity"), | ||
| "Generated ApiException should accept errorEntity in constructor"); | ||
| } | ||
|
|
||
| /** | ||
| * Verify generated code includes deserializeErrorEntity method | ||
| */ | ||
| @Test | ||
| public void testGeneratedApiClientHasDeserializeErrorEntity() throws Exception { | ||
| String template = readTemplate("ApiClient.mustache"); | ||
| assertNotNull(template); | ||
|
|
||
| // Verify deserializeErrorEntity method exists | ||
| assertTrue(template.contains("deserializeErrorEntity"), | ||
| "Generated ApiClient should have deserializeErrorEntity method"); | ||
|
|
||
| // Verify errorTypes parameter handling | ||
| assertTrue(template.contains("Map<String, GenericType> errorTypes"), | ||
| "Generated ApiClient should handle errorTypes parameter"); | ||
| } | ||
|
|
||
| /** | ||
| * Verify generated API methods build error types map | ||
| */ | ||
| @Test | ||
| public void testGeneratedApiMethodsBuildErrorTypesMap() throws Exception { | ||
| String template = readTemplate("api.mustache"); | ||
| assertNotNull(template); | ||
|
|
||
| // Verify localVarErrorTypes is built | ||
| assertTrue(template.contains("localVarErrorTypes"), | ||
| "Generated API methods should build localVarErrorTypes"); | ||
|
|
||
| // Verify error types are put into the map | ||
| assertTrue(template.contains("localVarErrorTypes.put"), | ||
| "Generated API methods should put error types into map"); | ||
| } | ||
|
|
||
| /** | ||
| * Verify null is returned when deserialization fails | ||
| */ | ||
| @Test | ||
| public void testDeserializationReturnsNullOnFailure() throws Exception { | ||
| String template = readTemplate("ApiClient.mustache"); | ||
| assertNotNull(template); | ||
|
|
||
| // Verify that on exception, null is returned (not error message) | ||
| // This is the fix we applied for the P2 issue | ||
| assertFalse(template.contains("String.format(\"Failed deserializing"), | ||
| "deserializeErrorEntity should return null, not error message string"); | ||
| } | ||
|
|
||
| /** | ||
| * Helper method to read template files | ||
| */ | ||
| private String readTemplate(String templateName) throws Exception { | ||
| java.nio.file.Path templatePath = java.nio.file.Paths.get(JERSEY3_TEMPLATE_DIR + templateName); | ||
| if (java.nio.file.Files.exists(templatePath)) { | ||
| return java.nio.file.Files.readString(templatePath); | ||
| } | ||
| return null; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
please add a docstring explaining what this function does
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.
done