Skip to content

fix: send exploded form arrays as repeated keys in client form bodies - #2677

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/client-form-encoding
Nov 20, 2026
Merged

mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/client-form-encoding

Conversation

@mromaszewicz

@mromaszewicz mromaszewicz commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Fixes #2604. Depends on runtime v1.9.1 (oapi-codegen/runtime#187), which this bumps in internal/test and examples.

Problem

For application/x-www-form-urlencoded request bodies, the client always called runtime.MarshalForm(body, nil), so the spec's encoding never reached the runtime. An array was sent with indexed keys (topic[0]=a&topic[1]=b) even when it declared style: form, explode: true, which serializes as repeated keys (topic=a&topic=b). A server which reads repeated keys saw no value at all.

Change

The client now passes one declared encoding through to MarshalForm: an array of primitives in the exploded form style. For the spec in #2604 the request builder becomes:

bodyStr, err := runtime.MarshalForm(body, func() map[string]runtime.RequestBodyEncoding {
	explode := true
	return map[string]runtime.RequestBodyEncoding{
		"topic": {Style: "form", Explode: &explode},
	}
}())

The encodings are built inside a function literal so that the explode variable cannot collide with a path parameter of the same name, which is an argument of the request builder. (new(true) is not an option: the net/http output is tagged //go:build go1.22.)

A property qualifies when all of these hold:

  • Its encoding declares style: form, explode: true, or both, and nothing to the contrary. form is the default style and explodes by default, so style: form alone and explode: true alone both count. An encoding which declares neither does not.
  • Its encoding has no contentType.
  • Its schema is plainly an array of strings, integers, numbers or booleans: not nullable, no x-go-type on the array or its items, no oneOf/anyOf/allOf items, and not format: binary or byte.

The property is looked up on the body schema and through its allOf members, so referenced and composed bodies work. The same change is made in initiator.tmpl for webhook and callback initiators.

Why only this encoding

Generated strict servers bind form bodies with runtime.BindForm(&body, form, nil, nil), without encodings. Repeated keys are the only styled serialization that path reads back the same as the indexed keys the client writes otherwise. Honoring the rest on the client alone (explode: false, spaceDelimited, pipeDelimited, styled objects, contentType) would break clients and servers generated from the same spec, so those are still ignored exactly as before. Doing both sides properly is tracked in #2676.

What does not change

  • Bodies with no qualifying encoding generate the same code as before. make generate changes no existing generated file.
  • Properties with any other declared encoding are still sent with indexed and bracketed keys.
  • Generated servers are untouched.

What does change, beyond the keys

For the arrays which qualify, values are now formatted as query parameters are, rather than with fmt.Sprint. For most values that is identical. Two cases differ, both for the better and both still parsed by generated servers:

  • Large or small floats are written in plain decimal, 1000000 rather than 1e+06.
  • date-time items are written as RFC 3339 rather than Go's time.Time.String() format.

Code generated by this version still compiles against an older runtime, which ignores the style, so the fix only takes effect with runtime v1.9.1 or later.

Tests

internal/test/bodies/form_encoding generates a client and a strict net/http server from one spec and sends real requests between them, checking both the body on the wire and what the server bound:

  • Exploded form arrays are sent as repeated keys, whether the encoding declares style and explode, only the style, or only explode.
  • The same for a $ref body whose array comes from an allOf member.
  • Every other encoding (explode: false, pipeDelimited, deepObject, contentType, arrays of objects, objects, a name which is not a property) is unchanged on the wire and still binds.
  • A body with no encoding is unchanged.
  • An operation with a path parameter named explode still compiles and round-trips.

The initiator template gets the identical change but has no form-body fixture of its own.

🤖 Generated with Claude Code

@mromaszewicz
mromaszewicz requested a review from a team as a code owner November 20, 2026 16:47
@socket-security

socket-security Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updatedgithub.com/​oapi-codegen/​runtime@​v1.9.0 ⏵ v1.9.194 +1100100100100

View full report

@greptile-apps

greptile-apps Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the previous compilation issue is fixed, and no new actionable issues were found.

Summary

The PR sends qualifying exploded form arrays as repeated keys in generated clients and initiators.

  • Exploded form arrays now use repeated keys in generated clients.

Reviews (2) · Last reviewed commit: "fix: send exploded form arrays as repeat..." · Reviewed by Greptile

Comment thread pkg/codegen/templates/client.tmpl Resolved
For application/x-www-form-urlencoded request bodies the client always
called runtime.MarshalForm(body, nil), so the spec's encoding never
reached the runtime and an array was sent with indexed keys
(topic[0]=a&topic[1]=b) even when it declared style: form,
explode: true, which serializes as repeated keys (topic=a&topic=b).
Servers which read repeated keys saw no value at all.

The client now passes that one encoding through: an array of primitives
which declares the form style, explode: true, or both, and nothing to
the contrary. Runtime v1.9.1 serializes it as repeated keys, which
generated servers already bind the same as indexed keys.

Every other declared encoding is still ignored, as before. Generated
servers bind form bodies without their encodings, so honoring the rest
on the client alone would break clients and servers generated from the
same spec. Doing both sides is tracked in oapi-codegen#2676.

Bodies with no such encoding generate exactly the code they did.

Fixes oapi-codegen#2604

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@mromaszewicz
mromaszewicz force-pushed the fix/client-form-encoding branch from 6a96104 to 8d53f41 Compare November 20, 2026 16:54
@mromaszewicz
mromaszewicz merged commit b5847d1 into oapi-codegen:main Nov 20, 2026
13 checks passed
@mromaszewicz
mromaszewicz deleted the fix/client-form-encoding branch November 20, 2026 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client form bodies ignore encoding style/explode, so arrays are always sent as name[0]=…

1 participant