Skip to content

Commit b72509c

Browse files
committed
feat(client): Add ContentType() to ClientWithResponses responses
1 parent 09919e7 commit b72509c

File tree

7 files changed

+39
-4
lines changed

7 files changed

+39
-4
lines changed

configuration-schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@
238238
"type": "boolean",
239239
"description": "Enable the generation of a `Bytes()` method on response objects for `ClientWithResponses`"
240240
},
241+
"client-response-content-type-function": {
242+
"type": "boolean",
243+
"description": "Enable the generation of a `ContentType()` method on response objects for `ClientWithResponses`"
244+
},
241245
"prefer-skip-optional-pointer": {
242246
"type": "boolean",
243247
"description": "Allows defining at a global level whether to omit the pointer for a type to indicate that the field/type is optional. This is the same as adding `x-go-type-skip-optional-pointer` to each field (manually, or using an OpenAPI Overlay). A field can set `x-go-type-skip-optional-pointer: false` to still require the optional pointer.",
File renamed without changes.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# yaml-language-server: $schema=../../../../configuration-schema.json
2-
package: issue240
2+
package: clientresponsefunctions
33
output: client.gen.go
44
generate:
55
models: true
66
client: true
77
output-options:
88
client-response-bytes-function: true
9+
client-response-content-type-function: true

internal/test/issues/issue240/client.gen.go renamed to internal/test/issues/issue-240-2149/client.gen.go

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
package issue240
1+
package clientresponsefunctions
22

33
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml api.yaml

pkg/codegen/configuration.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ type OutputOptions struct {
288288
// ClientResponseBytesFunction decides whether to enable the generation of a `Bytes()` method on response objects for `ClientWithResponses`
289289
ClientResponseBytesFunction bool `yaml:"client-response-bytes-function,omitempty"`
290290

291+
// ClientResponseContentTypeFunction decides whether to enable the generation of a `ContentType()` method on response objects for `ClientWithResponses`
292+
ClientResponseContentTypeFunction bool `yaml:"client-response-content-type-function,omitempty"`
293+
291294
// PreferSkipOptionalPointer allows defining at a global level whether to omit the pointer for a type to indicate that the field/type is optional.
292295
// This is the same as adding `x-go-type-skip-optional-pointer` to each field (manually, or using an OpenAPI Overlay)
293296
PreferSkipOptionalPointer bool `yaml:"prefer-skip-optional-pointer,omitempty"`

pkg/codegen/templates/client-with-responses.tmpl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ func (r {{genResponseTypeName $opid | ucFirst}}) Bytes() []byte {
8181
return r.Body
8282
}
8383
{{end}}
84+
85+
{{ if opts.OutputOptions.ClientResponseContentTypeFunction }}
86+
// ContentType is a convenience method to retrieve the Content-Type value from the HTTP response headers
87+
func (r {{genResponseTypeName $opid | ucFirst}}) ContentType() string {
88+
if r.HTTPResponse != nil {
89+
return r.HTTPResponse.Header.Get("Content-Type")
90+
}
91+
return ""
92+
}
93+
{{end}}
94+
8495
{{end}}
8596

8697

0 commit comments

Comments
 (0)