Skip to content

fix: honour style and explode in MarshalForm encodings - #187

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/marshal-form-style-explode
Nov 20, 2026
Merged

mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/marshal-form-style-explode

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Fixes #186. Runtime half of oapi-codegen/oapi-codegen#2604.

Problem

MarshalForm only read ContentType from a property's RequestBodyEncoding, so an array was always written with indexed keys (topic[0]=a&topic[1]=b), even when the encoding declared style: form, explode: true, which serializes as repeated keys (topic=a&topic=b). Servers that read repeated keys saw no value at all.

Change

A property whose encoding sets Style or Explode is now serialized the way a query parameter of that style would be:

style explode array ["a", "b"]
form true (default) topic=a&topic=b
form false topic=a,b
spaceDelimited false (default) topic=a b
pipeDelimited false (default) topic=a|b
deepObject topic[0]=a&topic[1]=b (the existing output)
  • Explode defaults to true for form and false for the other styles, as in the specification. Style defaults to form when only Explode is set.
  • Flat objects (structs and maps) follow the same styles: exploded, each property becomes a field of its own; otherwise name=k,v,k,v.
  • simple, label and matrix are rejected, as are arrays of objects under a style other than deepObject.
  • A property with no encoding, or an encoding that sets neither Style nor Explode, keeps the bracketed output. Generated code passes nil today, so existing callers are unaffected.

styleStruct and styleMap have their field-flattening loops extracted into structFieldDict and mapFieldDict so the form path can share them; their behaviour is unchanged.

Also fixed: the ContentType branch

The application/json branch returned unsupported encoding, only application/json is supported even for application/json, since the error return wasn't in an else, and it marshaled the reflect.Value rather than the value it holds. It now writes the JSON and carries on, and skips nil optional properties. This has to work before the generator can pass encodings through.

Notes

  • BindForm is untouched. It treats an unset Explode as false, which differs from the default here, but generated servers pass it nil encodings and the default binding already accepts repeated keys, name[] and name[i].
  • An encoding with a non-JSON ContentType still errors, as before.

🤖 Generated with Claude Code

MarshalForm only read ContentType from a property's encoding, so an
array was always written with indexed keys (topic[0]=a&topic[1]=b), even
when the encoding declared style: form, explode: true, which serializes
as repeated keys (topic=a&topic=b).

A property whose encoding sets Style or Explode is now serialized as a
query parameter of that style would be: form, spaceDelimited and
pipeDelimited repeat the key when exploded and write one delimited value
otherwise, and deepObject is the bracketed default. Explode defaults to
true for form and false otherwise, as in the specification. Properties
with no encoding, or one that sets neither field, are unchanged.

Also fix the ContentType branch, which returned "unsupported encoding"
for application/json too and marshaled the reflect.Value rather than the
value it holds.

Fixes oapi-codegen#186
Refs oapi-codegen/oapi-codegen#2604

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner November 20, 2026 15:35
@mromaszewicz
mromaszewicz merged commit c67d5d3 into oapi-codegen:main Nov 20, 2026
10 checks passed
@mromaszewicz mromaszewicz added the bug Something isn't working label Nov 20, 2026
@mromaszewicz
mromaszewicz deleted the fix/marshal-form-style-explode branch November 20, 2026 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MarshalForm ignores the style and explode of a property's encoding

1 participant