Skip to content

Commit eb350d7

Browse files
api-clients-generation-pipeline[bot]NouemanKHALci.datadog-api-spec
authored
Refactor typescript code to reduce the library size (#350)
* refactor serializiation logic and attributeTypeMap * remove serialization methods, fix raw object * add required and name properties, and make format optional * fix typo * fix issues, add optional EnumValues * make enumValues of type any * fix enum checks * remove enumValues from classes * last working change * change enumsMap map type to any[] * remove discriminator and findCorrectType * Regenerate client from commit 40ae541 of spec repo Co-authored-by: NouemanKHAL <[email protected]> Co-authored-by: api-clients-generation-pipeline[bot] <54105614+api-clients-generation-pipeline[bot]@users.noreply.github.com> Co-authored-by: ci.datadog-api-spec <[email protected]>
1 parent fdecc53 commit eb350d7

File tree

726 files changed

+6245
-57342
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

726 files changed

+6245
-57342
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-03 12:23:56.257041",
8-
"spec_repo_commit": "5a8a968"
7+
"regenerated": "2021-11-03 13:12:22.209070",
8+
"spec_repo_commit": "40ae541"
99
},
1010
"v2": {
1111
"apigentools_version": "1.5.1.dev2",
12-
"regenerated": "2021-11-03 12:23:56.280638",
13-
"spec_repo_commit": "5a8a968"
12+
"regenerated": "2021-11-03 13:12:22.241215",
13+
"spec_repo_commit": "40ae541"
1414
}
1515
}
1616
}

.generator/templates/model/ObjectSerializer.mustache

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,22 @@ const supportedMediaTypes: { [mediaType: string]: number } = {
2222
"application/octet-stream": 0
2323
}
2424

25-
26-
let enumsMap: Set<string> = new Set<string>([
25+
const enumsMap: {[key: string]: any[]} = {
2726
{{#models}}
2827
{{#model}}
2928
{{#isEnum}}
30-
"{{classname}}{{enumName}}",
29+
"{{classname}}{{enumName}}": [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}]{{/allowableValues}},
3130
{{/isEnum}}
3231
{{#hasEnums}}
3332
{{#vars}}
3433
{{#isEnum}}
35-
"{{classname}}{{enumName}}",
34+
"{{classname}}{{enumName}}": [{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}]{{/allowableValues}},
3635
{{/isEnum}}
3736
{{/vars}}
3837
{{/hasEnums}}
3938
{{/model}}
4039
{{/models}}
41-
]);
40+
};
4241

4342
let typeMap: {[index: string]: any} = {
4443
{{#models}}
@@ -58,41 +57,6 @@ let oneOfMap: {[index: string]: string[]} = {
5857
};
5958

6059
export class ObjectSerializer {
61-
public static findCorrectType(data: any, expectedType: string) {
62-
if (data == undefined) {
63-
return expectedType;
64-
} else if (primitives.indexOf(expectedType.toLowerCase()) !== -1) {
65-
return expectedType;
66-
} else if (expectedType === "Date") {
67-
return expectedType;
68-
} else {
69-
if (enumsMap.has(expectedType)) {
70-
return expectedType;
71-
}
72-
73-
if (!typeMap[expectedType]) {
74-
return expectedType; // w/e we don't know the type
75-
}
76-
77-
// Check the discriminator
78-
let discriminatorProperty = typeMap[expectedType].discriminator;
79-
if (discriminatorProperty == null) {
80-
return expectedType; // the type does not have a discriminator. use it.
81-
} else {
82-
if (data[discriminatorProperty]) {
83-
var discriminatorType = data[discriminatorProperty];
84-
if(typeMap[discriminatorType]){
85-
return discriminatorType; // use the type given in the discriminator
86-
} else {
87-
return expectedType; // discriminator did not map to a type
88-
}
89-
} else {
90-
return expectedType; // discriminator was not present (or an empty string)
91-
}
92-
}
93-
}
94-
}
95-
9660
public static serialize(data: any, type: string, format: string) {
9761
if (data == undefined) {
9862
return data;
@@ -131,14 +95,17 @@ export class ObjectSerializer {
13195
return data.toISOString();
13296
}
13397
} else {
134-
if (enumsMap.has(type)) {
135-
return data;
98+
if (enumsMap[type]) {
99+
if (enumsMap[type].includes(data)) {
100+
return data;
101+
}
102+
throw new TypeError(`unknown enum value '${data}'`)
136103
}
137104
if (oneOfMap[type]) {
138105
let oneOfs: any[] = [];
139106
for(let oneOf of oneOfMap[type]) {
140107
try {
141-
oneOfs.push(ObjectSerializer.serialize(data, oneOf, format))
108+
oneOfs.push(ObjectSerializer.serialize(data, oneOf, format));
142109
} catch (e) {
143110
console.debug(`could not serialize ${oneOf} (${e})`)
144111
}
@@ -152,17 +119,28 @@ export class ObjectSerializer {
152119
return oneOfs[0];
153120
}
154121
155-
if (!typeMap[type]) { // dont know the type
156-
throw new TypeError(`unknown type '${type}'`);
157-
}
122+
// get the map for the correct type.
123+
let attributesMap = typeMap[type].getAttributeTypeMap();
124+
let instance: {[index: string]: any} = {};
158125
159-
return typeMap[type].serialize(data);
126+
for (let attributeName in attributesMap) {
127+
let attributeObj = attributesMap[attributeName];
128+
instance[attributeObj.baseName] = ObjectSerializer.serialize(data[attributeName], attributeObj.type, attributeObj.format);
129+
130+
// check for required properties
131+
if (attributeObj?.required && instance[attributeObj.baseName] === undefined) {
132+
throw new Error(`missing required property '${attributeObj.baseName}'`);
133+
}
134+
135+
if (enumsMap[attributeObj.type] && !enumsMap[attributeObj.type].includes(instance[attributeObj.baseName])) {
136+
instance.unparsedObject = instance[attributeObj.baseName];
137+
}
138+
}
139+
return instance;
160140
}
161141
}
162142
163-
public static deserialize(data: any, type: string, format: string) {
164-
// polymorphism may change the actual type.
165-
type = ObjectSerializer.findCorrectType(data, type);
143+
public static deserialize(data: any, type: string, format: string = "") {
166144
if (data == undefined) {
167145
return data;
168146
} else if (primitives.indexOf(type.toLowerCase()) !== -1) {
@@ -188,8 +166,8 @@ export class ObjectSerializer {
188166
} else if (type === "Date") {
189167
return new Date(data);
190168
} else {
191-
if (enumsMap.has(type)) {// is Enum
192-
return data;
169+
if (enumsMap[type]) {
170+
return data;
193171
}
194172
195173
if (oneOfMap[type]) {
@@ -214,7 +192,26 @@ export class ObjectSerializer {
214192
if (!typeMap[type]) { // dont know the type
215193
throw new TypeError(`unknown type '${type}'`);
216194
}
217-
return typeMap[type].deserialize(data);
195+
196+
let instance = new typeMap[type]();
197+
let attributesMap = typeMap[type].getAttributeTypeMap();
198+
199+
for (let attributeName in attributesMap) {
200+
let attributeObj = attributesMap[attributeName];
201+
instance[attributeName] = ObjectSerializer.deserialize(data[attributeObj.baseName], attributeObj.type, attributeObj.format);
202+
203+
// check for required properties
204+
if (attributeObj?.required && instance[attributeName] == undefined) {
205+
throw new Error(`missing required property '${attributeName}'`);
206+
}
207+
208+
// check for enum values
209+
if (enumsMap[attributeObj.type] && !enumsMap[attributeObj.type].includes(instance[attributeName])) {
210+
instance.unparsedObject = instance[attributeName];
211+
}
212+
}
213+
214+
return instance;
218215
}
219216
}
220217

.generator/templates/model/model.mustache

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,14 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
2525

2626
'unparsedObject'?:any;
2727

28-
{{#discriminator}}
29-
static readonly discriminator: string | undefined = "{{discriminatorName}}";
30-
{{/discriminator}}
31-
{{^discriminator}}
32-
static readonly discriminator: string | undefined = undefined;
33-
{{/discriminator}}
34-
3528
{{^isArray}}
36-
static readonly attributeTypeMap: {[key: string]: {baseName: string, type: string, format: string}} = {
29+
static readonly attributeTypeMap: {[key: string]: {baseName: string, type: string, required?: boolean, format?: string}} = {
3730
{{#vars}}
3831
"{{name}}": {
3932
"baseName": "{{baseName}}",
4033
"type": "{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}",
41-
"format": "{{dataFormat}}"
34+
{{#required}}"required": {{required}},{{/required}}
35+
{{#dataFormat}}"format": "{{.}}",{{/dataFormat}}
4236
}{{^-last}},
4337
{{/-last}}
4438
{{/vars}}
@@ -54,66 +48,6 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
5448
}
5549
{{/isArray}}
5650

57-
static deserialize(data: {[key: string]: any}): {{classname}} {
58-
let res = new {{classname}}();
59-
60-
{{#vars}}
61-
{{#required}}
62-
if (data.{{{baseName}}} === undefined) {
63-
throw new TypeError("missing required attribute '{{baseName}}' on '{{classname}}' object");
64-
}
65-
{{/required}}
66-
{{#allowableValues}}
67-
if ([{{#enumVars}}{{{value}}}, {{/enumVars}}undefined].includes(data.{{{baseName}}})) {
68-
res.{{name}} = data.{{{baseName}}};
69-
} else {
70-
let raw = new {{classname}}();
71-
raw.unparsedObject = data;
72-
return raw;
73-
}
74-
75-
{{/allowableValues}}
76-
{{^allowableValues}}
77-
res.{{{name}}} = ObjectSerializer.deserialize(data.{{{baseName}}}, "{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}", "{{dataFormat}}")
78-
{{/allowableValues}}
79-
80-
{{/vars}}
81-
82-
return res;
83-
}
84-
85-
static serialize(data: {{classname}}): {[key: string]: any} {
86-
let attributeTypes = {{{classname}}}.getAttributeTypeMap();
87-
let res: {[index: string]: any} = {};
88-
for (let [key, value] of Object.entries(data)) {
89-
if (!(key in attributeTypes)) {
90-
throw new TypeError(`${key} attribute not in schema`);
91-
}
92-
}
93-
if (data?.unparsedObject !== undefined) {
94-
return data.unparsedObject;
95-
}
96-
{{#vars}}
97-
{{#required}}
98-
if (data.{{{name}}} === undefined) {
99-
throw new TypeError("missing required attribute '{{baseName}}' on '{{classname}}' object");
100-
}
101-
{{/required}}
102-
{{#allowableValues}}
103-
if ([{{#enumVars}}{{{value}}}, {{/enumVars}}undefined].includes(data.{{{name}}})) {
104-
res.{{baseName}} = data.{{{name}}};
105-
} else {
106-
throw TypeError(`invalid enum value ${ data.{{{name}}} } for {{name}}`);
107-
}
108-
{{/allowableValues}}
109-
{{^allowableValues}}
110-
res.{{{baseName}}} = ObjectSerializer.serialize(data.{{{name}}}, "{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}", "{{dataFormat}}")
111-
{{/allowableValues}}
112-
113-
{{/vars}}
114-
return res
115-
}
116-
11751
public constructor() {
11852
{{#parent}}
11953
super();

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

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88
* Do not edit the class manually.
99
*/
1010

11-
import { ObjectSerializer } from "./ObjectSerializer";
12-
13-
/**
14-
* Error response object.
15-
*/
16-
1711
export class APIErrorResponse {
1812
/**
1913
* Array of errors returned by the API.
@@ -22,55 +16,24 @@ export class APIErrorResponse {
2216

2317
"unparsedObject"?: any;
2418

25-
static readonly discriminator: string | undefined = undefined;
26-
2719
static readonly attributeTypeMap: {
28-
[key: string]: { baseName: string; type: string; format: string };
20+
[key: string]: {
21+
baseName: string;
22+
type: string;
23+
required?: boolean;
24+
format?: string;
25+
};
2926
} = {
3027
errors: {
3128
baseName: "errors",
3229
type: "Array<string>",
33-
format: "",
30+
required: true,
3431
},
3532
};
3633

3734
static getAttributeTypeMap() {
3835
return APIErrorResponse.attributeTypeMap;
3936
}
4037

41-
static deserialize(data: { [key: string]: any }): APIErrorResponse {
42-
const res = new APIErrorResponse();
43-
44-
if (data.errors === undefined) {
45-
throw new TypeError(
46-
"missing required attribute 'errors' on 'APIErrorResponse' object"
47-
);
48-
}
49-
res.errors = ObjectSerializer.deserialize(data.errors, "Array<string>", "");
50-
51-
return res;
52-
}
53-
54-
static serialize(data: APIErrorResponse): { [key: string]: any } {
55-
const attributeTypes = APIErrorResponse.getAttributeTypeMap();
56-
const res: { [index: string]: any } = {};
57-
for (const [key, value] of Object.entries(data)) {
58-
if (!(key in attributeTypes)) {
59-
throw new TypeError(`${key} attribute not in schema`);
60-
}
61-
}
62-
if (data?.unparsedObject !== undefined) {
63-
return data.unparsedObject;
64-
}
65-
if (data.errors === undefined) {
66-
throw new TypeError(
67-
"missing required attribute 'errors' on 'APIErrorResponse' object"
68-
);
69-
}
70-
res.errors = ObjectSerializer.serialize(data.errors, "Array<string>", "");
71-
72-
return res;
73-
}
74-
7538
public constructor() {}
7639
}

0 commit comments

Comments
 (0)