Skip to content

strict-server: any-typed response bodies generate a method on an interface type, which does not compile #2525

Description

@mromaszewicz

Found during the review of #2522; pre-existing on main and independent of that PR.

Problem

Under strict-server, a JSON response body whose schema lowers to any generates a defined type with an interface underlying type, plus a value-receiver method on it:

type GetThing200JSONResponse any

func (response GetThing200JSONResponse) VisitGetThingResponse(w http.ResponseWriter) error {

Go forbids methods on types whose underlying type is a pointer or interface, so the output does not compile:

invalid receiver type GetThing200JSONResponse (pointer or interface type)

Reproduction

Verified on main with:

paths:
  /things:
    get:
      operationId: getThing
      responses:
        "200":
          description: ok
          content:
            application/json:
              schema: {}

and config generate: { models: true, std-http-server: true, strict-server: true }. The same happens for type: "null" (#2430) and, once #2522 lands, for multi-type unions (type: [string, number]) — every schema shape that lowers to any.

Notes

  • Non-strict servers are unaffected; only the strict envelope needs a method on the response type.
  • Since this output has never compiled, there are no existing users of the current shape — the fix is free to choose the right one. The obvious candidate is wrapping the body in a struct for interface-underlying response types (e.g. type GetThing200JSONResponse struct { Body any }, with the visitor marshaling response.Body), mirroring how strict response envelopes with headers already wrap. Alternatively the generator could error on this combination with a pointer at the workaround (x-go-type to a concrete type), but generating something that works seems strictly better.

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