Skip to content
Open
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
4 changes: 4 additions & 0 deletions configuration-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@
"type": "boolean",
"description": "Enable the generation of a `Bytes()` method on response objects for `ClientWithResponses`"
},
"client-response-content-type-function": {
"type": "boolean",
"description": "Enable the generation of a `ContentType()` method on response objects for `ClientWithResponses`"
},
"prefer-skip-optional-pointer": {
"type": "boolean",
"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.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# yaml-language-server: $schema=../../../../configuration-schema.json
package: issue240
package: clientresponsefunctions
output: client.gen.go
generate:
models: true
client: true
output-options:
client-response-bytes-function: true
client-response-content-type-function: true

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

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package issue240
package clientresponsefunctions

//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml api.yaml
3 changes: 3 additions & 0 deletions pkg/codegen/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ type OutputOptions struct {
// ClientResponseBytesFunction decides whether to enable the generation of a `Bytes()` method on response objects for `ClientWithResponses`
ClientResponseBytesFunction bool `yaml:"client-response-bytes-function,omitempty"`

// ClientResponseContentTypeFunction decides whether to enable the generation of a `ContentType()` method on response objects for `ClientWithResponses`
ClientResponseContentTypeFunction bool `yaml:"client-response-content-type-function,omitempty"`

// PreferSkipOptionalPointer 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)
PreferSkipOptionalPointer bool `yaml:"prefer-skip-optional-pointer,omitempty"`
Expand Down
11 changes: 11 additions & 0 deletions pkg/codegen/templates/client-with-responses.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ func (r {{genResponseTypeName $opid | ucFirst}}) Bytes() []byte {
return r.Body
}
{{end}}

{{ if opts.OutputOptions.ClientResponseContentTypeFunction }}
// ContentType is a convenience method to retrieve the Content-Type value from the HTTP response headers
func (r {{genResponseTypeName $opid | ucFirst}}) ContentType() string {
if r.HTTPResponse != nil {
return r.HTTPResponse.Header.Get("Content-Type")
}
return ""
}
{{end}}

{{end}}


Expand Down