-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
[go] fix: explode object query parameters #24797
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
base: master
Are you sure you want to change the base?
Changes from all commits
6d0e539
470b7e0
b26fefe
a60dc9e
8d3e24b
cd901bc
d30fe87
9c18dd2
d5cf44b
72692c6
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 |
|---|---|---|
|
|
@@ -141,6 +141,35 @@ public void testPrimitiveTypeInOneOf() throws IOException { | |
| TestUtils.assertFileNotContains(modelFile, "dst.int32"); | ||
| } | ||
|
|
||
| @Test(description = "Verify form style query parameters explode an object instead of bracketing it") | ||
| public void testExplodedObjectQueryParameter() throws IOException { | ||
| File output = Files.createTempDirectory("test").toFile(); | ||
| output.deleteOnExit(); | ||
|
|
||
| final CodegenConfigurator configurator = new CodegenConfigurator() | ||
| .setGeneratorName("go") | ||
| .setInputSpec("src/test/resources/3_0/exploded-object-query-param.yaml") | ||
| .setOutputDir(output.getAbsolutePath().replace("\\", "/")); | ||
|
|
||
| DefaultGenerator generator = new DefaultGenerator(); | ||
| List<File> files = generator.opts(configurator.toClientOptInput()).generate(); | ||
| files.forEach(File::deleteOnExit); | ||
|
|
||
| TestUtils.assertFileContains(Paths.get(output + "/client.go"), | ||
| "keyPrefixForMapEntry = k.String()", | ||
| "if !ok { continue } if entry.Kind() == reflect.Slice {", | ||
| "case reflect.Ptr: if v.IsNil() { return }", | ||
| "styleForElement = \"\""); | ||
|
|
||
| // the api passes the declared style through | ||
| Path api = Paths.get(output + "/api_default.go"); | ||
| TestUtils.assertFileContains(api, | ||
| "parameterAddToHeaderOrQuery(localVarQueryParams, \"filter\", r.filter, \"form\", \"\")", | ||
|
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. P1: These assertions expect the generated api_default.go to pass "form" to parameterAddToHeaderOrQuery for filter and typedFilter, but GoClientCodegen only sets codegenParameter.style when the OpenAPI document declares a style. The fixture leaves style unset for those two params (they default to form by spec, not by the codegen), so the generated calls carry an empty style ( Prompt for AI agents
Contributor
Author
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. Not valid — the test passes. ( The first step of the reasoning is right: The regenerated samples show it directly. parameterAddToHeaderOrQuery(localVarQueryParams, "query", r.query, "form", "")not the |
||
| "parameterAddToHeaderOrQuery(localVarQueryParams, \"typedFilter\", r.typedFilter, \"form\", \"\")", | ||
| "parameterAddToHeaderOrQuery(localVarQueryParams, \"deepFilter\", r.deepFilter, \"deepObject\", \"\")", | ||
| "parameterAddToHeaderOrQuery(localVarQueryParams, \"flatFilter\", r.flatFilter, \"form\", \"\")"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNullableComposition() throws IOException { | ||
| File output = Files.createTempDirectory("test").toFile(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| openapi: 3.0.3 | ||
| info: | ||
| title: Exploded object query parameters | ||
| description: > | ||
| Object typed query parameters under form/explode (as a free-form object and as a typed map), deepObject, and form without explode. The free-form variant matters because it is flagged isMap but not isContainer. | ||
| version: 1.0.0 | ||
| servers: | ||
| - url: localhost:8080 | ||
| paths: | ||
| /items: | ||
| get: | ||
| operationId: listItems | ||
| parameters: | ||
| # style and explode both left out, so the form/true defaults apply: every entry | ||
| # becomes its own parameter, keyed by the property name alone. | ||
| - in: query | ||
| name: filter | ||
| schema: | ||
| type: object | ||
| # the same, but declared as a map rather than as a free-form object | ||
| - in: query | ||
| name: typedFilter | ||
| schema: | ||
| type: object | ||
| additionalProperties: | ||
| type: string | ||
| # deepObject nests each entry under the parameter name: deepFilter[key]=value | ||
| - in: query | ||
| name: deepFilter | ||
| style: deepObject | ||
| explode: true | ||
| schema: | ||
| type: object | ||
| # form without explode keeps a single parameter carrying the whole object | ||
| - in: query | ||
| name: flatFilter | ||
| style: form | ||
| explode: false | ||
| schema: | ||
| type: object | ||
| responses: | ||
| '200': | ||
| description: a list of items | ||
| content: | ||
| application/json: | ||
| schema: | ||
| type: array | ||
| items: | ||
| type: string |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
P2: The
if style == "form"branch explodes every form-style object to per-entry keys even whenexplode: false(e.g. flatFilter), because Go never passesexplodeto the runtime. This produceskey=valueentries for flatFilter instead of the single comma-joinedflatFilter=key,valuethe new fixture documents. The new Go test checks only the call-string and not the wire output, so it does not catch the mismatch; either propagate explode into the runtime or make the test verify the actual serialized output.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.
Known — this is the "Known gaps, called out deliberately" section of the description, and the same caveat is repeated in a comment in the go test.
The go templates pass only
styleto the runtime, neverexplode, soparameterAddToHeaderOrQuerygenuinely cannot tell the two apart. ForflatFilterthe spec asks forflatFilter=category,books; go bracketed it (flatFilter[category]=books) before this change and explodes it (category=books) after. Both are wrong, so this is not a regression in correctness — but I agree it is not a fix either, which is why it is documented rather than claimed.On the test: it asserts on the emitted source because there is no harness in the repo that exercises
parameterAddToHeaderOrQueryat runtime — nothing undersamples/**/*_test.goreferences it. Verifying actual wire output would mean adding that harness, which I am glad to do if it is wanted.Plumbing explode properly changes
parameterAddToHeaderOrQuery's signature and all 12 call sites ingo/api.mustache. I offered that in the description and the offer stands — it just wants a maintainer's call, since it changes a runtime function signature that generated code has depended on for a long time.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.
Two corrections: the
explode: falsecaveat now lives only in the description (d5cf44b dropped the test comment), andTestQueryDeepObjectinsamples/openapi3/client/petstore/go/fake_api_test.godoes exerciseparameterAddToHeaderOrQueryat runtime; it just has no form-style map parameter to reach the new branch.