Skip to content

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

Closed

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.

Activity

  1. bendrucker commented on Aug 19, 2026

    @bendrucker
    Contributor

    Cross-linking: this is the same bug as #1328, and #2514 is an open fix for it. It wraps receiver-incompatible JSON response bodies in a struct with a Body field, the shape suggested here, across all the strict templates.

    I tested that branch merged onto current main. The merge is clean, make generate produces no drift, and the full suite passes. Every shape from this issue generates the wrapper and compiles: schema: {}, type: "null", a 3.1 multi-type union, and a $ref to a named union component (it resolves the ref and classifies by the underlying schema). Its test specs are all 3.0, so the 3.1 cases aren't pinned by fixtures, but the classification handles them.

    Given that, I'll hold off on a separate PR for this.

  2. mromaszewicz commented on Nov 7, 2026

    @mromaszewicz
    MemberAuthor

    This is fixed on main by #2514 and will ship in the next release.

    When a strict-server JSON response body's schema resolves to any, the response is now wrapped in a struct instead of being declared as a type over an interface:

    type GetThing200JSONResponse struct {
    	Body any
    }

    The visitor encodes response.Body. This is the shape suggested in the issue.

    I re-checked both cases from the report, schema: {} and type: "null", with models, std-http-server and strict-server enabled. Both generate the wrapped form, and the output compiles and passes go vet.

    Closing as fixed.


    This message was drafted with the help of Claude.

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