Skip to content

Commit 1154162

Browse files
authored
More resilient date-time deserialization (#1009)
* more resilient date-time deserializing and fix error throwing
1 parent 14092ee commit 1154162

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

.generator/src/generator/templates/model/ObjectSerializer.j2

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ export class ObjectSerializer {
186186
}
187187
return transformedData;
188188
} else if (type === "Date") {
189-
return dateFromRFC3339String(data)
189+
try {
190+
return dateFromRFC3339String(data)
191+
} catch {
192+
return new Date(data)
193+
}
190194
} else {
191195
if (enumsMap[type]) {
192196
if (enumsMap[type].includes(data)) {

features/step_definitions/request_steps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ When("the request is sent", async function (this: World) {
148148
if (error instanceof datadogApiClient.client.ApiException) {
149149
this.response = error.body;
150150
} else {
151-
this.response = error;
151+
throw error;
152152
}
153153
logger.debug(error);
154154
if (this.requestContext === undefined) {
@@ -223,7 +223,7 @@ When("the request with pagination is sent", async function (this: World) {
223223
if (error instanceof datadogApiClient.client.ApiException) {
224224
this.response = error.body;
225225
} else {
226-
this.response = error;
226+
throw error;
227227
}
228228
logger.debug(error);
229229
if (this.requestContext === undefined) {

packages/datadog-api-client-v1/models/ObjectSerializer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2280,7 +2280,11 @@ export class ObjectSerializer {
22802280
}
22812281
return transformedData;
22822282
} else if (type === "Date") {
2283-
return dateFromRFC3339String(data);
2283+
try {
2284+
return dateFromRFC3339String(data);
2285+
} catch {
2286+
return new Date(data);
2287+
}
22842288
} else {
22852289
if (enumsMap[type]) {
22862290
if (enumsMap[type].includes(data)) {

packages/datadog-api-client-v2/models/ObjectSerializer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,11 @@ export class ObjectSerializer {
19571957
}
19581958
return transformedData;
19591959
} else if (type === "Date") {
1960-
return dateFromRFC3339String(data);
1960+
try {
1961+
return dateFromRFC3339String(data);
1962+
} catch {
1963+
return new Date(data);
1964+
}
19611965
} else {
19621966
if (enumsMap[type]) {
19631967
if (enumsMap[type].includes(data)) {

tests/api/date_time.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { MonthlyUsageAttributionResponse } from '../../packages/datadog-api-client-v1/models/MonthlyUsageAttributionResponse';
2+
import { ObjectSerializer as ObjectSerializerV1 } from '../../packages/datadog-api-client-v1/models/ObjectSerializer';
3+
4+
test('TestDeserializationOfInvalidDates', () => {
5+
const data = `
6+
{
7+
"usage":[
8+
{
9+
"org_name":"DD Integration Tests (321813)",
10+
"public_id":"fasjyydbcgwwc2uc",
11+
"tag_config_source":"DD Integration Tests (321813):::project",
12+
"tags":null,
13+
"updated_at":"2023-02-0",
14+
"month":"2023-02-01T00:00:00+00:00",
15+
"values":{
16+
"infra_host_usage":44
17+
}
18+
}
19+
],
20+
"metadata":{
21+
"pagination":{
22+
"next_record_id":null
23+
},
24+
"aggregates":[
25+
{
26+
"field":"infra_host_usage",
27+
"value":45.0,
28+
"agg_type":"sum"
29+
}
30+
]
31+
}
32+
}
33+
`;
34+
35+
36+
const result = ObjectSerializerV1.deserialize(
37+
ObjectSerializerV1.parse(data, "application/json"),
38+
"MonthlyUsageAttributionResponse",
39+
"");
40+
41+
// Invalid dates should be deserialized into an Invalid Date instance
42+
expect(result).toBeInstanceOf(MonthlyUsageAttributionResponse)
43+
expect(result.usage[0].updatedAt).toBeInstanceOf(Date);
44+
}
45+
);

0 commit comments

Comments
 (0)