Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ir/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions ir/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions render/templates/client.gen.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
//
Expand Down
6 changes: 4 additions & 2 deletions testdata/golden/api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Entry"
"$ref": "#/components/schemas/Entry",
"description": "The entry to create, without its id."
}
}
}
Expand All @@ -249,7 +250,8 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Entry"
"$ref": "#/components/schemas/Entry",
"description": "The entry created, with its id."
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion testdata/golden/pkg/golden/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions testdata/golden/pkg/golden/ir.json
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@
"requestBody": {
"typeName": "Entry",
"contentType": "application/json",
"required": true
"required": true,
"description": "The entry to create, without its id."
},
"responses": [
{
Expand All @@ -269,7 +270,8 @@
"goType": {
"name": "Entry"
},
"isSuccess": true
"isSuccess": true,
"bodyDescription": "The entry created, with its id."
}
],
"successReturn": {
Expand Down
Loading