@azure/core-client 1.10.2-alpha.20260311.2 → 1.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/authorizeRequestOnTenantChallenge.js +1 -2
- package/dist/browser/authorizeRequestOnTenantChallenge.js.map +1 -1
- package/dist/browser/base64.d.ts +1 -5
- package/dist/browser/base64.js +6 -14
- package/dist/browser/base64.js.map +1 -1
- package/dist/browser/interfaceHelpers.js +1 -2
- package/dist/browser/interfaceHelpers.js.map +1 -1
- package/dist/browser/operationHelpers.js +8 -4
- package/dist/browser/operationHelpers.js.map +1 -1
- package/dist/browser/serializationPolicy.js +1 -1
- package/dist/browser/serializationPolicy.js.map +1 -1
- package/dist/browser/serializer.js +32 -11
- package/dist/browser/serializer.js.map +1 -1
- package/dist/browser/serviceClient.js +2 -2
- package/dist/browser/serviceClient.js.map +1 -1
- package/dist/browser/{state.d.ts → state-web.d.mts} +1 -1
- package/dist/browser/{state.js → state-web.mjs} +1 -1
- package/dist/browser/state-web.mjs.map +1 -0
- package/dist/browser/urlHelpers.js +2 -1
- package/dist/browser/urlHelpers.js.map +1 -1
- package/dist/commonjs/authorizeRequestOnTenantChallenge.js +1 -2
- package/dist/commonjs/authorizeRequestOnTenantChallenge.js.map +1 -1
- package/dist/commonjs/base64.d.ts +1 -1
- package/dist/commonjs/base64.js +6 -6
- package/dist/commonjs/base64.js.map +1 -1
- package/dist/commonjs/interfaceHelpers.js +1 -2
- package/dist/commonjs/interfaceHelpers.js.map +1 -1
- package/dist/commonjs/operationHelpers.js +10 -6
- package/dist/commonjs/operationHelpers.js.map +1 -1
- package/dist/commonjs/serializationPolicy.js +1 -1
- package/dist/commonjs/serializationPolicy.js.map +1 -1
- package/dist/commonjs/serializer.js +32 -11
- package/dist/commonjs/serializer.js.map +1 -1
- package/dist/commonjs/serviceClient.js +2 -2
- package/dist/commonjs/serviceClient.js.map +1 -1
- package/dist/commonjs/{state.d.ts → state-cjs.d.ts} +1 -1
- package/dist/commonjs/{state.js → state-cjs.js} +1 -1
- package/dist/commonjs/state-cjs.js.map +1 -0
- package/dist/commonjs/tsdoc-metadata.json +1 -1
- package/dist/commonjs/urlHelpers.js +2 -1
- package/dist/commonjs/urlHelpers.js.map +1 -1
- package/dist/esm/authorizeRequestOnClaimChallenge.js +67 -44
- package/dist/esm/authorizeRequestOnClaimChallenge.js.map +1 -7
- package/dist/esm/authorizeRequestOnTenantChallenge.js +97 -72
- package/dist/esm/authorizeRequestOnTenantChallenge.js.map +1 -7
- package/dist/esm/base64.d.ts +1 -1
- package/dist/esm/base64.js +32 -15
- package/dist/esm/base64.js.map +1 -7
- package/dist/esm/deserializationPolicy.js +202 -205
- package/dist/esm/deserializationPolicy.js.map +1 -7
- package/dist/esm/httpClientCache.js +8 -8
- package/dist/esm/httpClientCache.js.map +1 -7
- package/dist/esm/index.js +11 -31
- package/dist/esm/index.js.map +1 -7
- package/dist/esm/interfaceHelpers.js +34 -23
- package/dist/esm/interfaceHelpers.js.map +1 -7
- package/dist/esm/interfaces.js +11 -6
- package/dist/esm/interfaces.js.map +1 -7
- package/dist/esm/log.js +4 -4
- package/dist/esm/log.js.map +1 -7
- package/dist/esm/operationHelpers.js +86 -73
- package/dist/esm/operationHelpers.js.map +1 -7
- package/dist/esm/pipeline.js +23 -22
- package/dist/esm/pipeline.js.map +1 -7
- package/dist/esm/serializationPolicy.js +134 -163
- package/dist/esm/serializationPolicy.js.map +1 -7
- package/dist/esm/serializer.js +877 -812
- package/dist/esm/serializer.js.map +1 -7
- package/dist/esm/serviceClient.js +150 -151
- package/dist/esm/serviceClient.js.map +1 -7
- package/dist/esm/state-cjs.d.ts +8 -0
- package/dist/esm/state-cjs.js +9 -0
- package/dist/esm/state-cjs.js.map +1 -0
- package/dist/esm/state.js +10 -5
- package/dist/esm/state.js.map +1 -7
- package/dist/esm/urlHelpers.js +207 -199
- package/dist/esm/urlHelpers.js.map +1 -7
- package/dist/esm/utils.js +118 -64
- package/dist/esm/utils.js.map +1 -7
- package/dist/react-native/authorizeRequestOnTenantChallenge.js +1 -2
- package/dist/react-native/authorizeRequestOnTenantChallenge.js.map +1 -1
- package/dist/react-native/base64.d.ts +1 -1
- package/dist/react-native/base64.js +6 -6
- package/dist/react-native/base64.js.map +1 -1
- package/dist/react-native/interfaceHelpers.js +1 -2
- package/dist/react-native/interfaceHelpers.js.map +1 -1
- package/dist/react-native/operationHelpers.js +8 -4
- package/dist/react-native/operationHelpers.js.map +1 -1
- package/dist/react-native/serializationPolicy.js +1 -1
- package/dist/react-native/serializationPolicy.js.map +1 -1
- package/dist/react-native/serializer.js +32 -11
- package/dist/react-native/serializer.js.map +1 -1
- package/dist/react-native/serviceClient.js +2 -2
- package/dist/react-native/serviceClient.js.map +1 -1
- package/dist/react-native/state-web.d.mts +8 -0
- package/dist/react-native/state-web.mjs +9 -0
- package/dist/react-native/state-web.mjs.map +1 -0
- package/dist/react-native/urlHelpers.js +2 -1
- package/dist/react-native/urlHelpers.js.map +1 -1
- package/package.json +31 -17
- package/dist/browser/state.js.map +0 -1
- package/dist/commonjs/state.js.map +0 -1
- package/dist/react-native/state.d.ts +0 -8
- package/dist/react-native/state.js +0 -10
- package/dist/react-native/state.js.map +0 -1
package/dist/esm/serializer.js
CHANGED
|
@@ -1,877 +1,942 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
1
3
|
import * as base64 from "./base64.js";
|
|
2
4
|
import { XML_ATTRKEY, XML_CHARKEY } from "./interfaces.js";
|
|
3
5
|
import { isDuration, isValidUuid } from "./utils.js";
|
|
4
6
|
class SerializerImpl {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
payload
|
|
243
|
-
|
|
244
|
-
mapper,
|
|
245
|
-
responseBody,
|
|
246
|
-
objectName,
|
|
247
|
-
updatedOptions
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
if (mapper.isConstant) {
|
|
252
|
-
payload = mapper.defaultValue;
|
|
253
|
-
}
|
|
254
|
-
return payload;
|
|
255
|
-
}
|
|
7
|
+
modelMappers;
|
|
8
|
+
isXML;
|
|
9
|
+
constructor(modelMappers = {}, isXML = false) {
|
|
10
|
+
this.modelMappers = modelMappers;
|
|
11
|
+
this.isXML = isXML;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated Removing the constraints validation on client side.
|
|
15
|
+
*/
|
|
16
|
+
validateConstraints(mapper, value, objectName) {
|
|
17
|
+
const failValidation = (constraintName, constraintValue) => {
|
|
18
|
+
throw new Error(`"${objectName}" with value "${value}" should satisfy the constraint "${constraintName}": ${constraintValue}.`);
|
|
19
|
+
};
|
|
20
|
+
if (mapper.constraints && value !== undefined && value !== null) {
|
|
21
|
+
const { ExclusiveMaximum, ExclusiveMinimum, InclusiveMaximum, InclusiveMinimum, MaxItems, MaxLength, MinItems, MinLength, MultipleOf, Pattern, UniqueItems, } = mapper.constraints;
|
|
22
|
+
if (ExclusiveMaximum !== undefined && value >= ExclusiveMaximum) {
|
|
23
|
+
failValidation("ExclusiveMaximum", ExclusiveMaximum);
|
|
24
|
+
}
|
|
25
|
+
if (ExclusiveMinimum !== undefined && value <= ExclusiveMinimum) {
|
|
26
|
+
failValidation("ExclusiveMinimum", ExclusiveMinimum);
|
|
27
|
+
}
|
|
28
|
+
if (InclusiveMaximum !== undefined && value > InclusiveMaximum) {
|
|
29
|
+
failValidation("InclusiveMaximum", InclusiveMaximum);
|
|
30
|
+
}
|
|
31
|
+
if (InclusiveMinimum !== undefined && value < InclusiveMinimum) {
|
|
32
|
+
failValidation("InclusiveMinimum", InclusiveMinimum);
|
|
33
|
+
}
|
|
34
|
+
if (MaxItems !== undefined && value.length > MaxItems) {
|
|
35
|
+
failValidation("MaxItems", MaxItems);
|
|
36
|
+
}
|
|
37
|
+
if (MaxLength !== undefined && value.length > MaxLength) {
|
|
38
|
+
failValidation("MaxLength", MaxLength);
|
|
39
|
+
}
|
|
40
|
+
if (MinItems !== undefined && value.length < MinItems) {
|
|
41
|
+
failValidation("MinItems", MinItems);
|
|
42
|
+
}
|
|
43
|
+
if (MinLength !== undefined && value.length < MinLength) {
|
|
44
|
+
failValidation("MinLength", MinLength);
|
|
45
|
+
}
|
|
46
|
+
if (MultipleOf !== undefined && value % MultipleOf !== 0) {
|
|
47
|
+
failValidation("MultipleOf", MultipleOf);
|
|
48
|
+
}
|
|
49
|
+
if (Pattern) {
|
|
50
|
+
const pattern = typeof Pattern === "string" ? new RegExp(Pattern) : Pattern;
|
|
51
|
+
if (typeof value !== "string" || value.match(pattern) === null) {
|
|
52
|
+
failValidation("Pattern", Pattern);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (UniqueItems &&
|
|
56
|
+
value.some((item, i, ar) => ar.indexOf(item) !== i)) {
|
|
57
|
+
failValidation("UniqueItems", UniqueItems);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Serialize the given object based on its metadata defined in the mapper
|
|
63
|
+
*
|
|
64
|
+
* @param mapper - The mapper which defines the metadata of the serializable object
|
|
65
|
+
*
|
|
66
|
+
* @param object - A valid Javascript object to be serialized
|
|
67
|
+
*
|
|
68
|
+
* @param objectName - Name of the serialized object
|
|
69
|
+
*
|
|
70
|
+
* @param options - additional options to serialization
|
|
71
|
+
*
|
|
72
|
+
* @returns A valid serialized Javascript object
|
|
73
|
+
*/
|
|
74
|
+
serialize(mapper, object, objectName, options = { xml: {} }) {
|
|
75
|
+
const updatedOptions = {
|
|
76
|
+
xml: {
|
|
77
|
+
rootName: options.xml.rootName ?? "",
|
|
78
|
+
includeRoot: options.xml.includeRoot ?? false,
|
|
79
|
+
xmlCharKey: options.xml.xmlCharKey ?? XML_CHARKEY,
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
let payload = {};
|
|
83
|
+
const mapperType = mapper.type.name;
|
|
84
|
+
if (!objectName) {
|
|
85
|
+
objectName = mapper.serializedName;
|
|
86
|
+
}
|
|
87
|
+
if (mapperType.match(/^Sequence$/i) !== null) {
|
|
88
|
+
payload = [];
|
|
89
|
+
}
|
|
90
|
+
if (mapper.isConstant) {
|
|
91
|
+
object = mapper.defaultValue;
|
|
92
|
+
}
|
|
93
|
+
// This table of allowed values should help explain
|
|
94
|
+
// the mapper.required and mapper.nullable properties.
|
|
95
|
+
// X means "neither undefined or null are allowed".
|
|
96
|
+
// || required
|
|
97
|
+
// || true | false
|
|
98
|
+
// nullable || ==========================
|
|
99
|
+
// true || null | undefined/null
|
|
100
|
+
// false || X | undefined
|
|
101
|
+
// undefined || X | undefined/null
|
|
102
|
+
const { required, nullable } = mapper;
|
|
103
|
+
if (required && nullable && object === undefined) {
|
|
104
|
+
throw new Error(`${objectName} cannot be undefined.`);
|
|
105
|
+
}
|
|
106
|
+
if (required && !nullable && (object === undefined || object === null)) {
|
|
107
|
+
throw new Error(`${objectName} cannot be null or undefined.`);
|
|
108
|
+
}
|
|
109
|
+
if (!required && nullable === false && object === null) {
|
|
110
|
+
throw new Error(`${objectName} cannot be null.`);
|
|
111
|
+
}
|
|
112
|
+
if (object === undefined || object === null) {
|
|
113
|
+
payload = object;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
if (mapperType.match(/^any$/i) !== null) {
|
|
117
|
+
payload = object;
|
|
118
|
+
}
|
|
119
|
+
else if (mapperType.match(/^(Number|String|Boolean|Object|Stream|Uuid)$/i) !== null) {
|
|
120
|
+
payload = serializeBasicTypes(mapperType, objectName, object);
|
|
121
|
+
}
|
|
122
|
+
else if (mapperType.match(/^Enum$/i) !== null) {
|
|
123
|
+
const enumMapper = mapper;
|
|
124
|
+
payload = serializeEnumType(objectName, enumMapper.type.allowedValues, object);
|
|
125
|
+
}
|
|
126
|
+
else if (mapperType.match(/^(Date|DateTime|TimeSpan|DateTimeRfc1123|UnixTime)$/i) !== null) {
|
|
127
|
+
payload = serializeDateTypes(mapperType, object, objectName);
|
|
128
|
+
}
|
|
129
|
+
else if (mapperType.match(/^ByteArray$/i) !== null) {
|
|
130
|
+
payload = serializeByteArrayType(objectName, object);
|
|
131
|
+
}
|
|
132
|
+
else if (mapperType.match(/^Base64Url$/i) !== null) {
|
|
133
|
+
payload = serializeBase64UrlType(objectName, object);
|
|
134
|
+
}
|
|
135
|
+
else if (mapperType.match(/^Sequence$/i) !== null) {
|
|
136
|
+
payload = serializeSequenceType(this, mapper, object, objectName, Boolean(this.isXML), updatedOptions);
|
|
137
|
+
}
|
|
138
|
+
else if (mapperType.match(/^Dictionary$/i) !== null) {
|
|
139
|
+
payload = serializeDictionaryType(this, mapper, object, objectName, Boolean(this.isXML), updatedOptions);
|
|
140
|
+
}
|
|
141
|
+
else if (mapperType.match(/^Composite$/i) !== null) {
|
|
142
|
+
payload = serializeCompositeType(this, mapper, object, objectName, Boolean(this.isXML), updatedOptions);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return payload;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Deserialize the given object based on its metadata defined in the mapper
|
|
149
|
+
*
|
|
150
|
+
* @param mapper - The mapper which defines the metadata of the serializable object
|
|
151
|
+
*
|
|
152
|
+
* @param responseBody - A valid Javascript entity to be deserialized
|
|
153
|
+
*
|
|
154
|
+
* @param objectName - Name of the deserialized object
|
|
155
|
+
*
|
|
156
|
+
* @param options - Controls behavior of XML parser and builder.
|
|
157
|
+
*
|
|
158
|
+
* @returns A valid deserialized Javascript object
|
|
159
|
+
*/
|
|
160
|
+
deserialize(mapper, responseBody, objectName, options = { xml: {} }) {
|
|
161
|
+
const updatedOptions = {
|
|
162
|
+
xml: {
|
|
163
|
+
rootName: options.xml.rootName ?? "",
|
|
164
|
+
includeRoot: options.xml.includeRoot ?? false,
|
|
165
|
+
xmlCharKey: options.xml.xmlCharKey ?? XML_CHARKEY,
|
|
166
|
+
},
|
|
167
|
+
ignoreUnknownProperties: options.ignoreUnknownProperties ?? false,
|
|
168
|
+
};
|
|
169
|
+
if (responseBody === undefined || responseBody === null) {
|
|
170
|
+
if (this.isXML && mapper.type.name === "Sequence" && !mapper.xmlIsWrapped) {
|
|
171
|
+
// Edge case for empty XML non-wrapped lists. xml2js can't distinguish
|
|
172
|
+
// between the list being empty versus being missing,
|
|
173
|
+
// so let's do the more user-friendly thing and return an empty list.
|
|
174
|
+
responseBody = [];
|
|
175
|
+
}
|
|
176
|
+
// specifically check for undefined as default value can be a falsey value `0, "", false, null`
|
|
177
|
+
if (mapper.defaultValue !== undefined) {
|
|
178
|
+
responseBody = mapper.defaultValue;
|
|
179
|
+
}
|
|
180
|
+
return responseBody;
|
|
181
|
+
}
|
|
182
|
+
let payload;
|
|
183
|
+
const mapperType = mapper.type.name;
|
|
184
|
+
if (!objectName) {
|
|
185
|
+
objectName = mapper.serializedName;
|
|
186
|
+
}
|
|
187
|
+
if (mapperType.match(/^Composite$/i) !== null) {
|
|
188
|
+
payload = deserializeCompositeType(this, mapper, responseBody, objectName, updatedOptions);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
if (this.isXML) {
|
|
192
|
+
const xmlCharKey = updatedOptions.xml.xmlCharKey;
|
|
193
|
+
/**
|
|
194
|
+
* If the mapper specifies this as a non-composite type value but the responseBody contains
|
|
195
|
+
* both header ("$" i.e., XML_ATTRKEY) and body ("#" i.e., XML_CHARKEY) properties,
|
|
196
|
+
* then just reduce the responseBody value to the body ("#" i.e., XML_CHARKEY) property.
|
|
197
|
+
*/
|
|
198
|
+
if (responseBody[XML_ATTRKEY] !== undefined && responseBody[xmlCharKey] !== undefined) {
|
|
199
|
+
responseBody = responseBody[xmlCharKey];
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (mapperType.match(/^Number$/i) !== null) {
|
|
203
|
+
payload = parseFloat(responseBody);
|
|
204
|
+
if (isNaN(payload)) {
|
|
205
|
+
payload = responseBody;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
else if (mapperType.match(/^Boolean$/i) !== null) {
|
|
209
|
+
if (responseBody === "true") {
|
|
210
|
+
payload = true;
|
|
211
|
+
}
|
|
212
|
+
else if (responseBody === "false") {
|
|
213
|
+
payload = false;
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
payload = responseBody;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
else if (mapperType.match(/^(String|Enum|Object|Stream|Uuid|TimeSpan|any)$/i) !== null) {
|
|
220
|
+
payload = responseBody;
|
|
221
|
+
}
|
|
222
|
+
else if (mapperType.match(/^(Date|DateTime|DateTimeRfc1123)$/i) !== null) {
|
|
223
|
+
payload = new Date(responseBody);
|
|
224
|
+
}
|
|
225
|
+
else if (mapperType.match(/^UnixTime$/i) !== null) {
|
|
226
|
+
payload = unixTimeToDate(responseBody);
|
|
227
|
+
}
|
|
228
|
+
else if (mapperType.match(/^ByteArray$/i) !== null) {
|
|
229
|
+
payload = base64.decodeString(responseBody);
|
|
230
|
+
}
|
|
231
|
+
else if (mapperType.match(/^Base64Url$/i) !== null) {
|
|
232
|
+
payload = base64UrlToByteArray(responseBody);
|
|
233
|
+
}
|
|
234
|
+
else if (mapperType.match(/^Sequence$/i) !== null) {
|
|
235
|
+
payload = deserializeSequenceType(this, mapper, responseBody, objectName, updatedOptions);
|
|
236
|
+
}
|
|
237
|
+
else if (mapperType.match(/^Dictionary$/i) !== null) {
|
|
238
|
+
payload = deserializeDictionaryType(this, mapper, responseBody, objectName, updatedOptions);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (mapper.isConstant) {
|
|
242
|
+
payload = mapper.defaultValue;
|
|
243
|
+
}
|
|
244
|
+
return payload;
|
|
245
|
+
}
|
|
256
246
|
}
|
|
257
|
-
|
|
258
|
-
|
|
247
|
+
/**
|
|
248
|
+
* Method that creates and returns a Serializer.
|
|
249
|
+
* @param modelMappers - Known models to map
|
|
250
|
+
* @param isXML - If XML should be supported
|
|
251
|
+
*/
|
|
252
|
+
export function createSerializer(modelMappers = {}, isXML = false) {
|
|
253
|
+
return new SerializerImpl(modelMappers, isXML);
|
|
259
254
|
}
|
|
260
255
|
function trimEnd(str, ch) {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
256
|
+
let len = str.length;
|
|
257
|
+
while (len - 1 >= 0 && str[len - 1] === ch) {
|
|
258
|
+
--len;
|
|
259
|
+
}
|
|
260
|
+
return str.substr(0, len);
|
|
266
261
|
}
|
|
267
262
|
function bufferToBase64Url(buffer) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
263
|
+
if (!buffer) {
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
if (!(buffer instanceof Uint8Array)) {
|
|
267
|
+
throw new Error(`Please provide an input of type Uint8Array for converting to Base64Url.`);
|
|
268
|
+
}
|
|
269
|
+
// Uint8Array to Base64.
|
|
270
|
+
const str = base64.encodeByteArray(buffer);
|
|
271
|
+
// Base64 to Base64Url.
|
|
272
|
+
return trimEnd(str, "=").replace(/\+/g, "-").replace(/\//g, "_");
|
|
276
273
|
}
|
|
277
274
|
function base64UrlToByteArray(str) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
275
|
+
if (!str) {
|
|
276
|
+
return undefined;
|
|
277
|
+
}
|
|
278
|
+
if (str && typeof str.valueOf() !== "string") {
|
|
279
|
+
throw new Error("Please provide an input of type string for converting to Uint8Array");
|
|
280
|
+
}
|
|
281
|
+
// Base64Url to Base64.
|
|
282
|
+
str = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
283
|
+
// Base64 to Uint8Array.
|
|
284
|
+
return base64.decodeString(str);
|
|
286
285
|
}
|
|
287
286
|
function splitSerializeName(prop) {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
287
|
+
const classes = [];
|
|
288
|
+
let partialclass = "";
|
|
289
|
+
if (prop) {
|
|
290
|
+
const subwords = prop.split(".");
|
|
291
|
+
for (const item of subwords) {
|
|
292
|
+
if (item.charAt(item.length - 1) === "\\") {
|
|
293
|
+
partialclass += item.substr(0, item.length - 1) + ".";
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
partialclass += item;
|
|
297
|
+
classes.push(partialclass);
|
|
298
|
+
partialclass = "";
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return classes;
|
|
303
303
|
}
|
|
304
304
|
function dateToUnixTime(d) {
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
305
|
+
if (!d) {
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
if (typeof d.valueOf() === "string") {
|
|
309
|
+
d = new Date(d);
|
|
310
|
+
}
|
|
311
|
+
return Math.floor(d.getTime() / 1000);
|
|
312
312
|
}
|
|
313
313
|
function unixTimeToDate(n) {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
314
|
+
if (!n) {
|
|
315
|
+
return undefined;
|
|
316
|
+
}
|
|
317
|
+
return new Date(n * 1000);
|
|
318
318
|
}
|
|
319
319
|
function serializeBasicTypes(typeName, objectName, value) {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
320
|
+
if (value !== null && value !== undefined) {
|
|
321
|
+
if (typeName.match(/^Number$/i) !== null) {
|
|
322
|
+
if (typeof value !== "number") {
|
|
323
|
+
throw new Error(`${objectName} with value ${value} must be of type number.`);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
else if (typeName.match(/^String$/i) !== null) {
|
|
327
|
+
if (typeof value.valueOf() !== "string") {
|
|
328
|
+
throw new Error(`${objectName} with value "${value}" must be of type string.`);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
else if (typeName.match(/^Uuid$/i) !== null) {
|
|
332
|
+
if (!(typeof value.valueOf() === "string" && isValidUuid(value))) {
|
|
333
|
+
throw new Error(`${objectName} with value "${value}" must be of type string and a valid uuid.`);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
else if (typeName.match(/^Boolean$/i) !== null) {
|
|
337
|
+
if (typeof value !== "boolean") {
|
|
338
|
+
throw new Error(`${objectName} with value ${value} must be of type boolean.`);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
else if (typeName.match(/^Stream$/i) !== null) {
|
|
342
|
+
const objectType = typeof value;
|
|
343
|
+
if (objectType !== "string" &&
|
|
344
|
+
typeof value.pipe !== "function" && // NodeJS.ReadableStream
|
|
345
|
+
typeof value.tee !== "function" && // browser ReadableStream
|
|
346
|
+
!(value instanceof ArrayBuffer) &&
|
|
347
|
+
!ArrayBuffer.isView(value) &&
|
|
348
|
+
// File objects count as a type of Blob, so we want to use instanceof explicitly
|
|
349
|
+
!((typeof Blob === "function" || typeof Blob === "object") && value instanceof Blob) &&
|
|
350
|
+
objectType !== "function") {
|
|
351
|
+
throw new Error(`${objectName} must be a string, Blob, ArrayBuffer, ArrayBufferView, ReadableStream, or () => ReadableStream.`);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return value;
|
|
352
356
|
}
|
|
353
357
|
function serializeEnumType(objectName, allowedValues, value) {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
)
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
`${value} is not a valid value for ${objectName}. The valid values are: ${JSON.stringify(
|
|
368
|
-
allowedValues
|
|
369
|
-
)}.`
|
|
370
|
-
);
|
|
371
|
-
}
|
|
372
|
-
return value;
|
|
358
|
+
if (!allowedValues) {
|
|
359
|
+
throw new Error(`Please provide a set of allowedValues to validate ${objectName} as an Enum Type.`);
|
|
360
|
+
}
|
|
361
|
+
const isPresent = allowedValues.some((item) => {
|
|
362
|
+
if (typeof item.valueOf() === "string") {
|
|
363
|
+
return item.toLowerCase() === value.toLowerCase();
|
|
364
|
+
}
|
|
365
|
+
return item === value;
|
|
366
|
+
});
|
|
367
|
+
if (!isPresent) {
|
|
368
|
+
throw new Error(`${value} is not a valid value for ${objectName}. The valid values are: ${JSON.stringify(allowedValues)}.`);
|
|
369
|
+
}
|
|
370
|
+
return value;
|
|
373
371
|
}
|
|
374
372
|
function serializeByteArrayType(objectName, value) {
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
373
|
+
if (value !== undefined && value !== null) {
|
|
374
|
+
if (!(value instanceof Uint8Array)) {
|
|
375
|
+
throw new Error(`${objectName} must be of type Uint8Array.`);
|
|
376
|
+
}
|
|
377
|
+
value = base64.encodeByteArray(value);
|
|
378
378
|
}
|
|
379
|
-
|
|
380
|
-
}
|
|
381
|
-
return value;
|
|
379
|
+
return value;
|
|
382
380
|
}
|
|
383
381
|
function serializeBase64UrlType(objectName, value) {
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
382
|
+
if (value !== undefined && value !== null) {
|
|
383
|
+
if (!(value instanceof Uint8Array)) {
|
|
384
|
+
throw new Error(`${objectName} must be of type Uint8Array.`);
|
|
385
|
+
}
|
|
386
|
+
value = bufferToBase64Url(value);
|
|
387
387
|
}
|
|
388
|
-
|
|
389
|
-
}
|
|
390
|
-
return value;
|
|
388
|
+
return value;
|
|
391
389
|
}
|
|
392
390
|
function serializeDateTypes(typeName, value, objectName) {
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
391
|
+
if (value !== undefined && value !== null) {
|
|
392
|
+
if (typeName.match(/^Date$/i) !== null) {
|
|
393
|
+
if (!(value instanceof Date ||
|
|
394
|
+
(typeof value.valueOf() === "string" && !isNaN(Date.parse(value))))) {
|
|
395
|
+
throw new Error(`${objectName} must be an instanceof Date or a string in ISO8601 format.`);
|
|
396
|
+
}
|
|
397
|
+
value =
|
|
398
|
+
value instanceof Date
|
|
399
|
+
? value.toISOString().substring(0, 10)
|
|
400
|
+
: new Date(value).toISOString().substring(0, 10);
|
|
401
|
+
}
|
|
402
|
+
else if (typeName.match(/^DateTime$/i) !== null) {
|
|
403
|
+
if (!(value instanceof Date ||
|
|
404
|
+
(typeof value.valueOf() === "string" && !isNaN(Date.parse(value))))) {
|
|
405
|
+
throw new Error(`${objectName} must be an instanceof Date or a string in ISO8601 format.`);
|
|
406
|
+
}
|
|
407
|
+
value = value instanceof Date ? value.toISOString() : new Date(value).toISOString();
|
|
408
|
+
}
|
|
409
|
+
else if (typeName.match(/^DateTimeRfc1123$/i) !== null) {
|
|
410
|
+
if (!(value instanceof Date ||
|
|
411
|
+
(typeof value.valueOf() === "string" && !isNaN(Date.parse(value))))) {
|
|
412
|
+
throw new Error(`${objectName} must be an instanceof Date or a string in RFC-1123 format.`);
|
|
413
|
+
}
|
|
414
|
+
value = value instanceof Date ? value.toUTCString() : new Date(value).toUTCString();
|
|
415
|
+
}
|
|
416
|
+
else if (typeName.match(/^UnixTime$/i) !== null) {
|
|
417
|
+
if (!(value instanceof Date ||
|
|
418
|
+
(typeof value.valueOf() === "string" && !isNaN(Date.parse(value))))) {
|
|
419
|
+
throw new Error(`${objectName} must be an instanceof Date or a string in RFC-1123/ISO8601 format ` +
|
|
420
|
+
`for it to be serialized in UnixTime/Epoch format.`);
|
|
421
|
+
}
|
|
422
|
+
value = dateToUnixTime(value);
|
|
423
|
+
}
|
|
424
|
+
else if (typeName.match(/^TimeSpan$/i) !== null) {
|
|
425
|
+
if (!isDuration(value)) {
|
|
426
|
+
throw new Error(`${objectName} must be a string in ISO 8601 format. Instead was "${value}".`);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return value;
|
|
425
431
|
}
|
|
426
432
|
function serializeSequenceType(serializer, mapper, object, objectName, isXml, options) {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
const
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
433
|
+
if (!Array.isArray(object)) {
|
|
434
|
+
throw new Error(`${objectName} must be of type Array.`);
|
|
435
|
+
}
|
|
436
|
+
let elementType = mapper.type.element;
|
|
437
|
+
if (!elementType || typeof elementType !== "object") {
|
|
438
|
+
throw new Error(`"element" metadata for an Array must be defined in the ` +
|
|
439
|
+
`mapper and it must be of type "object" in ${objectName}.`);
|
|
440
|
+
}
|
|
441
|
+
// Quirk: Composite mappers referenced by `element` might
|
|
442
|
+
// not have *all* properties declared (like uberParent),
|
|
443
|
+
// so let's try to look up the full definition by name.
|
|
444
|
+
if (elementType.type.name === "Composite" && elementType.type.className) {
|
|
445
|
+
elementType = serializer.modelMappers[elementType.type.className] ?? elementType;
|
|
446
|
+
}
|
|
447
|
+
const tempArray = [];
|
|
448
|
+
for (let i = 0; i < object.length; i++) {
|
|
449
|
+
const serializedValue = serializer.serialize(elementType, object[i], objectName, options);
|
|
450
|
+
if (isXml && elementType.xmlNamespace) {
|
|
451
|
+
const xmlnsKey = elementType.xmlNamespacePrefix
|
|
452
|
+
? `xmlns:${elementType.xmlNamespacePrefix}`
|
|
453
|
+
: "xmlns";
|
|
454
|
+
if (elementType.type.name === "Composite") {
|
|
455
|
+
tempArray[i] = { ...serializedValue };
|
|
456
|
+
tempArray[i][XML_ATTRKEY] = { [xmlnsKey]: elementType.xmlNamespace };
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
tempArray[i] = {};
|
|
460
|
+
tempArray[i][options.xml.xmlCharKey] = serializedValue;
|
|
461
|
+
tempArray[i][XML_ATTRKEY] = { [xmlnsKey]: elementType.xmlNamespace };
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
tempArray[i] = serializedValue;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return tempArray;
|
|
457
469
|
}
|
|
458
470
|
function serializeDictionaryType(serializer, mapper, object, objectName, isXml, options) {
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
471
|
+
if (typeof object !== "object") {
|
|
472
|
+
throw new Error(`${objectName} must be of type object.`);
|
|
473
|
+
}
|
|
474
|
+
const valueType = mapper.type.value;
|
|
475
|
+
if (!valueType || typeof valueType !== "object") {
|
|
476
|
+
throw new Error(`"value" metadata for a Dictionary must be defined in the ` +
|
|
477
|
+
`mapper and it must of type "object" in ${objectName}.`);
|
|
478
|
+
}
|
|
479
|
+
const tempDictionary = {};
|
|
480
|
+
for (const key of Object.keys(object)) {
|
|
481
|
+
const serializedValue = serializer.serialize(valueType, object[key], objectName, options);
|
|
482
|
+
// If the element needs an XML namespace we need to add it within the $ property
|
|
483
|
+
tempDictionary[key] = getXmlObjectValue(valueType, serializedValue, isXml, options);
|
|
484
|
+
}
|
|
485
|
+
// Add the namespace to the root element if needed
|
|
486
|
+
if (isXml && mapper.xmlNamespace) {
|
|
487
|
+
const xmlnsKey = mapper.xmlNamespacePrefix ? `xmlns:${mapper.xmlNamespacePrefix}` : "xmlns";
|
|
488
|
+
const result = tempDictionary;
|
|
489
|
+
result[XML_ATTRKEY] = { [xmlnsKey]: mapper.xmlNamespace };
|
|
490
|
+
return result;
|
|
491
|
+
}
|
|
492
|
+
return tempDictionary;
|
|
480
493
|
}
|
|
494
|
+
/**
|
|
495
|
+
* Resolves the additionalProperties property from a referenced mapper
|
|
496
|
+
* @param serializer - the serializer containing the entire set of mappers
|
|
497
|
+
* @param mapper - the composite mapper to resolve
|
|
498
|
+
* @param objectName - name of the object being serialized
|
|
499
|
+
*/
|
|
481
500
|
function resolveAdditionalProperties(serializer, mapper, objectName) {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
501
|
+
const additionalProperties = mapper.type.additionalProperties;
|
|
502
|
+
if (!additionalProperties && mapper.type.className) {
|
|
503
|
+
const modelMapper = resolveReferencedMapper(serializer, mapper, objectName);
|
|
504
|
+
return modelMapper?.type.additionalProperties;
|
|
505
|
+
}
|
|
506
|
+
return additionalProperties;
|
|
488
507
|
}
|
|
508
|
+
/**
|
|
509
|
+
* Finds the mapper referenced by className
|
|
510
|
+
* @param serializer - the serializer containing the entire set of mappers
|
|
511
|
+
* @param mapper - the composite mapper to resolve
|
|
512
|
+
* @param objectName - name of the object being serialized
|
|
513
|
+
*/
|
|
489
514
|
function resolveReferencedMapper(serializer, mapper, objectName) {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
void 0,
|
|
496
|
-
2
|
|
497
|
-
)}".`
|
|
498
|
-
);
|
|
499
|
-
}
|
|
500
|
-
return serializer.modelMappers[className];
|
|
515
|
+
const className = mapper.type.className;
|
|
516
|
+
if (!className) {
|
|
517
|
+
throw new Error(`Class name for model "${objectName}" is not provided in the mapper "${JSON.stringify(mapper, undefined, 2)}".`);
|
|
518
|
+
}
|
|
519
|
+
return serializer.modelMappers[className];
|
|
501
520
|
}
|
|
521
|
+
/**
|
|
522
|
+
* Resolves a composite mapper's modelProperties.
|
|
523
|
+
* @param serializer - the serializer containing the entire set of mappers
|
|
524
|
+
* @param mapper - the composite mapper to resolve
|
|
525
|
+
*/
|
|
502
526
|
function resolveModelProperties(serializer, mapper, objectName) {
|
|
503
|
-
|
|
504
|
-
if (!modelProps) {
|
|
505
|
-
const modelMapper = resolveReferencedMapper(serializer, mapper, objectName);
|
|
506
|
-
if (!modelMapper) {
|
|
507
|
-
throw new Error(`mapper() cannot be null or undefined for model "${mapper.type.className}".`);
|
|
508
|
-
}
|
|
509
|
-
modelProps = modelMapper?.type.modelProperties;
|
|
527
|
+
let modelProps = mapper.type.modelProperties;
|
|
510
528
|
if (!modelProps) {
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
529
|
+
const modelMapper = resolveReferencedMapper(serializer, mapper, objectName);
|
|
530
|
+
if (!modelMapper) {
|
|
531
|
+
throw new Error(`mapper() cannot be null or undefined for model "${mapper.type.className}".`);
|
|
532
|
+
}
|
|
533
|
+
modelProps = modelMapper?.type.modelProperties;
|
|
534
|
+
if (!modelProps) {
|
|
535
|
+
throw new Error(`modelProperties cannot be null or undefined in the ` +
|
|
536
|
+
`mapper "${JSON.stringify(modelMapper)}" of type "${mapper.type.className}" for object "${objectName}".`);
|
|
537
|
+
}
|
|
514
538
|
}
|
|
515
|
-
|
|
516
|
-
return modelProps;
|
|
539
|
+
return modelProps;
|
|
517
540
|
}
|
|
518
541
|
function serializeCompositeType(serializer, mapper, object, objectName, isXml, options) {
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
542
|
+
if (getPolymorphicDiscriminatorRecursively(serializer, mapper)) {
|
|
543
|
+
mapper = getPolymorphicMapper(serializer, mapper, object, "clientName");
|
|
544
|
+
}
|
|
545
|
+
if (object !== undefined && object !== null) {
|
|
546
|
+
const payload = {};
|
|
547
|
+
const modelProps = resolveModelProperties(serializer, mapper, objectName);
|
|
548
|
+
for (const key of Object.keys(modelProps)) {
|
|
549
|
+
const propertyMapper = modelProps[key];
|
|
550
|
+
if (propertyMapper.readOnly) {
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
let propName;
|
|
554
|
+
let parentObject = payload;
|
|
555
|
+
if (serializer.isXML) {
|
|
556
|
+
if (propertyMapper.xmlIsWrapped) {
|
|
557
|
+
propName = propertyMapper.xmlName;
|
|
558
|
+
}
|
|
559
|
+
else {
|
|
560
|
+
propName = propertyMapper.xmlElementName || propertyMapper.xmlName;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
else {
|
|
564
|
+
const paths = splitSerializeName(propertyMapper.serializedName);
|
|
565
|
+
propName = paths.pop();
|
|
566
|
+
for (const pathName of paths) {
|
|
567
|
+
const childObject = parentObject[pathName];
|
|
568
|
+
if ((childObject === undefined || childObject === null) &&
|
|
569
|
+
((object[key] !== undefined && object[key] !== null) ||
|
|
570
|
+
propertyMapper.defaultValue !== undefined)) {
|
|
571
|
+
parentObject[pathName] = {};
|
|
572
|
+
}
|
|
573
|
+
parentObject = parentObject[pathName];
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
if (parentObject !== undefined && parentObject !== null) {
|
|
577
|
+
if (isXml && mapper.xmlNamespace) {
|
|
578
|
+
const xmlnsKey = mapper.xmlNamespacePrefix
|
|
579
|
+
? `xmlns:${mapper.xmlNamespacePrefix}`
|
|
580
|
+
: "xmlns";
|
|
581
|
+
parentObject[XML_ATTRKEY] = {
|
|
582
|
+
...parentObject[XML_ATTRKEY],
|
|
583
|
+
[xmlnsKey]: mapper.xmlNamespace,
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
const propertyObjectName = propertyMapper.serializedName !== ""
|
|
587
|
+
? objectName + "." + propertyMapper.serializedName
|
|
588
|
+
: objectName;
|
|
589
|
+
let toSerialize = object[key];
|
|
590
|
+
const polymorphicDiscriminator = getPolymorphicDiscriminatorRecursively(serializer, mapper);
|
|
591
|
+
if (polymorphicDiscriminator &&
|
|
592
|
+
polymorphicDiscriminator.clientName === key &&
|
|
593
|
+
(toSerialize === undefined || toSerialize === null)) {
|
|
594
|
+
toSerialize = mapper.serializedName;
|
|
595
|
+
}
|
|
596
|
+
const serializedValue = serializer.serialize(propertyMapper, toSerialize, propertyObjectName, options);
|
|
597
|
+
if (serializedValue !== undefined && propName !== undefined && propName !== null) {
|
|
598
|
+
const value = getXmlObjectValue(propertyMapper, serializedValue, isXml, options);
|
|
599
|
+
if (isXml && propertyMapper.xmlIsAttribute) {
|
|
600
|
+
// XML_ATTRKEY, i.e., $ is the key attributes are kept under in xml2js.
|
|
601
|
+
// This keeps things simple while preventing name collision
|
|
602
|
+
// with names in user documents.
|
|
603
|
+
parentObject[XML_ATTRKEY] = parentObject[XML_ATTRKEY] || {};
|
|
604
|
+
parentObject[XML_ATTRKEY][propName] = serializedValue;
|
|
605
|
+
}
|
|
606
|
+
else if (isXml && propertyMapper.xmlIsWrapped) {
|
|
607
|
+
parentObject[propName] = { [propertyMapper.xmlElementName]: value };
|
|
608
|
+
}
|
|
609
|
+
else {
|
|
610
|
+
parentObject[propName] = value;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
const additionalPropertiesMapper = resolveAdditionalProperties(serializer, mapper, objectName);
|
|
616
|
+
if (additionalPropertiesMapper) {
|
|
617
|
+
const propNames = Object.keys(modelProps);
|
|
618
|
+
for (const clientPropName of Object.keys(object)) {
|
|
619
|
+
const isAdditionalProperty = propNames.every((pn) => pn !== clientPropName);
|
|
620
|
+
if (isAdditionalProperty) {
|
|
621
|
+
Object.defineProperty(payload, clientPropName, {
|
|
622
|
+
value: serializer.serialize(additionalPropertiesMapper, object[clientPropName], objectName + '["' + clientPropName + '"]', options),
|
|
623
|
+
enumerable: true,
|
|
624
|
+
configurable: true,
|
|
625
|
+
writable: true,
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return payload;
|
|
631
|
+
}
|
|
632
|
+
return object;
|
|
600
633
|
}
|
|
601
634
|
function getXmlObjectValue(propertyMapper, serializedValue, isXml, options) {
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
635
|
+
if (!isXml || !propertyMapper.xmlNamespace) {
|
|
636
|
+
return serializedValue;
|
|
637
|
+
}
|
|
638
|
+
const xmlnsKey = propertyMapper.xmlNamespacePrefix
|
|
639
|
+
? `xmlns:${propertyMapper.xmlNamespacePrefix}`
|
|
640
|
+
: "xmlns";
|
|
641
|
+
const xmlNamespace = { [xmlnsKey]: propertyMapper.xmlNamespace };
|
|
642
|
+
if (["Composite"].includes(propertyMapper.type.name)) {
|
|
643
|
+
if (serializedValue[XML_ATTRKEY]) {
|
|
644
|
+
return serializedValue;
|
|
645
|
+
}
|
|
646
|
+
else {
|
|
647
|
+
const result = { ...serializedValue };
|
|
648
|
+
result[XML_ATTRKEY] = xmlNamespace;
|
|
649
|
+
return result;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
const result = {};
|
|
653
|
+
result[options.xml.xmlCharKey] = serializedValue;
|
|
654
|
+
result[XML_ATTRKEY] = xmlNamespace;
|
|
655
|
+
return result;
|
|
620
656
|
}
|
|
621
657
|
function isSpecialXmlProperty(propertyName, options) {
|
|
622
|
-
|
|
658
|
+
return [XML_ATTRKEY, options.xml.xmlCharKey].includes(propertyName);
|
|
623
659
|
}
|
|
624
660
|
function deserializeCompositeType(serializer, mapper, responseBody, objectName, options) {
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
)
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
661
|
+
const xmlCharKey = options.xml.xmlCharKey ?? XML_CHARKEY;
|
|
662
|
+
if (getPolymorphicDiscriminatorRecursively(serializer, mapper)) {
|
|
663
|
+
mapper = getPolymorphicMapper(serializer, mapper, responseBody, "serializedName");
|
|
664
|
+
}
|
|
665
|
+
const modelProps = resolveModelProperties(serializer, mapper, objectName);
|
|
666
|
+
let instance = {};
|
|
667
|
+
const handledPropertyNames = [];
|
|
668
|
+
for (const key of Object.keys(modelProps)) {
|
|
669
|
+
const propertyMapper = modelProps[key];
|
|
670
|
+
const paths = splitSerializeName(modelProps[key].serializedName);
|
|
671
|
+
handledPropertyNames.push(paths[0]);
|
|
672
|
+
const { serializedName, xmlName, xmlElementName } = propertyMapper;
|
|
673
|
+
let propertyObjectName = objectName;
|
|
674
|
+
if (serializedName !== "" && serializedName !== undefined) {
|
|
675
|
+
propertyObjectName = objectName + "." + serializedName;
|
|
676
|
+
}
|
|
677
|
+
const headerCollectionPrefix = propertyMapper.headerCollectionPrefix;
|
|
678
|
+
if (headerCollectionPrefix) {
|
|
679
|
+
const dictionary = {};
|
|
680
|
+
for (const headerKey of Object.keys(responseBody)) {
|
|
681
|
+
if (headerKey.startsWith(headerCollectionPrefix)) {
|
|
682
|
+
dictionary[headerKey.substring(headerCollectionPrefix.length)] = serializer.deserialize(propertyMapper.type.value, responseBody[headerKey], propertyObjectName, options);
|
|
683
|
+
}
|
|
684
|
+
handledPropertyNames.push(headerKey);
|
|
685
|
+
}
|
|
686
|
+
instance[key] = dictionary;
|
|
687
|
+
}
|
|
688
|
+
else if (serializer.isXML) {
|
|
689
|
+
if (propertyMapper.xmlIsAttribute && responseBody[XML_ATTRKEY]) {
|
|
690
|
+
instance[key] = serializer.deserialize(propertyMapper, responseBody[XML_ATTRKEY][xmlName], propertyObjectName, options);
|
|
691
|
+
}
|
|
692
|
+
else if (propertyMapper.xmlIsMsText) {
|
|
693
|
+
if (responseBody[xmlCharKey] !== undefined) {
|
|
694
|
+
instance[key] = responseBody[xmlCharKey];
|
|
695
|
+
}
|
|
696
|
+
else if (typeof responseBody === "string") {
|
|
697
|
+
// The special case where xml parser parses "<Name>content</Name>" into JSON of
|
|
698
|
+
// `{ name: "content"}` instead of `{ name: { "_": "content" }}`
|
|
699
|
+
instance[key] = responseBody;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
else {
|
|
703
|
+
const propertyName = xmlElementName || xmlName || serializedName;
|
|
704
|
+
if (propertyMapper.xmlIsWrapped) {
|
|
705
|
+
/* a list of <xmlElementName> wrapped by <xmlName>
|
|
706
|
+
For the xml example below
|
|
707
|
+
<Cors>
|
|
708
|
+
<CorsRule>...</CorsRule>
|
|
709
|
+
<CorsRule>...</CorsRule>
|
|
710
|
+
</Cors>
|
|
711
|
+
the responseBody has
|
|
712
|
+
{
|
|
713
|
+
Cors: {
|
|
714
|
+
CorsRule: [{...}, {...}]
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
xmlName is "Cors" and xmlElementName is"CorsRule".
|
|
718
|
+
*/
|
|
719
|
+
const wrapped = responseBody[xmlName];
|
|
720
|
+
const elementList = wrapped?.[xmlElementName] ?? [];
|
|
721
|
+
Object.defineProperty(instance, key, {
|
|
722
|
+
value: serializer.deserialize(propertyMapper, elementList, propertyObjectName, options),
|
|
723
|
+
enumerable: true,
|
|
724
|
+
configurable: true,
|
|
725
|
+
writable: true,
|
|
726
|
+
});
|
|
727
|
+
handledPropertyNames.push(xmlName);
|
|
728
|
+
}
|
|
729
|
+
else {
|
|
730
|
+
const property = responseBody[propertyName];
|
|
731
|
+
instance[key] = serializer.deserialize(propertyMapper, property, propertyObjectName, options);
|
|
732
|
+
handledPropertyNames.push(propertyName);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
else {
|
|
737
|
+
// deserialize the property if it is present in the provided responseBody instance
|
|
738
|
+
let propertyInstance;
|
|
739
|
+
let res = responseBody;
|
|
740
|
+
// traversing the object step by step.
|
|
741
|
+
let steps = 0;
|
|
742
|
+
for (const item of paths) {
|
|
743
|
+
if (!res)
|
|
744
|
+
break;
|
|
745
|
+
steps++;
|
|
746
|
+
res = res[item];
|
|
747
|
+
}
|
|
748
|
+
// only accept null when reaching the last position of object otherwise it would be undefined
|
|
749
|
+
if (res === null && steps < paths.length) {
|
|
750
|
+
res = undefined;
|
|
751
|
+
}
|
|
752
|
+
propertyInstance = res;
|
|
753
|
+
const polymorphicDiscriminator = mapper.type.polymorphicDiscriminator;
|
|
754
|
+
// checking that the model property name (key)(ex: "fishtype") and the
|
|
755
|
+
// clientName of the polymorphicDiscriminator {metadata} (ex: "fishtype")
|
|
756
|
+
// instead of the serializedName of the polymorphicDiscriminator (ex: "fish.type")
|
|
757
|
+
// is a better approach. The generator is not consistent with escaping '\.' in the
|
|
758
|
+
// serializedName of the property (ex: "fish\.type") that is marked as polymorphic discriminator
|
|
759
|
+
// and the serializedName of the metadata polymorphicDiscriminator (ex: "fish.type"). However,
|
|
760
|
+
// the clientName transformation of the polymorphicDiscriminator (ex: "fishtype") and
|
|
761
|
+
// the transformation of model property name (ex: "fishtype") is done consistently.
|
|
762
|
+
// Hence, it is a safer bet to rely on the clientName of the polymorphicDiscriminator.
|
|
763
|
+
if (polymorphicDiscriminator &&
|
|
764
|
+
key === polymorphicDiscriminator.clientName &&
|
|
765
|
+
(propertyInstance === undefined || propertyInstance === null)) {
|
|
766
|
+
propertyInstance = mapper.serializedName;
|
|
767
|
+
}
|
|
768
|
+
let serializedValue;
|
|
769
|
+
// paging
|
|
770
|
+
if (Array.isArray(responseBody[key]) && modelProps[key].serializedName === "") {
|
|
771
|
+
propertyInstance = responseBody[key];
|
|
772
|
+
const arrayInstance = serializer.deserialize(propertyMapper, propertyInstance, propertyObjectName, options);
|
|
773
|
+
// Copy over any properties that have already been added into the instance, where they do
|
|
774
|
+
// not exist on the newly de-serialized array
|
|
775
|
+
for (const [k, v] of Object.entries(instance)) {
|
|
776
|
+
if (!Object.prototype.hasOwnProperty.call(arrayInstance, k)) {
|
|
777
|
+
arrayInstance[k] = v;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
instance = arrayInstance;
|
|
781
|
+
}
|
|
782
|
+
else if (propertyInstance !== undefined || propertyMapper.defaultValue !== undefined) {
|
|
783
|
+
serializedValue = serializer.deserialize(propertyMapper, propertyInstance, propertyObjectName, options);
|
|
784
|
+
instance[key] = serializedValue;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
const additionalPropertiesMapper = mapper.type.additionalProperties;
|
|
789
|
+
if (additionalPropertiesMapper) {
|
|
790
|
+
const isAdditionalProperty = (responsePropName) => {
|
|
791
|
+
for (const clientPropName of Object.keys(modelProps)) {
|
|
792
|
+
const paths = splitSerializeName(modelProps[clientPropName].serializedName);
|
|
793
|
+
if (paths[0] === responsePropName) {
|
|
794
|
+
return false;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
return true;
|
|
798
|
+
};
|
|
799
|
+
for (const responsePropName of Object.keys(responseBody)) {
|
|
800
|
+
if (isAdditionalProperty(responsePropName)) {
|
|
801
|
+
const deserializedValue = serializer.deserialize(additionalPropertiesMapper, responseBody[responsePropName], objectName + '["' + responsePropName + '"]', options);
|
|
802
|
+
Object.defineProperty(instance, responsePropName, {
|
|
803
|
+
value: deserializedValue,
|
|
804
|
+
enumerable: true,
|
|
805
|
+
configurable: true,
|
|
806
|
+
writable: true,
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
else if (responseBody && !options.ignoreUnknownProperties) {
|
|
812
|
+
for (const key of Object.keys(responseBody)) {
|
|
813
|
+
if (instance[key] === undefined &&
|
|
814
|
+
!handledPropertyNames.includes(key) &&
|
|
815
|
+
!isSpecialXmlProperty(key, options)) {
|
|
816
|
+
Object.defineProperty(instance, key, {
|
|
817
|
+
value: responseBody[key],
|
|
818
|
+
enumerable: true,
|
|
819
|
+
configurable: true,
|
|
820
|
+
writable: true,
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
return instance;
|
|
765
826
|
}
|
|
766
827
|
function deserializeDictionaryType(serializer, mapper, responseBody, objectName, options) {
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
828
|
+
/* jshint validthis: true */
|
|
829
|
+
const value = mapper.type.value;
|
|
830
|
+
if (!value || typeof value !== "object") {
|
|
831
|
+
throw new Error(`"value" metadata for a Dictionary must be defined in the ` +
|
|
832
|
+
`mapper and it must of type "object" in ${objectName}`);
|
|
833
|
+
}
|
|
834
|
+
if (responseBody) {
|
|
835
|
+
const tempDictionary = {};
|
|
836
|
+
for (const key of Object.keys(responseBody)) {
|
|
837
|
+
tempDictionary[key] = serializer.deserialize(value, responseBody[key], objectName, options);
|
|
838
|
+
}
|
|
839
|
+
return tempDictionary;
|
|
777
840
|
}
|
|
778
|
-
return
|
|
779
|
-
}
|
|
780
|
-
return responseBody;
|
|
841
|
+
return responseBody;
|
|
781
842
|
}
|
|
782
843
|
function deserializeSequenceType(serializer, mapper, responseBody, objectName, options) {
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
responseBody
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
844
|
+
let element = mapper.type.element;
|
|
845
|
+
if (!element || typeof element !== "object") {
|
|
846
|
+
throw new Error(`"element" metadata for an Array must be defined in the ` +
|
|
847
|
+
`mapper and it must be of type "object" in ${objectName}`);
|
|
848
|
+
}
|
|
849
|
+
if (responseBody) {
|
|
850
|
+
if (!Array.isArray(responseBody)) {
|
|
851
|
+
// xml2js will interpret a single element array as just the element, so force it to be an array
|
|
852
|
+
responseBody = [responseBody];
|
|
853
|
+
}
|
|
854
|
+
// Quirk: Composite mappers referenced by `element` might
|
|
855
|
+
// not have *all* properties declared (like uberParent),
|
|
856
|
+
// so let's try to look up the full definition by name.
|
|
857
|
+
if (element.type.name === "Composite" && element.type.className) {
|
|
858
|
+
element = serializer.modelMappers[element.type.className] ?? element;
|
|
859
|
+
}
|
|
860
|
+
const tempArray = [];
|
|
861
|
+
for (let i = 0; i < responseBody.length; i++) {
|
|
862
|
+
tempArray[i] = serializer.deserialize(element, responseBody[i], `${objectName}[${i}]`, options);
|
|
863
|
+
}
|
|
864
|
+
return tempArray;
|
|
804
865
|
}
|
|
805
|
-
return
|
|
806
|
-
}
|
|
807
|
-
return responseBody;
|
|
866
|
+
return responseBody;
|
|
808
867
|
}
|
|
809
868
|
function getIndexDiscriminator(discriminators, discriminatorValue, typeName) {
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
869
|
+
const typeNamesToCheck = [typeName];
|
|
870
|
+
while (typeNamesToCheck.length) {
|
|
871
|
+
const currentName = typeNamesToCheck.shift();
|
|
872
|
+
const indexDiscriminator = discriminatorValue === currentName
|
|
873
|
+
? discriminatorValue
|
|
874
|
+
: currentName + "." + discriminatorValue;
|
|
875
|
+
if (Object.prototype.hasOwnProperty.call(discriminators, indexDiscriminator)) {
|
|
876
|
+
return discriminators[indexDiscriminator];
|
|
877
|
+
}
|
|
878
|
+
else {
|
|
879
|
+
for (const [name, mapper] of Object.entries(discriminators)) {
|
|
880
|
+
if (name.startsWith(currentName + ".") &&
|
|
881
|
+
mapper.type.uberParent === currentName &&
|
|
882
|
+
mapper.type.className) {
|
|
883
|
+
typeNamesToCheck.push(mapper.type.className);
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
return undefined;
|
|
825
889
|
}
|
|
826
890
|
function getPolymorphicMapper(serializer, mapper, object, polymorphicPropertyName) {
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
|
-
return mapper;
|
|
891
|
+
const polymorphicDiscriminator = getPolymorphicDiscriminatorRecursively(serializer, mapper);
|
|
892
|
+
if (polymorphicDiscriminator) {
|
|
893
|
+
let discriminatorName = polymorphicDiscriminator[polymorphicPropertyName];
|
|
894
|
+
if (discriminatorName) {
|
|
895
|
+
// The serializedName might have \\, which we just want to ignore
|
|
896
|
+
if (polymorphicPropertyName === "serializedName") {
|
|
897
|
+
discriminatorName = discriminatorName.replace(/\\/gi, "");
|
|
898
|
+
}
|
|
899
|
+
const discriminatorValue = object[discriminatorName];
|
|
900
|
+
const typeName = mapper.type.uberParent ?? mapper.type.className;
|
|
901
|
+
if (typeof discriminatorValue === "string" && typeName) {
|
|
902
|
+
const polymorphicMapper = getIndexDiscriminator(serializer.modelMappers.discriminators, discriminatorValue, typeName);
|
|
903
|
+
if (polymorphicMapper) {
|
|
904
|
+
mapper = polymorphicMapper;
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
return mapper;
|
|
849
910
|
}
|
|
850
911
|
function getPolymorphicDiscriminatorRecursively(serializer, mapper) {
|
|
851
|
-
|
|
912
|
+
return (mapper.type.polymorphicDiscriminator ||
|
|
913
|
+
getPolymorphicDiscriminatorSafely(serializer, mapper.type.uberParent) ||
|
|
914
|
+
getPolymorphicDiscriminatorSafely(serializer, mapper.type.className));
|
|
852
915
|
}
|
|
853
916
|
function getPolymorphicDiscriminatorSafely(serializer, typeName) {
|
|
854
|
-
|
|
917
|
+
return (typeName &&
|
|
918
|
+
serializer.modelMappers[typeName] &&
|
|
919
|
+
serializer.modelMappers[typeName].type.polymorphicDiscriminator);
|
|
855
920
|
}
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
createSerializer
|
|
921
|
+
/**
|
|
922
|
+
* Known types of Mappers
|
|
923
|
+
*/
|
|
924
|
+
export const MapperTypeNames = {
|
|
925
|
+
Base64Url: "Base64Url",
|
|
926
|
+
Boolean: "Boolean",
|
|
927
|
+
ByteArray: "ByteArray",
|
|
928
|
+
Composite: "Composite",
|
|
929
|
+
Date: "Date",
|
|
930
|
+
DateTime: "DateTime",
|
|
931
|
+
DateTimeRfc1123: "DateTimeRfc1123",
|
|
932
|
+
Dictionary: "Dictionary",
|
|
933
|
+
Enum: "Enum",
|
|
934
|
+
Number: "Number",
|
|
935
|
+
Object: "Object",
|
|
936
|
+
Sequence: "Sequence",
|
|
937
|
+
String: "String",
|
|
938
|
+
Stream: "Stream",
|
|
939
|
+
TimeSpan: "TimeSpan",
|
|
940
|
+
UnixTime: "UnixTime",
|
|
877
941
|
};
|
|
942
|
+
//# sourceMappingURL=serializer.js.map
|