Repository navigation
[Java][jersey2/jersey3] Fix serialization of array path parameters #24986
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
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 |
|---|---|---|
|
|
@@ -5351,6 +5351,27 @@ public void testInsecureTlsHookOmittedWhenDisabled(String library) { | |
| .doesNotContain("import java.security.cert.X509Certificate;"); | ||
| } | ||
|
|
||
| @Test(dataProvider = "jerseyLibraries") | ||
| public void testArrayPathParamSerializedAsCollection(String library) { | ||
| Path output = newTempFolder(); | ||
| CodegenConfigurator configurator = new CodegenConfigurator() | ||
| .setGeneratorName(JAVA_GENERATOR) | ||
| .setLibrary(library) | ||
| .addAdditionalProperty(CodegenConstants.INVOKER_PACKAGE, "xyz.abcdef.invoker") | ||
| .addAdditionalProperty(CodegenConstants.API_PACKAGE, "xyz.abcdef.api") | ||
| .setInputSpec("src/test/resources/3_0/java/array-path-param.yaml") | ||
| .setOutputDir(output.toString().replace("\\", "/")); | ||
|
|
||
| new DefaultGenerator().opts(configurator.toClientOptInput()).generate(); | ||
|
|
||
| assertThat(output.resolve("src/main/java/xyz/abcdef/api/DefaultApi.java")).content() | ||
| .contains("apiClient.collectionPathParameterToString(\"csv\", ids)") | ||
| .contains("apiClient.escapeString(id.toString())") | ||
| .doesNotContain("apiClient.escapeString(ids.toString())"); | ||
| JavaFileAssert.assertThat(output.resolve("src/main/java/xyz/abcdef/invoker/ApiClient.java").toFile()) | ||
|
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 test proves the template invokes Prompt for AI agents |
||
| .assertMethod("collectionPathParameterToString", "String", "Collection<?>"); | ||
| } | ||
|
|
||
| private static Path generateJerseyClient(String library, Boolean generateInsecureTlsHook) { | ||
| Path output = newTempFolder(); | ||
| CodegenConfigurator configurator = new CodegenConfigurator() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| openapi: 3.0.3 | ||
| info: | ||
| title: Array path param | ||
| version: 1.0.0 | ||
| paths: | ||
| /items/{ids}/action: | ||
| get: | ||
| operationId: getItems | ||
| parameters: | ||
| - name: ids | ||
| in: path | ||
| required: true | ||
| schema: | ||
| type: array | ||
| items: | ||
| type: string | ||
| responses: | ||
| '204': | ||
| description: ok | ||
| /items/{id}: | ||
| get: | ||
| operationId: getItem | ||
| parameters: | ||
| - name: id | ||
| in: path | ||
| required: true | ||
| schema: | ||
| type: string | ||
| responses: | ||
| '204': | ||
| description: ok |
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.
P3: An empty (non-null) required array path parameter serializes to an empty string, so
{ids}is replaced with an empty segment and the path becomes/items//action. The generated Api method only null-checks required params, so an empty list reaches this method. Compare the old behavior (escapeString("[]")→%5B%5D), which could not match any route, whereas an empty segment is liable to slash-collapsing/normalization by servers and can change route resolution. Return a placeholder or document this degenerate case explicitly.Prompt for AI agents
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.
Returning an empty string is intentional: for
style: simple, RFC 6570 expands an empty list to an empty string, and thecollectionPathParameterToStringhelpers in the resttemplate, webclient and restclient libraries behave the same way. The previous%5B%5Doutput was also an invalid path. A placeholder value would send a request the spec never defined, so I've kept this consistent with the other Java libraries.