3
3
import { {{ name }} } from "./{{ name }}";
4
4
{% - endif %}
5
5
{% - endfor %}
6
- import { UnparsedObject } from "../../datadog-api-client-common/util";
6
+ import { dateFromRFC3339String, dateToRFC3339String, UnparsedObject } from "../../datadog-api-client-common/util";
7
7
import { logger } from "../../../logger";
8
8
9
9
const primitives = [
@@ -54,6 +54,8 @@ export class ObjectSerializer {
54
54
public static serialize(data: any, type: string, format: string): any {
55
55
if (data == undefined || type == "any") {
56
56
return data;
57
+ } else if (data instanceof UnparsedObject) {
58
+ return data._data;
57
59
} else if (primitives.includes(type.toLowerCase()) && typeof data == type.toLowerCase()) {
58
60
return data;
59
61
} else if (type.startsWith(ARRAY_PREFIX)) {
@@ -87,12 +89,8 @@ export class ObjectSerializer {
87
89
if ("string" == typeof data) {
88
90
return data;
89
91
}
90
- if (format == "date") {
91
- let month = data.getMonth() + 1
92
- month = month < 10 ? "0" + month.toString() : month.toString()
93
- let day = data.getDate();
94
- day = day < 10 ? "0" + day.toString() : day.toString();
95
- return data.getFullYear() + "-" + month + "-" + day;
92
+ if (format == "date" || format == "date-time") {
93
+ return dateToRFC3339String(data)
96
94
} else {
97
95
return data.toISOString();
98
96
}
@@ -129,21 +127,6 @@ export class ObjectSerializer {
129
127
const attributesMap = typeMap[type].getAttributeTypeMap();
130
128
const instance: {[index: string]: any} = {};
131
129
132
- const extraAttributes = Object.keys(data)
133
- .filter((key) => !Object.prototype.hasOwnProperty.call(attributesMap, key))
134
- .reduce((obj, key) => {
135
- return Object.assign(obj, {
136
- [key]: data[key]
137
- });
138
- }, {});
139
-
140
- if (Object.keys(extraAttributes).length !== 0) {
141
- if (!data.additionalProperties) {
142
- data.additionalProperties = {};
143
- }
144
- Object.assign(data.additionalProperties, extraAttributes);
145
- }
146
-
147
130
for (const attributeName in attributesMap) {
148
131
const attributeObj = attributesMap[attributeName];
149
132
if (attributeName == "additionalProperties") {
@@ -163,11 +146,8 @@ export class ObjectSerializer {
163
146
if (attributeObj?.required && instance[attributeObj.baseName] === undefined) {
164
147
throw new Error(`missing required property '${attributeObj.baseName}'`);
165
148
}
166
-
167
- if (enumsMap[attributeObj.type] && !enumsMap[attributeObj.type].includes(instance[attributeObj.baseName])) {
168
- instance.unparsedObject = instance[attributeObj.baseName];
169
- }
170
149
}
150
+
171
151
return instance;
172
152
}
173
153
}
@@ -206,18 +186,20 @@ export class ObjectSerializer {
206
186
}
207
187
return transformedData;
208
188
} else if (type === "Date") {
209
- return new Date (data);
189
+ return dateFromRFC3339String (data)
210
190
} else {
211
191
if (enumsMap[type]) {
212
- return data;
192
+ if (enumsMap[type].includes(data)) {
193
+ return data;
194
+ }
195
+ return new UnparsedObject(data)
213
196
}
214
-
215
197
if (oneOfMap[type]) {
216
198
const oneOfs: any[] = [];
217
199
for (const oneOf of oneOfMap[type]) {
218
200
try {
219
201
const d = ObjectSerializer.deserialize(data, oneOf, format);
220
- if (d?.unparsedObject === undefined ) {
202
+ if (! d?._unparsed ) {
221
203
oneOfs.push(d);
222
204
}
223
205
} catch (e) {
@@ -237,18 +219,49 @@ export class ObjectSerializer {
237
219
238
220
const instance = new typeMap[type]();
239
221
const attributesMap = typeMap[type].getAttributeTypeMap();
222
+ let extraAttributes: any = []
223
+ if ("additionalProperties" in attributesMap) {
224
+ const attributesBaseNames = Object.keys(attributesMap).reduce((o, key) => Object.assign(o, {[attributesMap[key].baseName]: ""}), {});
225
+ extraAttributes = Object.keys(data).filter((key) => !Object.prototype.hasOwnProperty.call(attributesBaseNames, key))
226
+ }
240
227
241
228
for (const attributeName in attributesMap) {
242
229
const attributeObj = attributesMap[attributeName];
230
+ if (attributeName == "additionalProperties") {
231
+ if (extraAttributes.length > 0) {
232
+ if (!instance.additionalProperties) {
233
+ instance.additionalProperties = {};
234
+ }
235
+
236
+ for (const key in extraAttributes) {
237
+ instance.additionalProperties[extraAttributes[key]] = ObjectSerializer.deserialize(
238
+ data[extraAttributes[key]],
239
+ attributeObj.type,
240
+ attributeObj.format
241
+ );
242
+ }
243
+ }
244
+ continue;
245
+ }
246
+
243
247
instance[attributeName] = ObjectSerializer.deserialize(data[attributeObj.baseName], attributeObj.type, attributeObj.format);
248
+
244
249
// check for required properties
245
250
if (attributeObj?.required && instance[attributeName] === undefined) {
246
251
throw new Error(`missing required property '${attributeName}'`);
247
252
}
248
253
249
- // check for enum values
250
- if (enumsMap[attributeObj.type] && !enumsMap[attributeObj.type].includes(instance[attributeName])) {
251
- instance.unparsedObject = instance[attributeName];
254
+ if (instance[attributeName] instanceof UnparsedObject || instance[attributeName]?._unparsed) {
255
+ instance._unparsed = true
256
+ }
257
+
258
+ if (Array.isArray(instance[attributeName])) {
259
+ for (const d of instance[attributeName]) {
260
+ if (d instanceof UnparsedObject || d?._unparsed) {
261
+ instance._unparsed = true
262
+ break
263
+ }
264
+ }
252
265
}
253
266
}
254
267
0 commit comments