Skip to content

Honor every form body encoding (style, explode, contentType) in clients and strict servers #2676

Open

Description

@mromaszewicz

Follow-up to #2604.

For application/x-www-form-urlencoded request bodies, the fix for #2604 makes the generated client honor exactly one declared encoding: an array of primitives in the exploded form style, which is sent as repeated keys (topic=a&topic=b). Every other declared encoding is still ignored, and the property is sent with indexed or bracketed keys (topic[0]=a, filter[role]=admin) as before.

That limit is deliberate. Generated strict servers bind form bodies with runtime.BindForm(&body, form, nil, nil), without encodings, and repeated keys are the only styled serialization that path reads back correctly. Had the client honored the rest, a client and a server generated from the same spec would have stopped understanding each other:

Property Declared encoding Spec-correct wire format Strict server binds today
array form, explode true topic=news&topic=rel correct
array form, explode false topic=news,rel one element, "news,rel"
array spaceDelimited topic=news rel one element
array pipeDelimited topic=news|rel one element
object form, explode true role=admin&age=3 nothing
object form, explode false filter=age,3,role,admin nothing
any deepObject bracketed, as today correct
any contentType: application/json filter={"role":"admin"} nothing

So both sides have to change together. What needs doing:

1. runtime: make BindForm honor encodings

MarshalForm already serializes all of the above as of runtime v1.9.1 (oapi-codegen/runtime#187). BindForm's encoding path does not work well enough to turn on:

  • It reads only the first value of a key, so an exploded array binds one element.
  • spaceDelimited and pipeDelimited fail with unhandled parameter style.
  • Objects fail with can not bind to destination of type: struct, in either explode mode; exploded objects need their properties read from top-level keys.
  • An unset Explode is treated as false, where the specification's default for form is true (and MarshalForm follows the specification).
  • The ContentType branch unmarshals into the whole body rather than the field, and then returns unsupported encoding regardless.
  • A property with an encoding is read only from its plain key, so the bracketed forms (topic[0]=a, filter[role]=admin) are silently dropped. They need to stay accepted as a fallback, or every existing client, including ones generated by older releases, breaks against a new server.

2. Server templates: pass the encodings to BindForm

The strict templates (strict-echo, strict-http, strict-gin, strict-iris, and the Fiber ones if they bind forms) pass nil. They need the operation's encodings, which RequestBodyDefinition.Encoding already carries.

3. Client templates: pass every encoding to MarshalForm

Replace the narrow ExplodedFormArrays list in client.tmpl and initiator.tmpl with the full declared encodings.

Behavior changes to weigh

With the bracketed fallback kept, a strict server accepts strictly more than today, except for one narrowing:

  • Arrays declared with explode false get split on their delimiter. topic=a,b binds as ["a", "b"] where today it is the single element "a,b", so a client which sends one element containing the delimiter sees it split.
  • Exploded form objects are read from top-level keys, which can collide with a sibling property of the same name. The specification has that ambiguity built in.

On the client side, the wire format changes for every spec which declares one of these encodings, and clients talking to a server generated by an older release would no longer be understood for anything but exploded arrays and deepObject. That probably wants an opt-out under compatibility, or at least a prominent release note.

Also worth settling while here:

  • Arrays of objects, and nested objects, have no defined styled serialization outside deepObject. MarshalForm returns an error for them; the generator should probably keep them bracketed rather than emit a request that fails at run time.
  • contentType and style declared together: OpenAPI 3.0 and 3.1 disagree on which wins.
  • The default encoding. A property with no encoding at all is form + explode true per the specification, so arrays would be repeated keys by default. Today they are indexed. Changing that is a separate, larger compatibility question.

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