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

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

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 16 additions & 31 deletions modules/openapi-generator/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package {{packageName}}

{{#operations}}
import (
"bytes"
_context "context"
_ioutil "io/ioutil"
_nethttp "net/http"
Expand Down Expand Up @@ -36,7 +35,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, GenericOpenAPIError)
{{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) ({{#returnType}}{{{.}}}, {{/returnType}}*_nethttp.Response, error)
{{/operation}}
}
{{/generateInterfaces}}
Expand All @@ -61,7 +60,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, GenericOpenAPIError) {
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) Execute() ({{#returnType}}{{{.}}}, {{/returnType}}*_nethttp.Response, error) {
return r.ApiService.{{nickname}}Execute(r)
}

Expand All @@ -86,23 +85,21 @@ 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, GenericOpenAPIError) {
func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) ({{#returnType}}{{{.}}}, {{/returnType}}*_nethttp.Response, error) {
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 {
executionError.error = err.Error()
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, executionError
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, GenericOpenAPIError{error: err.Error()}
}

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

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

localVarBody, err := _ioutil.ReadAll(localVarHTTPResponse.Body)
localVarHTTPResponse.Body.Close()
localVarHTTPResponse.Body = _ioutil.NopCloser(bytes.NewBuffer(localVarBody))
wing328 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
executionError.error = err.Error()
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, executionError
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, err
}

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

{{/returnType}}
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, executionError
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, nil
}
{{/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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
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.Error() != "" {
if err != nil {
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
Expand Down
Loading