Skip to content

Commit

Permalink
Add client-type-prefix output option (deepmap#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedrivisser committed Oct 14, 2022
1 parent 63fe9cc commit 3262572
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 12 deletions.
23 changes: 23 additions & 0 deletions examples/prefixed-client/api.yaml
@@ -0,0 +1,23 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Prefixed Client Example
paths:
/client:
get:
operationId: getClient
responses:
200:
content:
application/json:
schema:
$ref: "#/components/schemas/Client"
components:
schemas:
Client:
type: object
required:
- name
properties:
name:
type: string
7 changes: 7 additions & 0 deletions examples/prefixed-client/cfg.yaml
@@ -0,0 +1,7 @@
package: prefixedclient
output: prefixed-client.gen.go
generate:
models: true
client: true
output-options:
client-type-prefix: Prefixed
6 changes: 6 additions & 0 deletions examples/prefixed-client/doc.go
@@ -0,0 +1,6 @@
package prefixedclient

// This is an example of how to add a prefix to the name of the generated Client struct
// See https://github.com/deepmap/oapi-codegen/issues/785 for why this might be necessary

//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen -config cfg.yaml api.yaml
239 changes: 239 additions & 0 deletions examples/prefixed-client/prefixed-client.gen.go

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

1 change: 1 addition & 0 deletions pkg/codegen/configuration.go
Expand Up @@ -76,6 +76,7 @@ type OutputOptions struct {

ExcludeSchemas []string `yaml:"exclude-schemas,omitempty"` // Exclude from generation schemas with given names. Ignored when empty.
ResponseTypeSuffix string `yaml:"response-type-suffix,omitempty"` // The suffix used for responses types
ClientTypePrefix string `yaml:"client-type-prefix,omitempty"` // The prefix used for the client type
}

// UpdateDefaults sets reasonable default values for unset fields in Configuration
Expand Down
4 changes: 3 additions & 1 deletion pkg/codegen/templates/client-with-responses.tmpl
Expand Up @@ -13,9 +13,11 @@ func NewClientWithResponses(server string, opts ...ClientOption) (*ClientWithRes
return &ClientWithResponses{client}, nil
}

{{$clientPrefix := opts.OutputOptions.ClientTypePrefix -}}

// WithBaseURL overrides the baseURL.
func WithBaseURL(baseURL string) ClientOption {
return func(c *Client) error {
return func(c *{{ $clientPrefix }}Client) error {
newBaseURL, err := url.Parse(baseURL)
if err != nil {
return err
Expand Down
24 changes: 13 additions & 11 deletions pkg/codegen/templates/client.tmpl
Expand Up @@ -8,8 +8,10 @@ type HttpRequestDoer interface {
Do(req *http.Request) (*http.Response, error)
}

// Client which conforms to the OpenAPI3 specification for this service.
type Client struct {
{{$clientPrefix := opts.OutputOptions.ClientTypePrefix -}}

// {{ $clientPrefix }}Client which conforms to the OpenAPI3 specification for this service.
type {{ $clientPrefix }}Client struct {
// The endpoint of the server conforming to this interface, with scheme,
// https://api.deepmap.com for example. This can contain a path relative
// to the server, such as https://api.deepmap.com/dev-test, and all the
Expand All @@ -26,12 +28,12 @@ type Client struct {
}

// ClientOption allows setting custom parameters during construction
type ClientOption func(*Client) error
type ClientOption func(*{{ $clientPrefix }}Client) error

// Creates a new Client, with reasonable defaults
func NewClient(server string, opts ...ClientOption) (*Client, error) {
// Creates a new {{ $clientPrefix }}Client, with reasonable defaults
func NewClient(server string, opts ...ClientOption) (*{{ $clientPrefix }}Client, error) {
// create a client with sane default values
client := Client{
client := {{ $clientPrefix }}Client{
Server: server,
}
// mutate client and add all optional params
Expand All @@ -54,7 +56,7 @@ func NewClient(server string, opts ...ClientOption) (*Client, error) {
// WithHTTPClient allows overriding the default Doer, which is
// automatically created using http.Client. This is useful for tests.
func WithHTTPClient(doer HttpRequestDoer) ClientOption {
return func(c *Client) error {
return func(c *{{ $clientPrefix }}Client) error {
c.Client = doer
return nil
}
Expand All @@ -63,7 +65,7 @@ func WithHTTPClient(doer HttpRequestDoer) ClientOption {
// WithRequestEditorFn allows setting up a callback function, which will be
// called right before sending the request. This can be used to mutate the request.
func WithRequestEditorFn(fn RequestEditorFn) ClientOption {
return func(c *Client) error {
return func(c *{{ $clientPrefix }}Client) error {
c.RequestEditors = append(c.RequestEditors, fn)
return nil
}
Expand Down Expand Up @@ -92,7 +94,7 @@ type ClientInterface interface {
{{$pathParams := .PathParams -}}
{{$opid := .OperationId -}}

func (c *Client) {{$opid}}{{if .HasBody}}WithBody{{end}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}{{if .HasBody}}, contentType string, body io.Reader{{end}}, reqEditors... RequestEditorFn) (*http.Response, error) {
func (c *{{ $clientPrefix }}Client) {{$opid}}{{if .HasBody}}WithBody{{end}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}{{if .HasBody}}, contentType string, body io.Reader{{end}}, reqEditors... RequestEditorFn) (*http.Response, error) {
req, err := New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(c.Server{{genParamNames .PathParams}}{{if $hasParams}}, params{{end}}{{if .HasBody}}, contentType, body{{end}})
if err != nil {
return nil, err
Expand All @@ -106,7 +108,7 @@ func (c *Client) {{$opid}}{{if .HasBody}}WithBody{{end}}(ctx context.Context{{ge

{{range .Bodies}}
{{if .IsSupportedByClient -}}
func (c *Client) {{$opid}}{{.Suffix}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody, reqEditors... RequestEditorFn) (*http.Response, error) {
func (c *{{ $clientPrefix }}Client) {{$opid}}{{.Suffix}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody, reqEditors... RequestEditorFn) (*http.Response, error) {
req, err := New{{$opid}}Request{{.Suffix}}(c.Server{{genParamNames $pathParams}}{{if $hasParams}}, params{{end}}, body)
if err != nil {
return nil, err
Expand Down Expand Up @@ -285,7 +287,7 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr

{{end}}{{/* Range */}}

func (c *Client) applyEditors(ctx context.Context, req *http.Request, additionalEditors []RequestEditorFn) error {
func (c *{{ $clientPrefix }}Client) applyEditors(ctx context.Context, req *http.Request, additionalEditors []RequestEditorFn) error {
for _, r := range c.RequestEditors {
if err := r(ctx, req); err != nil {
return err
Expand Down

0 comments on commit 3262572

Please sign in to comment.