Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GO][Client] return GenericOpenAPIError instead of error in Execute() #8137

Merged
merged 14 commits into from
Dec 17, 2020
Merged
47 changes: 31 additions & 16 deletions modules/openapi-generator/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package {{packageName}}

{{#operations}}
import (
"bytes"
_context "context"
_ioutil "io/ioutil"
_nethttp "net/http"
Expand Down Expand Up @@ -35,7 +36,7 @@ type {{classname}} interface {
* {{nickname}}Execute executes the request{{#returnType}}
* @return {{{.}}}{{/returnType}}
*/
{{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) ({{#returnType}}{{{.}}}, {{/returnType}}*_nethttp.Response, error)
{{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) ({{#returnType}}{{{.}}}, {{/returnType}}*_nethttp.Response, GenericOpenAPIError)
{{/operation}}
}
{{/generateInterfaces}}
Expand All @@ -60,7 +61,7 @@ func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Reques
return r
}{{/isPathParam}}{{/allParams}}

func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) Execute() ({{#returnType}}{{{.}}}, {{/returnType}}*_nethttp.Response, error) {
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) Execute() ({{#returnType}}{{{.}}}, {{/returnType}}*_nethttp.Response, GenericOpenAPIError) {
return r.ApiService.{{nickname}}Execute(r)
}

Expand All @@ -85,21 +86,23 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#pathParam
* Execute executes the request{{#returnType}}
* @return {{{.}}}{{/returnType}}
*/
func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) ({{#returnType}}{{{.}}}, {{/returnType}}*_nethttp.Response, error) {
func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) ({{#returnType}}{{{.}}}, {{/returnType}}*_nethttp.Response, GenericOpenAPIError) {
var (
localVarHTTPMethod = _nethttp.Method{{httpMethod}}
localVarPostBody interface{}
localVarFormFileName string
localVarFileName string
localVarFileBytes []byte
executionError GenericOpenAPIError
{{#returnType}}
localVarReturnValue {{{.}}}
{{/returnType}}
)

localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "{{{classname}}}Service.{{{nickname}}}")
if err != nil {
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, GenericOpenAPIError{error: err.Error()}
executionError.error = err.Error()
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, executionError
}

localVarPath := localBasePath + "{{{path}}}"{{#pathParams}}
Expand All @@ -112,27 +115,32 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
{{#required}}
{{^isPathParam}}
if r.{{paramName}} == nil {
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} is required and must be specified")
executionError.error = "{{paramName}} is required and must be specified"
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, executionError
}
{{/isPathParam}}
{{#minItems}}
if len({{^isPathParam}}*{{/isPathParam}}r.{{paramName}}) < {{minItems}} {
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must have at least {{minItems}} elements")
executionError.error = "{{paramName}} must have at least {{minItems}} elements"
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, executionError
}
{{/minItems}}
{{#maxItems}}
if len({{^isPathParam}}*{{/isPathParam}}r.{{paramName}}) > {{maxItems}} {
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must have less than {{maxItems}} elements")
executionError.error = "{{paramName}} must have less than {{maxItems}} elements"
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, executionError
}
{{/maxItems}}
{{#minLength}}
if strlen({{^isPathParam}}*{{/isPathParam}}r.{{paramName}}) < {{minLength}} {
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must have at least {{minLength}} elements")
executionError.error = "{{paramName}} must have at least {{minLength}} elements"
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, executionError
}
{{/minLength}}
{{#maxLength}}
if strlen({{^isPathParam}}*{{/isPathParam}}r.{{paramName}}) > {{maxLength}} {
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must have less than {{maxLength}} elements")
executionError.error = "{{paramName}} must have less than {{maxLength}} elements"
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, executionError
}
{{/maxLength}}
{{#minimum}}
Expand All @@ -143,7 +151,8 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
{{^isString}}
if {{^isPathParam}}*{{/isPathParam}}r.{{paramName}} < {{minimum}} {
{{/isString}}
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must be greater than {{minimum}}")
executionError.error = "{{paramName}} must be greater than {{minimum}}"
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, executionError
}
{{/minimum}}
{{#maximum}}
Expand All @@ -154,7 +163,8 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
{{^isString}}
if {{^isPathParam}}*{{/isPathParam}}r.{{paramName}} > {{maximum}} {
{{/isString}}
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must be less than {{maximum}}")
executionError.error = "{{paramName}} must be less than {{maximum}}"
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, executionError
}
{{/maximum}}
{{/required}}
Expand Down Expand Up @@ -257,7 +267,8 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
if r.{{paramName}} != nil {
paramJson, err := parameterToJson(*r.{{paramName}})
if err != nil {
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, err
executionError.error = err.Error()
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, executionError
}
localVarFormParams.Add("{{baseName}}", paramJson)
}
Expand Down Expand Up @@ -312,18 +323,22 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
{{/authMethods}}
req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
if err != nil {
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, err
executionError.error = err.Error()
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, executionError
}

localVarHTTPResponse, err := a.client.callAPI(req)
if err != nil || localVarHTTPResponse == nil {
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, err
executionError.error = err.Error()
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, executionError
}

localVarBody, err := _ioutil.ReadAll(localVarHTTPResponse.Body)
localVarHTTPResponse.Body.Close()
localVarHTTPResponse.Body = _ioutil.NopCloser(bytes.NewBuffer(localVarBody))
if err != nil {
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, err
executionError.error = err.Error()
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, executionError
}

if localVarHTTPResponse.StatusCode >= 300 {
Expand Down Expand Up @@ -369,7 +384,7 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
}

{{/returnType}}
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, nil
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, executionError
}
{{/operation}}
{{/operations}}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
configuration := {{goImportAlias}}.NewConfiguration()
api_client := {{goImportAlias}}.NewAPIClient(configuration)
resp, r, err := api_client.{{classname}}.{{operationId}}(context.Background(){{#pathParams}}, {{paramName}}{{/pathParams}}){{#allParams}}{{^isPathParam}}.{{vendorExtensions.x-export-param-name}}({{paramName}}){{/isPathParam}}{{/allParams}}.Execute()
if err != nil {
if err.Error() != "" {
fmt.Fprintf(os.Stderr, "Error when calling `{{classname}}.{{operationId}}``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
Expand Down
32 changes: 16 additions & 16 deletions samples/client/petstore/go/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestOAuth2(t *testing.T) {

r, err := client.PetApi.AddPet(context.Background()).Body(newPet).Execute()

if err != nil {
if err.Error() != "" {
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
Expand All @@ -52,7 +52,7 @@ func TestOAuth2(t *testing.T) {

r, err = client.PetApi.DeletePet(auth, 12992).Execute()

if err != nil {
if err.Error() != "" {
t.Fatalf("Error while deleting pet by id: %v", err)
}
if r.StatusCode != 200 {
Expand All @@ -78,7 +78,7 @@ func TestBasicAuth(t *testing.T) {

r, err := client.PetApi.AddPet(auth).Body(newPet).Execute()

if err != nil {
if err.Error() != "" {
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
Expand All @@ -87,7 +87,7 @@ func TestBasicAuth(t *testing.T) {

r, err = client.PetApi.DeletePet(auth, 12992).Execute()

if err != nil {
if err.Error() != "" {
t.Fatalf("Error while deleting pet by id: %v", err)
}
if r.StatusCode != 200 {
Expand All @@ -108,7 +108,7 @@ func TestAccessToken(t *testing.T) {

r, err := client.PetApi.AddPet(nil).Body(newPet).Execute()

if err != nil {
if err.Error() != "" {
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
Expand All @@ -117,7 +117,7 @@ func TestAccessToken(t *testing.T) {

r, err = client.PetApi.DeletePet(auth, 12992).Execute()

if err != nil {
if err.Error() != "" {
t.Fatalf("Error while deleting pet by id: %v", err)
}
if r.StatusCode != 200 {
Expand All @@ -138,15 +138,15 @@ func TestAPIKeyNoPrefix(t *testing.T) {

r, err := client.PetApi.AddPet(context.Background()).Body(newPet).Execute()

if err != nil {
if err.Error() != "" {
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
}

_, r, err = client.PetApi.GetPetById(auth, 12992).Execute()
if err != nil {
if err.Error() != "" {
t.Fatalf("Error while deleting pet by id: %v", err)
}

Expand All @@ -156,7 +156,7 @@ func TestAPIKeyNoPrefix(t *testing.T) {
}

r, err = client.PetApi.DeletePet(auth, 12992).Execute()
if err != nil {
if err.Error() != "" {
t.Fatalf("Error while deleting pet by id: %v", err)
}
if r.StatusCode != 200 {
Expand All @@ -173,15 +173,15 @@ func TestAPIKeyWithPrefix(t *testing.T) {

r, err := client.PetApi.AddPet(nil).Body(newPet).Execute()

if err != nil {
if err.Error() != "" {
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
}

_, r, err = client.PetApi.GetPetById(auth, 12992).Execute()
if err != nil {
if err.Error() != "" {
t.Fatalf("Error while deleting pet by id: %v", err)
}

Expand All @@ -191,7 +191,7 @@ func TestAPIKeyWithPrefix(t *testing.T) {
}

r, err = client.PetApi.DeletePet(auth, 12992).Execute()
if err != nil {
if err.Error() != "" {
t.Fatalf("Error while deleting pet by id: %v", err)
}
if r.StatusCode != 200 {
Expand All @@ -206,7 +206,7 @@ func TestDefaultHeader(t *testing.T) {

r, err := client.PetApi.AddPet(context.Background()).Body(newPet).Execute()

if err != nil {
if err.Error() != "" {
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
Expand All @@ -215,7 +215,7 @@ func TestDefaultHeader(t *testing.T) {

r, err = client.PetApi.DeletePet(context.Background(), 12992).Execute()

if err != nil {
if err.Error() != "" {
t.Fatalf("Error while deleting pet by id: %v", err)
}
if r.StatusCode != 200 {
Expand All @@ -230,7 +230,7 @@ func TestDefaultHeader(t *testing.T) {
func TestHostOverride(t *testing.T) {
_, r, err := client.PetApi.FindPetsByStatus(context.Background()).Status(nil).Execute()

if err != nil {
if err.Error() != "" {
t.Fatalf("Error while finding pets by status: %v", err)
}

Expand All @@ -242,7 +242,7 @@ func TestHostOverride(t *testing.T) {
func TestSchemeOverride(t *testing.T) {
_, r, err := client.PetApi.FindPetsByStatus(context.Background()).Status(nil).Execute()

if err != nil {
if err.Error() != "" {
t.Fatalf("Error while finding pets by status: %v", err)
}

Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/go/fake_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestPutBodyWithFileSchema(t *testing.T) {

r, err := client.FakeApi.TestBodyWithFileSchema(context.Background()).Body(schema).Execute()

if err != nil {
if err.Error() != "" {
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
Expand Down
Loading