Skip to content

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

Merged
mromaszewicz merged 3 commits into
oapi-codegen:mainfrom
go165:codex/bind-urlencoded-array-and-json-fields
Dec 5, 2026
Merged

mromaszewicz merged 3 commits into
oapi-codegen:mainfrom
go165:codex/bind-urlencoded-array-and-json-fields

Conversation

@go165

@go165 go165 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

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
@mromaszewicz mromaszewicz added the bug Something isn't working label Dec 5, 2026
Three cases where BindForm read less of an array under an encoding than it
does without one:

- An entry which sets neither style nor explode rejected an array sent as
  repeated keys with "unhandled parameter style". It now leaves the default
  binding in place, as MarshalForm does.
- A delimited (not exploded) array bound only the first value when a client
  repeated the key anyway. Every value is now split and bound.
- An empty delimited array, which MarshalForm writes as an empty value,
  failed to parse for elements which are not strings. It now holds no
  elements.

TestBindFormEncodedArrays becomes a matrix of every encoding an array can
declare against every way a client may write the array, and
TestMarshalFormEncoding reads each of its serializations back with BindForm.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Two cases where a form property with an encoding was not read:

- A property which is a single value, such as a date, was silently dropped
  under the deepObject style: every deepObject property went to the bracketed
  binder, which reads a struct as an object. Only arrays and objects go
  there now, and a single value is read as it is under any other style.
- A nullable property failed under every style, and under an entry which
  sets none, because the styled binder took its wrapper, a map, for an
  object. The property is now classified by what the wrapper holds, so a
  nullable array is read as an array and a nullable value as a value.

It also turns the panic for an `any` property under deepObject back into the
error it is under the other styles.

The array matrix gains a nullable array, and TestBindFormEncodedScalars is
the same set of encodings against strings, integers, dates and UUIDs,
whether required, optional or nullable.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mromaszewicz

Copy link
Copy Markdown
Member

I went over this closely, since form binding has a history of breaking in corners, and I'm adding commits on top of yours rather than sending it back for another round. The approach is right. What follows is what I changed and why.

Pushed: 0784634, arrays read in full under every encoding

The goal in oapi-codegen/oapi-codegen#2676 is that a server which passes encodings accepts strictly more than one which doesn't, apart from the delimiter split. Three array cases fell short of that:

  • An entry which sets neither style nor explode rejected topic=a&topic=b with unhandled parameter style:. The generator produces such entries for an encoding which only has headers or allowReserved. It now leaves the default binding in place, as MarshalForm does.
  • Repeated keys under a delimited style bound only the first value, so topic=a&topic=b became [a] where a server without encodings reads both. Every value is now split and bound.
  • An empty delimited array, which MarshalForm writes as counts=, failed with a ParseInt error for elements which aren't strings. An empty value now holds no elements. The cost is that a delimited []string{""}, which is the same bytes on the wire, also reads back as empty.

To follow: single values and nullable properties

  • deepObject on a single value regressed from main: day=2026-12-02 into a types.Date was silently dropped, because every deepObject property went to the bracketed binder, which reads a struct as an object. Only arrays and objects go there now.
  • Nullable properties failed under every style, and under an empty entry, because the styled binder took the nullable.Nullable wrapper (a map) for an object. That also failed on main, but it has to work before the server templates can pass encodings. The property is now classified by what the wrapper holds.

Tests

Your scenarios are all still there, rearranged so that a gap shows up as a missing cell:

  • TestBindFormEncodedArrays is a matrix. The rows are every encoding an array can declare: each style with explode unset, on and off, the empty entry, deepObject and JSON. The columns are every way a client may write the array: as declared, indexed keys, [] keys and repeated keys, plus cells for delimiters inside elements and for an empty array. Your seven cases are rows, and your legacy-keys case is a column which now runs against every row. A required, an optional and a nullable array go through each cell.
  • TestBindFormEncodedScalars runs the same rows against a string, an optional integer, a date, an optional date, a UUID, a nullable string and a nullable date.
  • TestMarshalFormEncoding now reads each of its serializations back with BindForm. The two rows where a delimited element holds its own delimiter pin what comes back, and the two styled-object rows are marked as not read back yet.
  • Your JSON-field and Required date tests are unchanged.

Of the 194 cells in the two matrices, 70 fail against 0784634, and 13 of the 89 array cells in the first version failed against the original commit.

Not in this PR

Styled objects stay with #2676, as you said. The review also turned up problems which predate this change, two panics among them. Those are in #202.

@mromaszewicz
mromaszewicz merged commit 476f879 into oapi-codegen:main Dec 5, 2026
10 checks passed
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.

2 participants