Skip to content

Commit c4d633e

Browse files
api-clients-generation-pipeline[bot]therveci.datadog-api-spec
authored
Be more resilient to plain text errors (#1227)
* Always parse JSON error, store it in BDD * Regenerate client from commit 60c6582 of spec repo Co-authored-by: Thomas Hervé <[email protected]> Co-authored-by: ci.datadog-api-spec <[email protected]> Co-authored-by: api-clients-generation-pipeline[bot] <54105614+api-clients-generation-pipeline[bot]@users.noreply.github.com>
1 parent 76bfb79 commit c4d633e

File tree

8 files changed

+105
-43
lines changed

8 files changed

+105
-43
lines changed

.apigentools-info

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"spec_versions": {
55
"v1": {
66
"apigentools_version": "1.5.1.dev2",
7-
"regenerated": "2021-11-26 10:29:43.003956",
8-
"spec_repo_commit": "fdabc88"
7+
"regenerated": "2021-11-29 17:29:19.610352",
8+
"spec_repo_commit": "60c6582"
99
},
1010
"v2": {
1111
"apigentools_version": "1.5.1.dev2",
12-
"regenerated": "2021-11-26 10:29:43.022631",
13-
"spec_repo_commit": "fdabc88"
12+
"regenerated": "2021-11-29 17:29:19.629845",
13+
"spec_repo_commit": "60c6582"
1414
}
1515
}
1616
}

.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

api/v1/datadog/client.go

Lines changed: 9 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v2/datadog/client.go

Lines changed: 9 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2021-11-27T20:34:41.465Z
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
interactions:
2+
- request:
3+
body: |
4+
{"date_happened":1,"tags":["test:TestPostaneventinthepastreturnsBadRequestresponse1638045281"],"text":"A text message.","title":"Test-Post_an_event_in_the_past_returns_Bad_Request_response-1638045281"}
5+
form: {}
6+
headers:
7+
Accept:
8+
- application/json, */*;q=0.8
9+
Connection:
10+
- close
11+
Content-Length:
12+
- '201'
13+
Content-Type:
14+
- application/json
15+
Host:
16+
- api.datadoghq.com
17+
User-Agent:
18+
- datadog-api-client-typescript/1.0.0-beta.6 (node 14.18.1; os Linux; arch x64)
19+
X-Datadog-Parent-Id:
20+
- '343467568446934486'
21+
X-Datadog-Trace-Id:
22+
- '4038788092229867157'
23+
method: POST
24+
url: https://api.datadoghq.com/api/v1/events
25+
response:
26+
body: '{"errors": ["Event too far in the past"]}'
27+
code: 400
28+
duration: ''
29+
headers:
30+
Connection:
31+
- close
32+
Content-Length:
33+
- '41'
34+
Content-Security-Policy:
35+
- frame-ancestors 'self'; report-uri https://api.datadoghq.com/csp-report
36+
Content-Type:
37+
- text/plain; charset=utf-8
38+
Date:
39+
- Sat, 27 Nov 2021 20:34:41 GMT
40+
Strict-Transport-Security:
41+
- max-age=15724800;
42+
X-Content-Type-Options:
43+
- nosniff
44+
X-Frame-Options:
45+
- SAMEORIGIN
46+
status: 400 Bad Request
47+
version: 1

tests/scenarios/features/v1/events.feature

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ Feature: Events
4444
When the request is sent
4545
Then the response status is 202 OK
4646

47+
Scenario: Post an event in the past returns "Bad Request" response
48+
Given new "CreateEvent" request
49+
And body with value {"title": "{{ unique }}", "text": "A text message.", "date_happened": 1, "tags": ["test:{{ unique_alnum }}"]}
50+
When the request is sent
51+
Then the response status is 400 Bad Request
52+
And the response "errors[0]" is equal to "Event too far in the past"
53+
4754
@generated @skip
4855
Scenario: Query the event stream returns "Bad Request" response
4956
Given a valid "appKeyAuth" key in the system

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)