Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #2294
Closed
Open
Issue created Mar 04, 2019 by Administrator@rootContributor

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

Created by: 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}}
Assignee
Assign to
Time tracking