From 7e45fadf1cf409e06fa269e9bedef7ab6a848199 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 21:05:47 +0000 Subject: [PATCH] feat: a client method tells what is said beside its body's and answer's $ref A description beside a reference to a body's schema says what that operation's body or answer is, which the schema's own, shared by every operation that uses it, cannot: compress puts one there where the schemas it merges disagree. The client method's doc comment now says it, as "Body:" and "Returns:". Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y --- ir/ir.go | 18 ++++++++++++++++++ ir/operation.go | 13 +++++++++++++ render/templates/client.gen.go.tmpl | 8 ++++++++ testdata/golden/api/openapi.json | 6 ++++-- testdata/golden/pkg/golden/client.gen.go | 6 +++++- testdata/golden/pkg/golden/ir.json | 6 ++++-- 6 files changed, 52 insertions(+), 5 deletions(-) diff --git a/ir/ir.go b/ir/ir.go index e65d7772..2cb222fc 100644 --- a/ir/ir.go +++ b/ir/ir.go @@ -641,6 +641,21 @@ type Response struct { // IsStream is true for a success response whose media type is not text, such as a zip, a PDF or an image: GoType // is io.ReadCloser, the response body itself, which the caller reads and closes. IsStream bool `json:"isStream,omitzero"` + // BodyDescription is what the specification says beside the reference to the body's schema, of this response + // alone, as a merged schema's description that differed from another's. + BodyDescription string `json:"bodyDescription,omitzero"` +} + +// SuccessDescription is what the specification says beside the reference to the schema of the body the operation +// returns, if anything. +func (op Operation) SuccessDescription() string { + for _, r := range op.Responses { + if r.IsSuccess && r.BodyDescription != "" { + return r.BodyDescription + } + } + + return "" } // IsJSON reports whether the response's media type is a JSON one, whose body the client decodes. @@ -653,6 +668,9 @@ type ReqBody struct { TypeName string `json:"typeName,omitzero"` ContentType string `json:"contentType,omitzero"` Required bool `json:"required,omitzero"` + // Description is what the specification says beside the reference to the body's schema, of this operation's + // body alone, which the type's own description, shared by every operation, cannot say. + Description string `json:"description,omitzero"` } type Auth struct { diff --git a/ir/operation.go b/ir/operation.go index b757f669..dd1f8d56 100644 --- a/ir/operation.go +++ b/ir/operation.go @@ -688,6 +688,7 @@ func fromRequestBody(rb *openapi.RequestBody) (*ReqBody, error) { TypeName: tp.String(), ContentType: string(mr), Required: rb.Required, + Description: refDescription(mt.Schema), }, nil } @@ -773,6 +774,8 @@ func fromResponses(responses openapi.OperationResponses) (Responses, *GoType, bo IsSuccess: isSuccess, IsRawBytes: isRawBytes, IsStream: isStream, + + BodyDescription: refDescription(jsonSchema), }) // an empty object is still decoded, so anything in it is an error, but there is nothing to return @@ -872,3 +875,13 @@ func isEmptyObject(s *openapi.Schema) bool { return s != nil && s.Type == openapi.TypeObject && len(s.Properties) == 0 && mapValues(s) == nil && len(s.AllOf) == 0 && len(s.OneOf) == 0 && len(s.AnyOf) == 0 } + +// refDescription is what the specification says beside s, a reference to a schema, of where it is used: "" for a +// schema of its own, whose description its type says. +func refDescription(s *openapi.Schema) string { + if s == nil || s.Ref == nil { + return "" + } + + return s.Description +} diff --git a/render/templates/client.gen.go.tmpl b/render/templates/client.gen.go.tmpl index e601c8bf..3dbeb597 100644 --- a/render/templates/client.gen.go.tmpl +++ b/render/templates/client.gen.go.tmpl @@ -221,6 +221,14 @@ func NewClient(opts ...ClientOption) (*Client, error) { // {{- end }} {{- end }} +{{- with .RequestBody }}{{ with .Description }} +{{ toGoComment (printf "Body: %s" .) }} +// +{{- end }}{{ end }} +{{- with .SuccessDescription }} +{{ toGoComment (printf "Returns: %s" .) }} +// +{{- end }} // {{ .Method }} {{ .PathTemplate }} {{- if .Deprecated}} // diff --git a/testdata/golden/api/openapi.json b/testdata/golden/api/openapi.json index 10e1b5a1..c51fb238 100644 --- a/testdata/golden/api/openapi.json +++ b/testdata/golden/api/openapi.json @@ -238,7 +238,8 @@ "content": { "application/json": { "schema": { - "$ref": "#/components/schemas/Entry" + "$ref": "#/components/schemas/Entry", + "description": "The entry to create, without its id." } } } @@ -249,7 +250,8 @@ "content": { "application/json": { "schema": { - "$ref": "#/components/schemas/Entry" + "$ref": "#/components/schemas/Entry", + "description": "The entry created, with its id." } } } diff --git a/testdata/golden/pkg/golden/client.gen.go b/testdata/golden/pkg/golden/client.gen.go index 0f45ceca..ec6576f4 100644 --- a/testdata/golden/pkg/golden/client.gen.go +++ b/testdata/golden/pkg/golden/client.gen.go @@ -349,7 +349,11 @@ func (c *Client) PatchUser(ctx context.Context, id string, body *User) error { } } -// POST /entries +// Body: The entry to create, without its id. +// +// Returns: The entry created, with its id. +// +// POST /entries func (c *Client) CreateEntry(ctx context.Context, body Entry) (*Entry, error) { u := c.baseURL.JoinPath("entries") pr, pw := io.Pipe() diff --git a/testdata/golden/pkg/golden/ir.json b/testdata/golden/pkg/golden/ir.json index c35024a4..43a1201f 100644 --- a/testdata/golden/pkg/golden/ir.json +++ b/testdata/golden/pkg/golden/ir.json @@ -258,7 +258,8 @@ "requestBody": { "typeName": "Entry", "contentType": "application/json", - "required": true + "required": true, + "description": "The entry to create, without its id." }, "responses": [ { @@ -269,7 +270,8 @@ "goType": { "name": "Entry" }, - "isSuccess": true + "isSuccess": true, + "bodyDescription": "The entry created, with its id." } ], "successReturn": {