Skip to content

MarshalForm ignores the style and explode of a property's encoding #186

Closed

Description

@mromaszewicz

Runtime half of oapi-codegen/oapi-codegen#2604.

MarshalForm takes a map[string]RequestBodyEncoding, but only reads ContentType from it. Style and Explode are ignored, so an array property is always written with indexed keys:

type body struct {
	Topic []string `json:"topic"`
}

explode := true
v, _ := runtime.MarshalForm(body{Topic: []string{"news", "releases"}}, map[string]runtime.RequestBodyEncoding{
	"topic": {Style: "form", Explode: &explode},
})
fmt.Println(v.Encode())
// topic%5B0%5D=news&topic%5B1%5D=releases

Per the OpenAPI Encoding Object, style: form, explode: true serializes as repeated keys: topic=news&topic=releases. A server which reads repeated keys sees no topic at all. BindForm accepts repeated keys, name[] and name[i], so the mismatch only shows up against servers that aren't generated by oapi-codegen.

Expected

When a property's encoding sets Style or Explode, MarshalForm serializes it the way a query parameter of that style would be:

style explode array ["a", "b"]
form true (default) topic=a&topic=b
form false topic=a,b
spaceDelimited false (default) topic=a b
pipeDelimited false (default) topic=a|b
deepObject topic[0]=a&topic[1]=b (today's output)

A property with no encoding, or with an encoding that sets neither field, keeps today's bracketed output, so nothing changes for existing callers: generated code passes nil today.

Related: the ContentType branch always fails

While here: the application/json branch of MarshalForm returns unsupported encoding, only application/json is supported even when the content type is application/json, because the error return isn't in an else. It also marshals the reflect.Value rather than the value it holds. That needs fixing before the generator can pass encodings through, or any spec with a JSON-encoded form property would start failing.

Activity

  1. added a commit that references this issue on Nov 20, 2026
    c67d5d3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions