@@ -5,17 +5,19 @@ import { {{classname}}{{#hasEnums}}{{#vars}}{{#isEnum}}, {{classname}}{{enumName
5
5
{ {/models} }
6
6
import { logger } from "../../../index";
7
7
8
- /* tslint:disable:no-unused-variable */
9
- let primitives = [
10
- "string",
11
- "boolean",
12
- "double",
13
- "integer",
14
- "long",
15
- "float",
16
- "number",
17
- "any"
18
- ];
8
+ const primitives = [
9
+ "string",
10
+ "boolean",
11
+ "double",
12
+ "integer",
13
+ "long",
14
+ "float",
15
+ "number",
16
+ "any"
17
+ ];
18
+
19
+ const ARRAY_PREFIX = "Array<";
20
+ const MAP_PREFIX = "{ [key: string]: " ;
19
21
20
22
const supportedMediaTypes: { [mediaType: string]: number } = {
21
23
" application/json" : Infinity,
@@ -40,7 +42,7 @@ const enumsMap: {[key: string]: any[]} = {
40
42
{ {/models} }
41
43
};
42
44
43
- let typeMap: { [index: string]: any} = {
45
+ const typeMap: { [index: string]: any} = {
44
46
{{#models} }
45
47
{ {#model} }
46
48
{ {^isEnum} }
@@ -52,7 +54,7 @@ let typeMap: {[index: string]: any} = {
52
54
{ {/models} }
53
55
}
54
56
55
- let oneOfMap: { [index: string]: string[]} = {
57
+ const oneOfMap: { [index: string]: string[]} = {
56
58
{{#models} }{ {#model} }{ {#oneOf} }{ {#-first} }"{ {classname} }": [{ {/-first} }"{ {{.} }}"{ {^-last} }, { {/-last} }{ {#-last} }],
57
59
{ {/-last} }{ {/oneOf} }{ {/model} }{ {/models} }
58
60
};
@@ -61,32 +63,30 @@ export class ObjectSerializer {
61
63
public static serialize(data: any, type: string, format: string): any {
62
64
if (data == undefined) {
63
65
return data;
64
- } else if (primitives.indexOf (type.toLowerCase()) !== -1 ) {
66
+ } else if (primitives.includes (type.toLowerCase())) {
65
67
return data;
66
- } else if (type.lastIndexOf("Array<", 0) === 0) { // string.startsWith pre es6
67
- let subType: string = type.replace(" Array<" , " " ); // Array< Type> => Type>
68
- subType = subType.substring(0, subType.length - 1); // Type> => Type
69
- let transformedData: any[] = [];
70
- for (let index in data) {
71
- let date = data[index];
72
- transformedData.push(ObjectSerializer.serialize(date, subType, format));
68
+ } else if (type.startsWith(ARRAY_PREFIX)) {
69
+ // Array< Type> => Type
70
+ const subType: string = type.substring(ARRAY_PREFIX.length, type.length - 1);
71
+ const transformedData: any[] = [];
72
+ for (const element of data) {
73
+ transformedData.push(ObjectSerializer.serialize(element, subType, format));
73
74
}
74
75
return transformedData;
75
- } else if (type.lastIndexOf("{ [key: string]: " , 0) === 0) { // string.startsWith pre es6
76
- let subType: string = type.replace(" { [key: string]: " , " " ); // { [key: string]: Type; } => Type; }
77
- subType = subType.substring(0, subType.length - 3); // Type; } => Type
78
- let transformedData: { [key: string]: any } = {};
79
- for (let k in data) {
80
- let date = data[k]
81
- transformedData[k] = ObjectSerializer.serialize(date, subType, format);
76
+ } else if (type.startsWith(MAP_PREFIX)) {
77
+ // { [key: string]: Type; } => Type
78
+ const subType: string = type.substring(MAP_PREFIX.length, type.length - 3);
79
+ const transformedData: { [key: string]: any } = { } ;
80
+ for (const key in data) {
81
+ transformedData[key] = ObjectSerializer.serialize(data[key], subType, format);
82
82
}
83
83
return transformedData;
84
84
} else if (type === "Date") {
85
85
if (" string" == typeof data) {
86
86
return data;
87
87
}
88
88
if (format == "date") {
89
- let month = data.getMonth()+ 1
89
+ let month = data.getMonth() + 1
90
90
month = month < 10 ? " 0" + month.toString() : month.toString()
91
91
let day = data.getDate();
92
92
day = day < 10 ? " 0" + day.toString() : day.toString();
@@ -103,8 +103,8 @@ export class ObjectSerializer {
103
103
throw new TypeError(`unknown enum value '${ data} '`)
104
104
}
105
105
if (oneOfMap[type]) {
106
- let oneOfs: any[] = [];
107
- for(let oneOf of oneOfMap[type]) {
106
+ const oneOfs: any[] = [];
107
+ for (const oneOf of oneOfMap[type]) {
108
108
try {
109
109
oneOfs.push(ObjectSerializer.serialize(data, oneOf, format));
110
110
} catch (e) {
@@ -120,12 +120,16 @@ export class ObjectSerializer {
120
120
return oneOfs[0];
121
121
}
122
122
123
+ if (!typeMap[type]) { // dont know the type
124
+ throw new TypeError(`unknown type ' ${type}' `);
125
+ }
126
+
123
127
// get the map for the correct type.
124
- let attributesMap = typeMap[type].getAttributeTypeMap();
125
- let instance: {[index: string]: any} = {};
128
+ const attributesMap = typeMap[type].getAttributeTypeMap();
129
+ const instance: { [index: string]: any} = { } ;
126
130
127
- for (let attributeName in attributesMap) {
128
- let attributeObj = attributesMap[attributeName];
131
+ for (const attributeName in attributesMap) {
132
+ const attributeObj = attributesMap[attributeName];
129
133
instance[attributeObj.baseName] = ObjectSerializer.serialize(data[attributeName], attributeObj.type, attributeObj.format);
130
134
131
135
// check for required properties
@@ -144,24 +148,22 @@ export class ObjectSerializer {
144
148
public static deserialize(data: any, type: string, format: string = ""): any {
145
149
if (data == undefined) {
146
150
return data;
147
- } else if (primitives.indexOf (type.toLowerCase()) !== -1 ) {
151
+ } else if (primitives.includes (type.toLowerCase())) {
148
152
return data;
149
- } else if (type.lastIndexOf(" Array< " , 0) === 0) { // string.startsWith pre es6
150
- let subType: string = type.replace(" Array< " , " " ); // Array<Type> => Type>
151
- subType = subType.substring(0, subType.length - 1); // Type> => Type
152
- let transformedData: any[] = [];
153
- for (let index in data) {
154
- let date = data[index];
155
- transformedData.push(ObjectSerializer.deserialize(date, subType, format));
153
+ } else if (type.startsWith(ARRAY_PREFIX)) {
154
+ // Array< Type> => Type
155
+ const subType: string = type.substring(ARRAY_PREFIX.length, type.length - 1);
156
+ const transformedData: any[] = [];
157
+ for (const element of data) {
158
+ transformedData.push(ObjectSerializer.deserialize(element, subType, format));
156
159
}
157
160
return transformedData;
158
- } else if (type.lastIndexOf(" { [key: string]: " , 0) === 0) { // string.startsWith pre es6
159
- let subType: string = type.replace(" { [key: string]: " , " " ); // { [key: string]: Type; } => Type; }
160
- subType = subType.substring(0, subType.length - 3); // Type; } => Type
161
- let transformedData: { [key: string]: any } = {};
162
- for (let k in data) {
163
- let date = data[k]
164
- transformedData[k] = ObjectSerializer.deserialize(date, subType, format);
161
+ } else if (type.startsWith(MAP_PREFIX)) {
162
+ // { [key: string]: Type; } => Type
163
+ const subType: string = type.substring(MAP_PREFIX.length, type.length - 3);
164
+ const transformedData: { [key: string]: any } = { } ;
165
+ for (const key in data) {
166
+ transformedData[key] = ObjectSerializer.deserialize(data[key], subType, format);
165
167
}
166
168
return transformedData;
167
169
} else if (type === "Date") {
@@ -172,10 +174,10 @@ export class ObjectSerializer {
172
174
}
173
175
174
176
if (oneOfMap[type]) {
175
- let oneOfs: any[] = [];
176
- for(let oneOf of oneOfMap[type]) {
177
+ const oneOfs: any[] = [];
178
+ for (const oneOf of oneOfMap[type]) {
177
179
try {
178
- let d = ObjectSerializer.deserialize(data, oneOf, format);
180
+ const d = ObjectSerializer.deserialize(data, oneOf, format);
179
181
if (d?.unparsedObject === undefined) {
180
182
oneOfs.push(d);
181
183
}
@@ -194,11 +196,11 @@ export class ObjectSerializer {
194
196
throw new TypeError(`unknown type ' ${type}' `);
195
197
}
196
198
197
- let instance = new typeMap[type]();
198
- let attributesMap = typeMap[type].getAttributeTypeMap();
199
+ const instance = new typeMap[type]();
200
+ const attributesMap = typeMap[type].getAttributeTypeMap();
199
201
200
- for (let attributeName in attributesMap) {
201
- let attributeObj = attributesMap[attributeName];
202
+ for (const attributeName in attributesMap) {
203
+ const attributeObj = attributesMap[attributeName];
202
204
instance[attributeName] = ObjectSerializer.deserialize(data[attributeObj.baseName], attributeObj.type, attributeObj.format);
203
205
204
206
// check for required properties
0 commit comments