Skip to content

fix: bind styled form arrays and JSON fields - #201

Open
go165 wants to merge 1 commit into
oapi-codegen:mainfrom
go165:codex/bind-urlencoded-array-and-json-fields
Open

go165 wants to merge 1 commit into
oapi-codegen:mainfrom
go165:codex/bind-urlencoded-array-and-json-fields

Conversation

@go165

@go165 go165 commented Dec 2, 2026

Copy link
Copy Markdown

Problem

MarshalForm can encode repeated or delimited arrays, but BindForm consumes only the first value and delegates to the query-parameter binder. Some form styles fail to bind. An encoded field also skips indexed or bracketed keys from older clients when its plain key is absent. For an application/json field, the decoder writes into the entire destination and returns an unsupported encoding error.

Change

Use the same form style defaults and delimiters for reading and writing arrays. Bind repeated values in order, allocate optional typed slices through the existing form binder, and accept the older indexed or bracketed keys when a field has no plain key. Decode JSON values into their own fields and consume already-decoded scalar values directly.

This covers the styled arrays and JSON-valued fields in the runtime work described in oapi-codegen/oapi-codegen#2676. That issue also tracks styled flat objects and generator template changes.

Verification

  • On upstream ee84079f0f599e5e82e4f4d81dc167373cb046be, eight array and legacy-input cases and the JSON-field round trip failed.

  • The tests round trip actual URL form representations through MarshalForm, url.Values.Encode, url.ParseQuery, and BindForm. They cover the supported array styles, defaults, optional integer slices, legacy bracketed input, JSON fields, and special characters in decoded values. An adjacent test checks the existing scalar text unmarshaler path.

  • On Go 1.26.0, go test -p 4 -json -count=1 ./... passed 716 tests and subtests across two packages. One package had no test files. go vet -p 4 ./... and gofmt -l completed without findings.

OpenAI Codex prepared the patch, regression tests, and local verification.

Share the form style defaults with the encoder, preserve legacy indexed keys, and bind JSON to the encoded field.

Prepared with OpenAI Codex.
@go165
go165 requested a review from a team as a code owner December 2, 2026 11:45
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.

1 participant