diff --git a/modules/openapi-generator/src/main/resources/go/api.mustache b/modules/openapi-generator/src/main/resources/go/api.mustache index 3e9ff07a7b2..fada916ff9c 100644 --- a/modules/openapi-generator/src/main/resources/go/api.mustache +++ b/modules/openapi-generator/src/main/resources/go/api.mustache @@ -352,6 +352,15 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, err } +{{#withCustomMiddlewareFunction}} + if a.client.cfg.ResponseMiddleware != nil { + err = a.client.cfg.ResponseMiddleware(localVarHTTPResponse, localVarBody) + if err != nil { + return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, err + } + } + +{{/withCustomMiddlewareFunction}} if localVarHTTPResponse.StatusCode >= 300 { newErr := &GenericOpenAPIError{ body: localVarBody, diff --git a/modules/openapi-generator/src/main/resources/go/client.mustache b/modules/openapi-generator/src/main/resources/go/client.mustache index 4c7c1f47623..3f6dcb7bf59 100644 --- a/modules/openapi-generator/src/main/resources/go/client.mustache +++ b/modules/openapi-generator/src/main/resources/go/client.mustache @@ -487,6 +487,13 @@ func (c *APIClient) prepareRequest( c.cfg.Middleware(localVarRequest) } + if c.cfg.MiddlewareWithError != nil { + err = c.cfg.MiddlewareWithError(localVarRequest) + if err != nil { + return nil, err + } + } + {{/withCustomMiddlewareFunction}} {{#hasHttpSignatureMethods}} if ctx != nil { diff --git a/modules/openapi-generator/src/main/resources/go/configuration.mustache b/modules/openapi-generator/src/main/resources/go/configuration.mustache index 419035d03d7..a4866e327b4 100644 --- a/modules/openapi-generator/src/main/resources/go/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/go/configuration.mustache @@ -104,9 +104,15 @@ type ServerConfiguration struct { type ServerConfigurations []ServerConfiguration {{#withCustomMiddlewareFunction}} -// MiddlewareFunction provides way to implement custom middleware +// MiddlewareFunction provides way to implement custom middleware in the prepareRequest type MiddlewareFunction func(*http.Request) +// MiddlewareFunctionWithError provides way to implement custom middleware with errors in the prepareRequest +type MiddlewareFunctionWithError func(*http.Request) error + +// ResponseMiddlewareFunction provides way to implement custom middleware with errors after the response is received +type ResponseMiddlewareFunction func(*http.Response, []byte) error + {{/withCustomMiddlewareFunction}} // Configuration stores the configuration of the API client type Configuration struct { @@ -119,7 +125,9 @@ type Configuration struct { OperationServers map[string]ServerConfigurations HTTPClient *http.Client {{#withCustomMiddlewareFunction}} - Middleware MiddlewareFunction + Middleware MiddlewareFunction + MiddlewareWithError MiddlewareFunctionWithError + ResponseMiddleware ResponseMiddlewareFunction {{/withCustomMiddlewareFunction}} }