Repository navigation
fix: bind styled form arrays and JSON fields - #201
mromaszewicz merged 3 commits into
Conversation
Share the form style defaults with the encoder, preserve legacy indexed keys, and bind JSON to the encoded field. Prepared with OpenAI Codex.
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>
|
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 encodingThe 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:
To follow: single values and nullable properties
TestsYour scenarios are all still there, rearranged so that a gap shows up as a missing cell:
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 PRStyled 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. |
Problem
MarshalFormcan encode repeated or delimited arrays, butBindFormconsumes 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 anapplication/jsonfield, 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, andBindForm. 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 ./...andgofmt -lcompleted without findings.OpenAI Codex prepared the patch, regression tests, and local verification.