-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathserializer.ts
1051 lines (970 loc) · 33.4 KB
/
serializer.ts
1
2
3
4
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import * as base64 from "./util/base64";
import * as utils from "./util/utils";
export class Serializer {
constructor(
public readonly modelMappers: { [key: string]: any } = {},
public readonly isXML?: boolean
) {}
validateConstraints(mapper: Mapper, value: any, objectName: string): void {
const failValidation = (constraintName: keyof MapperConstraints, constraintValue: any) => {
throw new Error(
`"${objectName}" with value "${value}" should satisfy the constraint "${constraintName}": ${constraintValue}.`
);
};
if (mapper.constraints && value != undefined) {
const {
ExclusiveMaximum,
ExclusiveMinimum,
InclusiveMaximum,
InclusiveMinimum,
MaxItems,
MaxLength,
MinItems,
MinLength,
MultipleOf,
Pattern,
UniqueItems,
} = mapper.constraints;
if (ExclusiveMaximum != undefined && value >= ExclusiveMaximum) {
failValidation("ExclusiveMaximum", ExclusiveMaximum);
}
if (ExclusiveMinimum != undefined && value <= ExclusiveMinimum) {
failValidation("ExclusiveMinimum", ExclusiveMinimum);
}
if (InclusiveMaximum != undefined && value > InclusiveMaximum) {
failValidation("InclusiveMaximum", InclusiveMaximum);
}
if (InclusiveMinimum != undefined && value < InclusiveMinimum) {
failValidation("InclusiveMinimum", InclusiveMinimum);
}
if (MaxItems != undefined && value.length > MaxItems) {
failValidation("MaxItems", MaxItems);
}
if (MaxLength != undefined && value.length > MaxLength) {
failValidation("MaxLength", MaxLength);
}
if (MinItems != undefined && value.length < MinItems) {
failValidation("MinItems", MinItems);
}
if (MinLength != undefined && value.length < MinLength) {
failValidation("MinLength", MinLength);
}
if (MultipleOf != undefined && value % MultipleOf !== 0) {
failValidation("MultipleOf", MultipleOf);
}
if (Pattern) {
const pattern: RegExp = typeof Pattern === "string" ? new RegExp(Pattern) : Pattern;
if (typeof value !== "string" || value.match(pattern) === null) {
failValidation("Pattern", Pattern);
}
}
if (
UniqueItems &&
value.some((item: any, i: number, ar: Array<any>) => ar.indexOf(item) !== i)
) {
failValidation("UniqueItems", UniqueItems);
}
}
}
/**
* Serialize the given object based on its metadata defined in the mapper
*
* @param {Mapper} mapper The mapper which defines the metadata of the serializable object
*
* @param {object|string|Array|number|boolean|Date|stream} object A valid Javascript object to be serialized
*
* @param {string} objectName Name of the serialized object
*
* @returns {object|string|Array|number|boolean|Date|stream} A valid serialized Javascript object
*/
serialize(mapper: Mapper, object: any, objectName?: string): any {
let payload: any = {};
const mapperType = mapper.type.name as string;
if (!objectName) {
objectName = mapper.serializedName!;
}
if (mapperType.match(/^Sequence$/gi) !== null) {
payload = [];
}
if (mapper.isConstant) {
object = mapper.defaultValue;
}
// This table of allowed values should help explain
// the mapper.required and mapper.nullable properties.
// X means "neither undefined or null are allowed".
// || required
// || true | false
// nullable || ==========================
// true || null | undefined/null
// false || X | undefined
// undefined || X | undefined/null
const { required, nullable } = mapper;
if (required && nullable && object === undefined) {
throw new Error(`${objectName} cannot be undefined.`);
}
if (required && !nullable && object == undefined) {
throw new Error(`${objectName} cannot be null or undefined.`);
}
if (!required && nullable === false && object === null) {
throw new Error(`${objectName} cannot be null.`);
}
if (object == undefined) {
payload = object;
} else {
// Validate Constraints if any
this.validateConstraints(mapper, object, objectName);
if (mapperType.match(/^any$/gi) !== null) {
payload = object;
} else if (mapperType.match(/^(Number|String|Boolean|Object|Stream|Uuid)$/gi) !== null) {
payload = serializeBasicTypes(mapperType, objectName, object);
} else if (mapperType.match(/^Enum$/gi) !== null) {
const enumMapper: EnumMapper = mapper as EnumMapper;
payload = serializeEnumType(objectName, enumMapper.type.allowedValues, object);
} else if (
mapperType.match(/^(Date|DateTime|TimeSpan|DateTimeRfc1123|UnixTime)$/gi) !== null
) {
payload = serializeDateTypes(mapperType, object, objectName);
} else if (mapperType.match(/^ByteArray$/gi) !== null) {
payload = serializeByteArrayType(objectName, object);
} else if (mapperType.match(/^Base64Url$/gi) !== null) {
payload = serializeBase64UrlType(objectName, object);
} else if (mapperType.match(/^Sequence$/gi) !== null) {
payload = serializeSequenceType(this, mapper as SequenceMapper, object, objectName);
} else if (mapperType.match(/^Dictionary$/gi) !== null) {
payload = serializeDictionaryType(this, mapper as DictionaryMapper, object, objectName);
} else if (mapperType.match(/^Composite$/gi) !== null) {
payload = serializeCompositeType(this, mapper as CompositeMapper, object, objectName);
}
}
return payload;
}
/**
* Deserialize the given object based on its metadata defined in the mapper
*
* @param {object} mapper The mapper which defines the metadata of the serializable object
*
* @param {object|string|Array|number|boolean|Date|stream} responseBody A valid Javascript entity to be deserialized
*
* @param {string} objectName Name of the deserialized object
*
* @returns {object|string|Array|number|boolean|Date|stream} A valid deserialized Javascript object
*/
deserialize(mapper: Mapper, responseBody: any, objectName: string): any {
if (responseBody == undefined) {
if (this.isXML && mapper.type.name === "Sequence" && !mapper.xmlIsWrapped) {
// Edge case for empty XML non-wrapped lists. xml2js can't distinguish
// between the list being empty versus being missing,
// so let's do the more user-friendly thing and return an empty list.
responseBody = [];
}
// specifically check for undefined as default value can be a falsey value `0, "", false, null`
if (mapper.defaultValue !== undefined) {
responseBody = mapper.defaultValue;
}
return responseBody;
}
let payload: any;
const mapperType = mapper.type.name;
if (!objectName) {
objectName = mapper.serializedName!;
}
if (mapperType.match(/^Composite$/gi) !== null) {
payload = deserializeCompositeType(this, mapper as CompositeMapper, responseBody, objectName);
} else {
if (this.isXML) {
/**
* If the mapper specifies this as a non-composite type value but the responseBody contains
* both header ("$") and body ("_") properties, then just reduce the responseBody value to
* the body ("_") property.
*/
if (responseBody["$"] != undefined && responseBody["_"] != undefined) {
responseBody = responseBody["_"];
}
}
if (mapperType.match(/^Number$/gi) !== null) {
payload = parseFloat(responseBody);
if (isNaN(payload)) {
payload = responseBody;
}
} else if (mapperType.match(/^Boolean$/gi) !== null) {
if (responseBody === "true") {
payload = true;
} else if (responseBody === "false") {
payload = false;
} else {
payload = responseBody;
}
} else if (mapperType.match(/^(String|Enum|Object|Stream|Uuid|TimeSpan|any)$/gi) !== null) {
payload = responseBody;
} else if (mapperType.match(/^(Date|DateTime|DateTimeRfc1123)$/gi) !== null) {
payload = new Date(responseBody);
} else if (mapperType.match(/^UnixTime$/gi) !== null) {
payload = unixTimeToDate(responseBody);
} else if (mapperType.match(/^ByteArray$/gi) !== null) {
payload = base64.decodeString(responseBody);
} else if (mapperType.match(/^Base64Url$/gi) !== null) {
payload = base64UrlToByteArray(responseBody);
} else if (mapperType.match(/^Sequence$/gi) !== null) {
payload = deserializeSequenceType(this, mapper as SequenceMapper, responseBody, objectName);
} else if (mapperType.match(/^Dictionary$/gi) !== null) {
payload = deserializeDictionaryType(
this,
mapper as DictionaryMapper,
responseBody,
objectName
);
}
}
if (mapper.isConstant) {
payload = mapper.defaultValue;
}
return payload;
}
}
function trimEnd(str: string, ch: string) {
let len = str.length;
while (len - 1 >= 0 && str[len - 1] === ch) {
--len;
}
return str.substr(0, len);
}
function bufferToBase64Url(buffer: any): string | undefined {
if (!buffer) {
return undefined;
}
if (!(buffer instanceof Uint8Array)) {
throw new Error(`Please provide an input of type Uint8Array for converting to Base64Url.`);
}
// Uint8Array to Base64.
const str = base64.encodeByteArray(buffer);
// Base64 to Base64Url.
return trimEnd(str, "=").replace(/\+/g, "-").replace(/\//g, "_");
}
function base64UrlToByteArray(str: string): Uint8Array | undefined {
if (!str) {
return undefined;
}
if (str && typeof str.valueOf() !== "string") {
throw new Error("Please provide an input of type string for converting to Uint8Array");
}
// Base64Url to Base64.
str = str.replace(/\-/g, "+").replace(/\_/g, "/");
// Base64 to Uint8Array.
return base64.decodeString(str);
}
function splitSerializeName(prop: string | undefined): string[] {
const classes: string[] = [];
let partialclass = "";
if (prop) {
const subwords = prop.split(".");
for (const item of subwords) {
if (item.charAt(item.length - 1) === "\\") {
partialclass += item.substr(0, item.length - 1) + ".";
} else {
partialclass += item;
classes.push(partialclass);
partialclass = "";
}
}
}
return classes;
}
function dateToUnixTime(d: string | Date): number | undefined {
if (!d) {
return undefined;
}
if (typeof d.valueOf() === "string") {
d = new Date(d as string);
}
return Math.floor((d as Date).getTime() / 1000);
}
function unixTimeToDate(n: number): Date | undefined {
if (!n) {
return undefined;
}
return new Date(n * 1000);
}
function serializeBasicTypes(typeName: string, objectName: string, value: any): any {
if (value !== null && value !== undefined) {
if (typeName.match(/^Number$/gi) !== null) {
if (typeof value !== "number") {
throw new Error(`${objectName} with value ${value} must be of type number.`);
}
} else if (typeName.match(/^String$/gi) !== null) {
if (typeof value.valueOf() !== "string") {
throw new Error(`${objectName} with value "${value}" must be of type string.`);
}
} else if (typeName.match(/^Uuid$/gi) !== null) {
if (!(typeof value.valueOf() === "string" && utils.isValidUuid(value))) {
throw new Error(
`${objectName} with value "${value}" must be of type string and a valid uuid.`
);
}
} else if (typeName.match(/^Boolean$/gi) !== null) {
if (typeof value !== "boolean") {
throw new Error(`${objectName} with value ${value} must be of type boolean.`);
}
} else if (typeName.match(/^Stream$/gi) !== null) {
const objectType = typeof value;
if (
objectType !== "string" &&
objectType !== "function" &&
!(value instanceof ArrayBuffer) &&
!ArrayBuffer.isView(value) &&
!(typeof Blob === "function" && value instanceof Blob)
) {
throw new Error(
`${objectName} must be a string, Blob, ArrayBuffer, ArrayBufferView, or a function returning NodeJS.ReadableStream.`
);
}
}
}
return value;
}
function serializeEnumType(objectName: string, allowedValues: Array<any>, value: any): any {
if (!allowedValues) {
throw new Error(
`Please provide a set of allowedValues to validate ${objectName} as an Enum Type.`
);
}
const isPresent = allowedValues.some((item) => {
if (typeof item.valueOf() === "string") {
return item.toLowerCase() === value.toLowerCase();
}
return item === value;
});
if (!isPresent) {
throw new Error(
`${value} is not a valid value for ${objectName}. The valid values are: ${JSON.stringify(
allowedValues
)}.`
);
}
return value;
}
function serializeByteArrayType(objectName: string, value: any): any {
if (value != undefined) {
if (!(value instanceof Uint8Array)) {
throw new Error(`${objectName} must be of type Uint8Array.`);
}
value = base64.encodeByteArray(value);
}
return value;
}
function serializeBase64UrlType(objectName: string, value: any): any {
if (value != undefined) {
if (!(value instanceof Uint8Array)) {
throw new Error(`${objectName} must be of type Uint8Array.`);
}
value = bufferToBase64Url(value);
}
return value;
}
function serializeDateTypes(typeName: string, value: any, objectName: string) {
if (value != undefined) {
if (typeName.match(/^Date$/gi) !== null) {
if (
!(
value instanceof Date ||
(typeof value.valueOf() === "string" && !isNaN(Date.parse(value)))
)
) {
throw new Error(`${objectName} must be an instanceof Date or a string in ISO8601 format.`);
}
value =
value instanceof Date
? value.toISOString().substring(0, 10)
: new Date(value).toISOString().substring(0, 10);
} else if (typeName.match(/^DateTime$/gi) !== null) {
if (
!(
value instanceof Date ||
(typeof value.valueOf() === "string" && !isNaN(Date.parse(value)))
)
) {
throw new Error(`${objectName} must be an instanceof Date or a string in ISO8601 format.`);
}
value = value instanceof Date ? value.toISOString() : new Date(value).toISOString();
} else if (typeName.match(/^DateTimeRfc1123$/gi) !== null) {
if (
!(
value instanceof Date ||
(typeof value.valueOf() === "string" && !isNaN(Date.parse(value)))
)
) {
throw new Error(`${objectName} must be an instanceof Date or a string in RFC-1123 format.`);
}
value = value instanceof Date ? value.toUTCString() : new Date(value).toUTCString();
} else if (typeName.match(/^UnixTime$/gi) !== null) {
if (
!(
value instanceof Date ||
(typeof value.valueOf() === "string" && !isNaN(Date.parse(value)))
)
) {
throw new Error(
`${objectName} must be an instanceof Date or a string in RFC-1123/ISO8601 format ` +
`for it to be serialized in UnixTime/Epoch format.`
);
}
value = dateToUnixTime(value);
} else if (typeName.match(/^TimeSpan$/gi) !== null) {
if (!utils.isDuration(value)) {
throw new Error(
`${objectName} must be a string in ISO 8601 format. Instead was "${value}".`
);
}
value = value;
}
}
return value;
}
function serializeSequenceType(
serializer: Serializer,
mapper: SequenceMapper,
object: any,
objectName: string
) {
if (!Array.isArray(object)) {
throw new Error(`${objectName} must be of type Array.`);
}
const elementType = mapper.type.element;
if (!elementType || typeof elementType !== "object") {
throw new Error(
`element" metadata for an Array must be defined in the ` +
`mapper and it must of type "object" in ${objectName}.`
);
}
const tempArray = [];
for (let i = 0; i < object.length; i++) {
tempArray[i] = serializer.serialize(elementType, object[i], objectName);
}
return tempArray;
}
function serializeDictionaryType(
serializer: Serializer,
mapper: DictionaryMapper,
object: any,
objectName: string
) {
if (typeof object !== "object") {
throw new Error(`${objectName} must be of type object.`);
}
const valueType = mapper.type.value;
if (!valueType || typeof valueType !== "object") {
throw new Error(
`"value" metadata for a Dictionary must be defined in the ` +
`mapper and it must of type "object" in ${objectName}.`
);
}
const tempDictionary: { [key: string]: any } = {};
for (const key of Object.keys(object)) {
tempDictionary[key] = serializer.serialize(valueType, object[key], objectName + "." + key);
}
return tempDictionary;
}
/**
* Resolves a composite mapper's modelProperties.
* @param serializer the serializer containing the entire set of mappers
* @param mapper the composite mapper to resolve
*/
function resolveModelProperties(
serializer: Serializer,
mapper: CompositeMapper,
objectName: string
): { [propertyName: string]: Mapper } {
let modelProps = mapper.type.modelProperties;
if (!modelProps) {
const className = mapper.type.className;
if (!className) {
throw new Error(
`Class name for model "${objectName}" is not provided in the mapper "${JSON.stringify(
mapper,
undefined,
2
)}".`
);
}
const modelMapper = serializer.modelMappers[className];
if (!modelMapper) {
throw new Error(`mapper() cannot be null or undefined for model "${className}".`);
}
modelProps = modelMapper.type.modelProperties;
if (!modelProps) {
throw new Error(
`modelProperties cannot be null or undefined in the ` +
`mapper "${JSON.stringify(
modelMapper
)}" of type "${className}" for object "${objectName}".`
);
}
}
return modelProps;
}
function serializeCompositeType(
serializer: Serializer,
mapper: CompositeMapper,
object: any,
objectName: string
) {
if (getPolymorphicDiscriminatorRecursively(serializer, mapper)) {
mapper = getPolymorphicMapper(serializer, mapper, object, "clientName");
}
if (object != undefined) {
const payload: any = {};
const modelProps = resolveModelProperties(serializer, mapper, objectName);
for (const key of Object.keys(modelProps)) {
const propertyMapper = modelProps[key];
if (propertyMapper.readOnly) {
continue;
}
let propName: string | undefined;
let parentObject: any = payload;
if (serializer.isXML) {
if (propertyMapper.xmlIsWrapped) {
propName = propertyMapper.xmlName;
} else {
propName = propertyMapper.xmlElementName || propertyMapper.xmlName;
}
} else {
const paths = splitSerializeName(propertyMapper.serializedName!);
propName = paths.pop();
for (const pathName of paths) {
const childObject = parentObject[pathName];
if (childObject == undefined && object[key] != undefined) {
parentObject[pathName] = {};
}
parentObject = parentObject[pathName];
}
}
if (parentObject != undefined) {
const propertyObjectName =
propertyMapper.serializedName !== ""
? objectName + "." + propertyMapper.serializedName
: objectName;
let toSerialize = object[key];
const polymorphicDiscriminator = getPolymorphicDiscriminatorRecursively(serializer, mapper);
if (
polymorphicDiscriminator &&
polymorphicDiscriminator.clientName === key &&
toSerialize == undefined
) {
toSerialize = mapper.serializedName;
}
const serializedValue = serializer.serialize(
propertyMapper,
toSerialize,
propertyObjectName
);
if (serializedValue !== undefined && propName != undefined) {
if (propertyMapper.xmlIsAttribute) {
// $ is the key attributes are kept under in xml2js.
// This keeps things simple while preventing name collision
// with names in user documents.
parentObject.$ = parentObject.$ || {};
parentObject.$[propName] = serializedValue;
} else if (propertyMapper.xmlIsWrapped) {
parentObject[propName] = { [propertyMapper.xmlElementName!]: serializedValue };
} else {
parentObject[propName] = serializedValue;
}
}
}
}
const additionalPropertiesMapper = mapper.type.additionalProperties;
if (additionalPropertiesMapper) {
const propNames = Object.keys(modelProps);
for (const clientPropName in object) {
const isAdditionalProperty = propNames.every((pn) => pn !== clientPropName);
if (isAdditionalProperty) {
payload[clientPropName] = serializer.serialize(
additionalPropertiesMapper,
object[clientPropName],
objectName + '["' + clientPropName + '"]'
);
}
}
}
return payload;
}
return object;
}
function isSpecialXmlProperty(propertyName: string): boolean {
return ["$", "_"].includes(propertyName);
}
function deserializeCompositeType(
serializer: Serializer,
mapper: CompositeMapper,
responseBody: any,
objectName: string
): any {
if (getPolymorphicDiscriminatorRecursively(serializer, mapper)) {
mapper = getPolymorphicMapper(serializer, mapper, responseBody, "serializedName");
}
const modelProps = resolveModelProperties(serializer, mapper, objectName);
let instance: { [key: string]: any } = {};
const handledPropertyNames: string[] = [];
for (const key of Object.keys(modelProps)) {
const propertyMapper = modelProps[key];
const paths = splitSerializeName(modelProps[key].serializedName!);
handledPropertyNames.push(paths[0]);
const { serializedName, xmlName, xmlElementName } = propertyMapper;
let propertyObjectName = objectName;
if (serializedName !== "" && serializedName !== undefined) {
propertyObjectName = objectName + "." + serializedName;
}
const headerCollectionPrefix = (propertyMapper as DictionaryMapper).headerCollectionPrefix;
if (headerCollectionPrefix) {
const dictionary: any = {};
for (const headerKey of Object.keys(responseBody)) {
if (headerKey.startsWith(headerCollectionPrefix)) {
dictionary[headerKey.substring(headerCollectionPrefix.length)] = serializer.deserialize(
(propertyMapper as DictionaryMapper).type.value,
responseBody[headerKey],
propertyObjectName
);
}
handledPropertyNames.push(headerKey);
}
instance[key] = dictionary;
} else if (serializer.isXML) {
if (propertyMapper.xmlIsAttribute && responseBody.$) {
instance[key] = serializer.deserialize(
propertyMapper,
responseBody.$[xmlName!],
propertyObjectName
);
} else {
const propertyName = xmlElementName || xmlName || serializedName;
let unwrappedProperty = responseBody[propertyName!];
if (propertyMapper.xmlIsWrapped) {
unwrappedProperty = responseBody[xmlName!];
unwrappedProperty = unwrappedProperty && unwrappedProperty[xmlElementName!];
const isEmptyWrappedList = unwrappedProperty === undefined;
if (isEmptyWrappedList) {
unwrappedProperty = [];
}
}
instance[key] = serializer.deserialize(
propertyMapper,
unwrappedProperty,
propertyObjectName
);
}
} else {
// deserialize the property if it is present in the provided responseBody instance
let propertyInstance;
let res = responseBody;
// traversing the object step by step.
for (const item of paths) {
if (!res) break;
res = res[item];
}
propertyInstance = res;
const polymorphicDiscriminator = mapper.type.polymorphicDiscriminator;
// checking that the model property name (key)(ex: "fishtype") and the
// clientName of the polymorphicDiscriminator {metadata} (ex: "fishtype")
// instead of the serializedName of the polymorphicDiscriminator (ex: "fish.type")
// is a better approach. The generator is not consistent with escaping '\.' in the
// serializedName of the property (ex: "fish\.type") that is marked as polymorphic discriminator
// and the serializedName of the metadata polymorphicDiscriminator (ex: "fish.type"). However,
// the clientName transformation of the polymorphicDiscriminator (ex: "fishtype") and
// the transformation of model property name (ex: "fishtype") is done consistently.
// Hence, it is a safer bet to rely on the clientName of the polymorphicDiscriminator.
if (
polymorphicDiscriminator &&
key === polymorphicDiscriminator.clientName &&
propertyInstance == undefined
) {
propertyInstance = mapper.serializedName;
}
let serializedValue;
// paging
if (Array.isArray(responseBody[key]) && modelProps[key].serializedName === "") {
propertyInstance = responseBody[key];
instance = serializer.deserialize(propertyMapper, propertyInstance, propertyObjectName);
} else if (propertyInstance !== undefined || propertyMapper.defaultValue !== undefined) {
serializedValue = serializer.deserialize(
propertyMapper,
propertyInstance,
propertyObjectName
);
instance[key] = serializedValue;
}
}
}
const additionalPropertiesMapper = mapper.type.additionalProperties;
if (additionalPropertiesMapper) {
const isAdditionalProperty = (responsePropName: string) => {
for (const clientPropName in modelProps) {
const paths = splitSerializeName(modelProps[clientPropName].serializedName);
if (paths[0] === responsePropName) {
return false;
}
}
return true;
};
for (const responsePropName in responseBody) {
if (isAdditionalProperty(responsePropName)) {
instance[responsePropName] = serializer.deserialize(
additionalPropertiesMapper,
responseBody[responsePropName],
objectName + '["' + responsePropName + '"]'
);
}
}
} else if (responseBody) {
for (const key of Object.keys(responseBody)) {
if (
instance[key] === undefined &&
!handledPropertyNames.includes(key) &&
!isSpecialXmlProperty(key)
) {
instance[key] = responseBody[key];
}
}
}
return instance;
}
function deserializeDictionaryType(
serializer: Serializer,
mapper: DictionaryMapper,
responseBody: any,
objectName: string
): any {
/*jshint validthis: true */
const value = mapper.type.value;
if (!value || typeof value !== "object") {
throw new Error(
`"value" metadata for a Dictionary must be defined in the ` +
`mapper and it must of type "object" in ${objectName}`
);
}
if (responseBody) {
const tempDictionary: { [key: string]: any } = {};
for (const key of Object.keys(responseBody)) {
tempDictionary[key] = serializer.deserialize(value, responseBody[key], objectName);
}
return tempDictionary;
}
return responseBody;
}
function deserializeSequenceType(
serializer: Serializer,
mapper: SequenceMapper,
responseBody: any,
objectName: string
): any {
/*jshint validthis: true */
const element = mapper.type.element;
if (!element || typeof element !== "object") {
throw new Error(
`element" metadata for an Array must be defined in the ` +
`mapper and it must of type "object" in ${objectName}`
);
}
if (responseBody) {
if (!Array.isArray(responseBody)) {
// xml2js will interpret a single element array as just the element, so force it to be an array
responseBody = [responseBody];
}
const tempArray = [];
for (let i = 0; i < responseBody.length; i++) {
tempArray[i] = serializer.deserialize(element, responseBody[i], `${objectName}[${i}]`);
}
return tempArray;
}
return responseBody;
}
function getPolymorphicMapper(
serializer: Serializer,
mapper: CompositeMapper,
object: any,
polymorphicPropertyName: "clientName" | "serializedName"
): CompositeMapper {
const polymorphicDiscriminator = getPolymorphicDiscriminatorRecursively(serializer, mapper);
if (polymorphicDiscriminator) {
const discriminatorName = polymorphicDiscriminator[polymorphicPropertyName];
if (discriminatorName != undefined) {
const discriminatorValue = object[discriminatorName];
if (discriminatorValue != undefined) {
const typeName = mapper.type.uberParent || mapper.type.className;
const indexDiscriminator =
discriminatorValue === typeName
? discriminatorValue
: typeName + "." + discriminatorValue;
const polymorphicMapper = serializer.modelMappers.discriminators[indexDiscriminator];
if (polymorphicMapper) {
mapper = polymorphicMapper;
}
}
}
}
return mapper;
}
function getPolymorphicDiscriminatorRecursively(
serializer: Serializer,
mapper: CompositeMapper
): PolymorphicDiscriminator | undefined {
return (
mapper.type.polymorphicDiscriminator ||
getPolymorphicDiscriminatorSafely(serializer, mapper.type.uberParent) ||
getPolymorphicDiscriminatorSafely(serializer, mapper.type.className)
);
}
function getPolymorphicDiscriminatorSafely(serializer: Serializer, typeName?: string) {
return (
typeName &&
serializer.modelMappers[typeName] &&
serializer.modelMappers[typeName].type.polymorphicDiscriminator
);
}
export interface MapperConstraints {
InclusiveMaximum?: number;
ExclusiveMaximum?: number;
InclusiveMinimum?: number;
ExclusiveMinimum?: number;
MaxLength?: number;
MinLength?: number;
Pattern?: RegExp;
MaxItems?: number;
MinItems?: number;
UniqueItems?: true;
MultipleOf?: number;
}
export type MapperType =
| SimpleMapperType
| CompositeMapperType
| SequenceMapperType
| DictionaryMapperType
| EnumMapperType;
export interface SimpleMapperType {
name:
| "Base64Url"
| "Boolean"
| "ByteArray"
| "Date"
| "DateTime"
| "DateTimeRfc1123"
| "Object"
| "Stream"
| "String"
| "TimeSpan"
| "UnixTime"
| "Uuid"
| "Number"
| "any";
}
export interface CompositeMapperType {
name: "Composite";
// Only one of the two below properties should be present.
// Use className to reference another type definition,
// and use modelProperties/additionalProperties when the reference to the other type has been resolved.
className?: string;
modelProperties?: { [propertyName: string]: Mapper };
additionalProperties?: Mapper;
uberParent?: string;
polymorphicDiscriminator?: PolymorphicDiscriminator;
}
export interface SequenceMapperType {
name: "Sequence";
element: Mapper;
}
export interface DictionaryMapperType {
name: "Dictionary";
value: Mapper;
}
export interface EnumMapperType {
name: "Enum";
allowedValues: any[];
}
export interface BaseMapper {
xmlName?: string;
xmlIsAttribute?: boolean;
xmlElementName?: string;
xmlIsWrapped?: boolean;
readOnly?: boolean;
isConstant?: boolean;
required?: boolean;
nullable?: boolean;
serializedName?: string;
type: MapperType;
defaultValue?: any;
constraints?: MapperConstraints;
}
export type Mapper = BaseMapper | CompositeMapper | SequenceMapper | DictionaryMapper | EnumMapper;
export interface PolymorphicDiscriminator {
serializedName: string;
clientName: string;
[key: string]: string;
}
export interface CompositeMapper extends BaseMapper {
type: CompositeMapperType;
}
export interface SequenceMapper extends BaseMapper {
type: SequenceMapperType;
}
export interface DictionaryMapper extends BaseMapper {
type: DictionaryMapperType;
headerCollectionPrefix?: string;
}
export interface EnumMapper extends BaseMapper {
type: EnumMapperType;
}
export interface UrlParameterValue {
value: string;
skipUrlEncoding: boolean;
}
// TODO: why is this here?
export function serializeObject(toSerialize: any): any {