Skip to content

[REQ][Golang][client] interface definitions for api functions #2294

@partkyle

Description

@partkyle

Is your feature request related to a problem? Please describe.

When using an external api in testing it is common to use a golang interface to introduce a mock or fake implementation.

Describe the solution you'd like

Automatically generating these interfaces along side the api definitions would make a consistent place for referencing these interfaces to facilitate unit testing for applications that would use this client.

Describe alternatives you've considered

I could create my own package that has this implementation, but the openapi spec already includes all the information I need. Furthermore, the generated client code includes the request input and output types, so it makes the most sense for the interface to be included in the same package.

Additional context

A quick-and-dirty stab at implementation would look something like this, though I would be up to discuss alternatives:

diff --git a/modules/openapi-generator/src/main/resources/go/api.mustache b/modules/openapi-generator/src/main/resources/go/api.mustache
index fe72ace87e..c8a374575e 100644
--- a/modules/openapi-generator/src/main/resources/go/api.mustache
+++ b/modules/openapi-generator/src/main/resources/go/api.mustache
@@ -17,6 +17,35 @@ var (
        _ context.Context
 )

+type {{classname}} interface {
+{{#operation}}
+    /*
+    {{{classname}}}Service{{#summary}} {{{.}}}{{/summary}}
+    {{#notes}}
+    {{notes}}
+    {{/notes}}
+    * @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
+    {{#allParams}}
+    {{#required}}
+    * @param {{paramName}}{{#description}} {{{.}}}{{/description}}
+    {{/required}}
+    {{/allParams}}
+    {{#hasOptionalParams}}
+    * @param optional nil or *{{{nickname}}}Opts - Optional Parameters:
+    {{#allParams}}
+    {{^required}}
+    * @param "{{vendorExtensions.x-exportParamName}}" ({{#isPrimitiveType}}{{^isBinary}}optional.{{vendorExtensions.x-optionalDataType}}{{/isBinary}}{{#isBinary}}optional.Interface of {{dataType}}{{/isBinary}}{{/isPrimitiveType}}{{^isPrimitiveType}}optional.Interface of {{dataType}}{{/isPrimitiveType}}) - {{#description}} {{{.}}}{{/description}}
+    {{/required}}
+    {{/allParams}}
+    {{/hasOptionalParams}}
+    {{#returnType}}
+    @return {{{returnType}}}
+    {{/returnType}}
+    */
+    {{{nickname}}}(ctx context.Context{{#hasParams}}, {{/hasParams}}{{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals *{{{nickname}}}Opts{{/hasOptionalParams}}) ({{#returnType}}{{{returnType}}}, {{/returnType}}*http.Response, error)
+{{/operation}}
+}
+
 type {{classname}}Service service
 {{#operation}}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions