Skip to content

add tests for https://github.com/oapi-codegen/oapi-codegen/issues/2183#2184

Open
TelpeNight wants to merge 6 commits intooapi-codegen:mainfrom
TelpeNight:bug/2183-explode-comma
Open

add tests for https://github.com/oapi-codegen/oapi-codegen/issues/2183#2184
TelpeNight wants to merge 6 commits intooapi-codegen:mainfrom
TelpeNight:bug/2183-explode-comma

Conversation

@TelpeNight
Copy link

I've added tests for #2183
But maybe adding more test cases would be helpful. It is pretty generic.

SpecQuery can be commented our to prevent comparison. In this case first test will pass, the second will fail.

@mromaszewicz
Copy link
Member

Please have a look at TelpeNight#1, I sent you a PR with the fix. WE can merge all in one once you approve.

There's a limitation dependent on a runtime bug. I'll fix that separately.

@mromaszewicz
Copy link
Member

Oh, and you will need to rebase for CI to pass.

The generated client code was round-tripping styled query parameter
fragments through url.ParseQuery + url.Values.Encode(), which re-encoded
comma delimiters as %2C. Per the OpenAPI spec, form/explode=false arrays
should use literal commas as delimiters (e.g. color=blue,black,brown).

Change the client template to collect pre-encoded fragments from
StyleParamWithLocation and join them directly, bypassing the re-encoding.

Also moves the issue-2183 test to internal/test/issues/issue-2183/,
extends it with explode=true and multi-param cases, and documents the
remaining server-side limitation (oapi-codegen/runtime#91) for values
containing embedded commas.

Fixes oapi-codegen#2183

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@mromaszewicz
Copy link
Member

@greptileai

@greptile-apps
Copy link

greptile-apps bot commented Mar 6, 2026

Greptile Summary

This PR fixes issue #2183 by changing how styled query parameters are assembled in the generated client code: instead of routing them through url.ParseQueryurl.Valuesurl.Values.Encode() (which re-encodes comma delimiters as %2C), the raw fragment returned by runtime.StyleParamWithLocation is now appended directly to a rawQueryFragments slice and joined with &. Non-styled (passthrough / JSON) parameters continue to use url.Values so they remain correctly percent-encoded. An accompanying unit test in internal/test/issues/issue-2183/ validates the serialisation and server-side round-trip for form style parameters with and without explode.

Key observations:

  • The fix correctly preserves literal commas as delimiters for explode=false (e.g. color=blue,black,brown) — the core bug being addressed.
  • The template change introduces a silent ordering change: styled params always appear first (in declaration order), and passthrough/JSON params appear last (sorted alphabetically). A test case mixing both parameter types would make this behaviour explicit.
  • The SkipServerRoundTrip flag in the test acknowledges a known remaining limitation — values that contain commas (e.g. c%2Cd) cannot currently survive a server-side round-trip without changes to the runtime library. This is clearly documented in the test comments.
  • The new issue-2183 test directory contains only the test file, with no OpenAPI spec or code-generation config, which is intentional here since the test exercises the runtime library directly rather than generated code.

Confidence Score: 4/5

  • Safe to merge — the fix is well-scoped and the test coverage is adequate for the core bug, with only minor gaps around mixed parameter types and ordering edge cases.
  • The template change is straightforward and the generated .gen.go files in the PR confirm it produces the expected output. The two minor issues (missing mixed styled/non-styled test case, undocumented ordering change for base-URL query params) are quality-of-life gaps rather than correctness blockers.
  • No files require special attention beyond the noted style suggestions.

Important Files Changed

Filename Overview
internal/test/issues/issue-2183/communication_test.go New test file verifying the client-side serialization and server-side round-trip for OpenAPI query parameter styles. Missing test coverage for mixed styled + non-styled (passthrough/JSON) parameter scenarios.
pkg/codegen/templates/client.tmpl Core template fix: styled params now bypass url.ParseQuery/url.Values.Encode to preserve comma delimiters. Introduces a behavior change where styled params always appear before passthrough/JSON params in the final query string.

Last reviewed commit: 917dbdc

mromaszewicz
mromaszewicz previously approved these changes Mar 6, 2026
@mromaszewicz
Copy link
Member

Could you allow maintainers to push changes to your branch in the PR settings so that I can resolve the conflicts and merge, or could you do a rebase and fix them yourself?

@TelpeNight
Copy link
Author

Hello. I've opened access to maintainers.

@TelpeNight
Copy link
Author

TelpeNight commented Mar 9, 2026

I've tried to resolve conflicts myself. But after regenerating tests, some of them start to fail.
For example internal/test/issues/issue-2031/prefer/issue2031_test.go
Old version did encode user_ids[] as "user_ids%5B%5D". This seems to be correct.
https://www.codestudy.net/blog/is-array-syntax-using-square-brackets-in-url-query-strings-valid/
https://swagger.io/specification/#percent-encoding-and-form-urlencoded

According to the specification [] should be encoded even if allowReserved is true.

New implementation seems to violate this. So I don't think it is ready to be merged.

# Conflicts:
#	internal/test/any_of/param/param.gen.go
#	internal/test/parameters/parameters.gen.go
#	internal/test/schemas/schemas.gen.go
#	pkg/codegen/templates/client.tmpl
@mromaszewicz
Copy link
Member

I resolved the conflicts, but we've caused a behavior regression with these changes (the CI will fail). I'm digging into it more. There is a behavioral change in how parameters are encoded now user_ids[]=1 isn't RFC compliant, but widely accepted in various tools, while user_ids%5B%5D=1 is spec compliant.

@mromaszewicz mromaszewicz dismissed their stale review March 10, 2026 17:09

I need to think about this more, it's complex.

@mromaszewicz
Copy link
Member

I pushed a fix which still encodes parameter names, but honestly, I am unsure if this is proper behavior.

There's also an OpenAPI parameter property, allowReserved which we are ignoring.

This code is also very old with an .IsStyled check in the template, which was added after basic parameters were handled. Honestly, everything should go through through the runtime.StyleParameter. I think I need to do some refactoring.

I've removed the approval for this PR for the moment while I think about this.

@TelpeNight
Copy link
Author

I believe that the better approach is to be spec compliant. Even allowReserved doesn't allow gen-delims. They should be escaped. Just like & character from the same set, as it would screw a query up.

@TelpeNight
Copy link
Author

After studding the runtime source a little bit, I think that it lacks a good test suit for serialisation/deserialisation process. The case I've started this issue from comes directly from oapi spec. Nothing extra-ordinal to be honest.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants