Skip to content

Commit 5b49812

Browse files
committed
Always parse JSON error, store it in BDD
1 parent 76bfb79 commit 5b49812

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

.generator/templates/client.mustache

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -467,21 +467,18 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err
467467
}
468468
return nil
469469
}
470-
if jsonCheck.MatchString(contentType) {
471-
if actualObj, ok := v.(interface{ GetActualInstance() interface{} }); ok { // oneOf, anyOf schemas
472-
if unmarshalObj, ok := actualObj.(interface{ UnmarshalJSON([]byte) error }); ok { // make sure it has UnmarshalJSON defined
473-
if err = unmarshalObj.UnmarshalJSON(b); err != nil {
474-
return err
475-
}
476-
} else {
477-
return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined")
470+
if actualObj, ok := v.(interface{ GetActualInstance() interface{} }); ok { // oneOf, anyOf schemas
471+
if unmarshalObj, ok := actualObj.(interface{ UnmarshalJSON([]byte) error }); ok { // make sure it has UnmarshalJSON defined
472+
if err = unmarshalObj.UnmarshalJSON(b); err != nil {
473+
return err
478474
}
479-
} else if err = json.Unmarshal(b, v); err != nil { // simple model
480-
return err
475+
} else {
476+
return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined")
481477
}
482-
return nil
478+
} else if err = json.Unmarshal(b, v); err != nil { // simple model
479+
return err
483480
}
484-
return errors.New("undefined response type")
481+
return nil
485482
}
486483

487484
// Add a file to the multipart request

tests/scenarios/step_definitions.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,26 @@ func requestIsSent(t gobdd.StepTest, ctx gobdd.Context) {
229229
if code < 300 && err != nil {
230230
t.Errorf("unexpected error: %v", err)
231231
}
232-
// Store the unmarshalled JSON in context
233-
responseJSON, err := toJSON(result[0])
232+
var responseJSON interface{}
234233
if err != nil {
235-
t.Errorf("Unable to decode response object to JSON: %v", err)
234+
version := GetVersion(ctx)
235+
if version == "v1" {
236+
err := err.(v1.GenericOpenAPIError)
237+
if newErr := json.Unmarshal(err.Body(), &responseJSON); newErr != nil {
238+
t.Errorf("Unable to decode response object to JSON: %v", newErr)
239+
}
240+
} else {
241+
err := err.(v2.GenericOpenAPIError)
242+
if newErr := json.Unmarshal(err.Body(), &responseJSON); newErr != nil {
243+
t.Errorf("Unable to decode response object to JSON: %v", newErr)
244+
}
245+
}
246+
} else {
247+
// Store the unmarshalled JSON in context
248+
responseJSON, err = toJSON(result[0])
249+
if err != nil {
250+
t.Errorf("Unable to decode response object to JSON: %v", err)
251+
}
236252
}
237253
ctx.Set(jsonResponseKey{}, responseJSON)
238254
}

0 commit comments

Comments
 (0)