Repository navigation
Description
Found while reviewing #201. None of these come from that PR: each reproduces on main (6cfc95d) with no encodings, unless noted. They are grouped here because three of them share a cause.
1. A form key with an unclosed bracket panics BindForm
var body struct {
Topic []string `json:"topic"`
}
err := runtime.BindForm(&body, url.Values{"topic[": {"a"}}, nil, nil)
// panic: runtime error: slice bounds out of range [:-1]indexedElementsCount cuts the key at strings.Index(str, "]") without checking for -1, and bindAdditionalProperties does the same, so a struct with AdditionalProperties panics on obj[ as well. bindFormMap already checks.
Any array property, or object with additional properties, is affected. The generated strict servers for Echo, Gin, Iris and net/http hand the request's form to BindForm as it arrived, so a client can trigger this. Those all run on net/http, which recovers a handler's panic (as recovery middleware does, where it is installed), so the result is an aborted or 500 response and a stack trace in the log where a 400 was due.
Expected: the key is ignored, or BindForm returns an error.
2. Date and time properties are silently dropped, in both directions
type body struct {
Date types.Date `json:"date"`
When time.Time `json:"when"`
Opt *types.Date `json:"opt,omitempty"`
Dates []types.Date `json:"dates,omitempty"`
}
values, _ := runtime.MarshalForm(body{Date: d, When: d.Time, Opt: &d, Dates: []types.Date{d}}, nil)
// values.Encode() == ""
var got body
_ = runtime.BindForm(&got, url.Values{
"date": {"2026-12-02"}, "when": {"2026-12-02T00:00:00Z"}, "opt": {"2026-12-02"}, "dates": {"2026-12-02"},
}, nil, nil)
// no error; got.Date and got.When are zero, got.Opt is nil. Only got.Dates is bound.marshalFormImpl and bindFormImpl treat every struct as an object and walk its exported fields, looking for date[...] keys. types.Date and time.Time have none worth walking, so nothing is written and nothing is read. Reading the code, the same goes for any struct type which serializes itself as text; only these two were tested.
So a format: date or format: date-time property of a form body is not sent by a generated client and not received by a generated strict server, without an error on either side. The parameter binders already treat these types as single values.
3. An any property with a value panics BindForm
var body struct {
Any interface{} `json:"any"`
}
err := runtime.BindForm(&body, url.Values{"any": {"x"}}, nil, nil)
// panic: reflect.Value.Addr of unaddressable valueThe interface case of bindFormImpl recurses into v.Elem(), which is the invalid reflect.Value for a nil interface. It only panics when the key is present.
Expected: an error, as BindStringToObject gives for an interface destination which has no declared type list (#153).
4. MarshalForm with a style on a []byte property panics, or writes a UUID
This one needs an encoding, and dates from #187 (v1.9.1).
enc := map[string]runtime.RequestBodyEncoding{"data": {Style: "form"}}
runtime.MarshalForm(struct{ Data []byte `json:"data"` }{[]byte("hi")}, enc)
// panic: reflect: cannot convert slice with length 2 to array with length 16
runtime.MarshalForm(struct{ Data []byte `json:"data"` }{[]byte("0123456789abcdef")}, enc)
// data=30313233-3435-3637-3839-616263646566marshalFormStyled asks marshalKnownTypes first, which tests t.ConvertibleTo(reflect.TypeOf(types.UUID{})). That holds for every byte slice as far as the type goes; the conversion itself panics unless the length is 16, and at 16 the bytes are formatted as a UUID. I did not check whether the other callers of marshalKnownTypes can be handed a byte slice, so it is worth guarding there rather than in the form code.
The generated client does not pass an encoding for such a property today, so this needs a direct caller, until the rest of oapi-codegen/oapi-codegen#2676 passes every declared encoding through.
5. MarshalForm writes a nullable.Nullable as a Go map
type body struct {
Name nullable.Nullable[string] `json:"name"`
}
v := body{Name: nullable.NewNullableWithValue("x")}
runtime.MarshalForm(v, nil)
// name=map[true:x]
runtime.MarshalForm(v, map[string]runtime.RequestBodyEncoding{"name": {Style: "form"}})
// <bool Value>=xBindForm has read a Nullable since #133, and #56 stopped the nil case from panicking, but the marshal side was never taught that a bool-keyed map is a wrapper. Without an encoding the map falls to fmt.Sprint; with a style it is treated as an object whose key is the bool.
Common cause
2, 3 and 5 are the same gap seen from different sides: the form code decides what a value is from its reflect.Kind alone, so it has no notion of a struct, interface or map which stands for a single form value. Teaching bindFormImpl and marshalFormImpl that, the way the parameter binders already check for Binder, encoding.TextUnmarshaler and the time types before they treat a struct as an object, would fix those three together. It would also let #201 route scalars through bindFormImpl, where it currently has to keep a second path for them.
1 and 4 are independent, and small.
Activity