2017-09-13 19:42:16 +03:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// Licensed under the MIT License. See License.txt in the project root for license information.
|
|
|
|
|
2018-05-10 21:20:38 +03:00
|
|
|
import * as base64 from "./util/base64";
|
2018-07-10 00:26:56 +03:00
|
|
|
import * as utils from "./util/utils";
|
2017-09-13 19:42:16 +03:00
|
|
|
|
|
|
|
export class Serializer {
|
2021-02-02 04:54:34 +03:00
|
|
|
constructor(
|
|
|
|
public readonly modelMappers: { [key: string]: any } = {},
|
|
|
|
public readonly isXML?: boolean
|
|
|
|
) {}
|
2017-09-13 19:42:16 +03:00
|
|
|
|
|
|
|
validateConstraints(mapper: Mapper, value: any, objectName: string): void {
|
2018-06-19 23:09:51 +03:00
|
|
|
const failValidation = (constraintName: keyof MapperConstraints, constraintValue: any) => {
|
2021-02-02 04:54:34 +03:00
|
|
|
throw new Error(
|
|
|
|
`"${objectName}" with value "${value}" should satisfy the constraint "${constraintName}": ${constraintValue}.`
|
|
|
|
);
|
2018-06-19 23:09:51 +03:00
|
|
|
};
|
2021-02-02 04:54:34 +03:00
|
|
|
if (mapper.constraints && value != undefined) {
|
2018-06-19 23:18:48 +03:00
|
|
|
const {
|
|
|
|
ExclusiveMaximum,
|
|
|
|
ExclusiveMinimum,
|
|
|
|
InclusiveMaximum,
|
|
|
|
InclusiveMinimum,
|
|
|
|
MaxItems,
|
|
|
|
MaxLength,
|
|
|
|
MinItems,
|
|
|
|
MinLength,
|
|
|
|
MultipleOf,
|
|
|
|
Pattern,
|
2021-02-02 04:54:34 +03:00
|
|
|
UniqueItems,
|
2018-06-19 23:18:48 +03:00
|
|
|
} = mapper.constraints;
|
|
|
|
if (ExclusiveMaximum != undefined && value >= ExclusiveMaximum) {
|
|
|
|
failValidation("ExclusiveMaximum", ExclusiveMaximum);
|
2018-06-19 23:09:51 +03:00
|
|
|
}
|
2018-06-19 23:18:48 +03:00
|
|
|
if (ExclusiveMinimum != undefined && value <= ExclusiveMinimum) {
|
|
|
|
failValidation("ExclusiveMinimum", ExclusiveMinimum);
|
2018-06-19 23:09:51 +03:00
|
|
|
}
|
2018-06-19 23:18:48 +03:00
|
|
|
if (InclusiveMaximum != undefined && value > InclusiveMaximum) {
|
|
|
|
failValidation("InclusiveMaximum", InclusiveMaximum);
|
2018-06-19 23:09:51 +03:00
|
|
|
}
|
2018-06-19 23:18:48 +03:00
|
|
|
if (InclusiveMinimum != undefined && value < InclusiveMinimum) {
|
|
|
|
failValidation("InclusiveMinimum", InclusiveMinimum);
|
2018-06-19 23:09:51 +03:00
|
|
|
}
|
2018-06-19 23:18:48 +03:00
|
|
|
if (MaxItems != undefined && value.length > MaxItems) {
|
|
|
|
failValidation("MaxItems", MaxItems);
|
2018-06-19 23:09:51 +03:00
|
|
|
}
|
2018-06-19 23:18:48 +03:00
|
|
|
if (MaxLength != undefined && value.length > MaxLength) {
|
|
|
|
failValidation("MaxLength", MaxLength);
|
2018-06-19 23:09:51 +03:00
|
|
|
}
|
2018-06-19 23:18:48 +03:00
|
|
|
if (MinItems != undefined && value.length < MinItems) {
|
|
|
|
failValidation("MinItems", MinItems);
|
2018-06-19 23:09:51 +03:00
|
|
|
}
|
2018-06-19 23:18:48 +03:00
|
|
|
if (MinLength != undefined && value.length < MinLength) {
|
|
|
|
failValidation("MinLength", MinLength);
|
2018-06-19 23:09:51 +03:00
|
|
|
}
|
2018-06-19 23:18:48 +03:00
|
|
|
if (MultipleOf != undefined && value % MultipleOf !== 0) {
|
|
|
|
failValidation("MultipleOf", MultipleOf);
|
2018-06-19 23:09:51 +03:00
|
|
|
}
|
2019-06-26 03:08:42 +03:00
|
|
|
if (Pattern) {
|
2019-06-26 19:18:35 +03:00
|
|
|
const pattern: RegExp = typeof Pattern === "string" ? new RegExp(Pattern) : Pattern;
|
2021-02-02 04:54:34 +03:00
|
|
|
if (typeof value !== "string" || value.match(pattern) === null) {
|
2019-06-26 03:08:42 +03:00
|
|
|
failValidation("Pattern", Pattern);
|
|
|
|
}
|
2018-06-19 23:09:51 +03:00
|
|
|
}
|
2021-02-02 04:54:34 +03:00
|
|
|
if (
|
|
|
|
UniqueItems &&
|
|
|
|
value.some((item: any, i: number, ar: Array<any>) => ar.indexOf(item) !== i)
|
|
|
|
) {
|
2018-06-19 23:18:48 +03:00
|
|
|
failValidation("UniqueItems", UniqueItems);
|
2017-10-17 22:10:36 +03:00
|
|
|
}
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-06-14 23:30:31 +03:00
|
|
|
serialize(mapper: Mapper, object: any, objectName?: string): any {
|
2017-09-13 19:42:16 +03:00
|
|
|
let payload: any = {};
|
|
|
|
const mapperType = mapper.type.name as string;
|
2018-06-14 23:30:31 +03:00
|
|
|
if (!objectName) {
|
2018-07-31 23:08:24 +03:00
|
|
|
objectName = mapper.serializedName!;
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
2021-02-02 04:54:34 +03:00
|
|
|
if (mapperType.match(/^Sequence$/gi) !== null) {
|
2018-06-14 23:30:31 +03:00
|
|
|
payload = [];
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
2018-06-14 23:30:31 +03:00
|
|
|
|
2019-07-11 02:44:40 +03:00
|
|
|
if (mapper.isConstant) {
|
2018-06-28 22:17:31 +03:00
|
|
|
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.`);
|
2018-06-14 23:30:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (object == undefined) {
|
2018-02-23 04:16:50 +03:00
|
|
|
payload = object;
|
2018-06-14 23:30:31 +03:00
|
|
|
} else {
|
|
|
|
// Validate Constraints if any
|
|
|
|
this.validateConstraints(mapper, object, objectName);
|
2021-02-02 04:54:34 +03:00
|
|
|
if (mapperType.match(/^any$/gi) !== null) {
|
2018-06-14 23:30:31 +03:00
|
|
|
payload = object;
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^(Number|String|Boolean|Object|Stream|Uuid)$/gi) !== null) {
|
2018-06-15 00:14:10 +03:00
|
|
|
payload = serializeBasicTypes(mapperType, objectName, object);
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^Enum$/gi) !== null) {
|
2018-06-14 23:30:31 +03:00
|
|
|
const enumMapper: EnumMapper = mapper as EnumMapper;
|
2018-06-15 00:14:10 +03:00
|
|
|
payload = serializeEnumType(objectName, enumMapper.type.allowedValues, object);
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (
|
|
|
|
mapperType.match(/^(Date|DateTime|TimeSpan|DateTimeRfc1123|UnixTime)$/gi) !== null
|
|
|
|
) {
|
2018-06-15 00:14:10 +03:00
|
|
|
payload = serializeDateTypes(mapperType, object, objectName);
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^ByteArray$/gi) !== null) {
|
2018-06-15 00:14:10 +03:00
|
|
|
payload = serializeByteArrayType(objectName, object);
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^Base64Url$/gi) !== null) {
|
2018-06-15 00:14:10 +03:00
|
|
|
payload = serializeBase64UrlType(objectName, object);
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^Sequence$/gi) !== null) {
|
2018-06-15 00:14:10 +03:00
|
|
|
payload = serializeSequenceType(this, mapper as SequenceMapper, object, objectName);
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^Dictionary$/gi) !== null) {
|
2018-06-15 00:14:10 +03:00
|
|
|
payload = serializeDictionaryType(this, mapper as DictionaryMapper, object, objectName);
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^Composite$/gi) !== null) {
|
2018-06-15 00:14:10 +03:00
|
|
|
payload = serializeCompositeType(this, mapper as CompositeMapper, object, objectName);
|
2018-06-14 23:30:31 +03:00
|
|
|
}
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
|
|
|
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 {
|
2018-03-29 21:39:59 +03:00
|
|
|
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 = [];
|
|
|
|
}
|
2019-07-11 06:06:22 +03:00
|
|
|
// specifically check for undefined as default value can be a falsey value `0, "", false, null`
|
|
|
|
if (mapper.defaultValue !== undefined) {
|
2019-07-11 02:44:40 +03:00
|
|
|
responseBody = mapper.defaultValue;
|
|
|
|
}
|
2018-03-27 03:54:26 +03:00
|
|
|
return responseBody;
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:42:16 +03:00
|
|
|
let payload: any;
|
|
|
|
const mapperType = mapper.type.name;
|
2018-03-29 00:25:36 +03:00
|
|
|
if (!objectName) {
|
2018-07-31 23:08:24 +03:00
|
|
|
objectName = mapper.serializedName!;
|
2018-03-29 00:25:36 +03:00
|
|
|
}
|
2017-09-13 19:42:16 +03:00
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
if (mapperType.match(/^Composite$/gi) !== null) {
|
2018-10-17 04:49:18 +03:00
|
|
|
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["_"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
if (mapperType.match(/^Number$/gi) !== null) {
|
2018-10-17 19:30:42 +03:00
|
|
|
payload = parseFloat(responseBody);
|
|
|
|
if (isNaN(payload)) {
|
|
|
|
payload = responseBody;
|
|
|
|
}
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^Boolean$/gi) !== null) {
|
2018-10-17 04:49:18 +03:00
|
|
|
if (responseBody === "true") {
|
|
|
|
payload = true;
|
|
|
|
} else if (responseBody === "false") {
|
|
|
|
payload = false;
|
|
|
|
} else {
|
|
|
|
payload = responseBody;
|
|
|
|
}
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^(String|Enum|Object|Stream|Uuid|TimeSpan|any)$/gi) !== null) {
|
2018-03-29 00:14:56 +03:00
|
|
|
payload = responseBody;
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^(Date|DateTime|DateTimeRfc1123)$/gi) !== null) {
|
2018-10-17 04:49:18 +03:00
|
|
|
payload = new Date(responseBody);
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^UnixTime$/gi) !== null) {
|
2018-10-17 04:49:18 +03:00
|
|
|
payload = unixTimeToDate(responseBody);
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^ByteArray$/gi) !== null) {
|
2018-10-17 04:49:18 +03:00
|
|
|
payload = base64.decodeString(responseBody);
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^Base64Url$/gi) !== null) {
|
2018-10-17 04:49:18 +03:00
|
|
|
payload = base64UrlToByteArray(responseBody);
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^Sequence$/gi) !== null) {
|
2018-10-17 04:49:18 +03:00
|
|
|
payload = deserializeSequenceType(this, mapper as SequenceMapper, responseBody, objectName);
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (mapperType.match(/^Dictionary$/gi) !== null) {
|
|
|
|
payload = deserializeDictionaryType(
|
|
|
|
this,
|
|
|
|
mapper as DictionaryMapper,
|
|
|
|
responseBody,
|
|
|
|
objectName
|
|
|
|
);
|
2018-03-29 00:14:56 +03:00
|
|
|
}
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
|
|
|
|
2018-03-29 00:25:36 +03:00
|
|
|
if (mapper.isConstant) {
|
|
|
|
payload = mapper.defaultValue;
|
|
|
|
}
|
2017-09-13 19:42:16 +03:00
|
|
|
|
|
|
|
return payload;
|
|
|
|
}
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
2017-09-13 19:42:16 +03:00
|
|
|
|
2018-06-15 00:14:10 +03:00
|
|
|
function trimEnd(str: string, ch: string) {
|
|
|
|
let len = str.length;
|
2021-02-02 04:54:34 +03:00
|
|
|
while (len - 1 >= 0 && str[len - 1] === ch) {
|
2018-06-15 00:14:10 +03:00
|
|
|
--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");
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
2018-06-15 00:14:10 +03:00
|
|
|
// Base64Url to Base64.
|
|
|
|
str = str.replace(/\-/g, "+").replace(/\_/g, "/");
|
|
|
|
// Base64 to Uint8Array.
|
|
|
|
return base64.decodeString(str);
|
|
|
|
}
|
2017-09-13 19:42:16 +03:00
|
|
|
|
2018-09-10 23:44:24 +03:00
|
|
|
function splitSerializeName(prop: string | undefined): string[] {
|
|
|
|
const classes: string[] = [];
|
2018-06-15 00:14:10 +03:00
|
|
|
let partialclass = "";
|
2018-09-05 05:02:45 +03:00
|
|
|
if (prop) {
|
|
|
|
const subwords = prop.split(".");
|
2017-09-13 19:42:16 +03:00
|
|
|
|
2018-09-05 05:02:45 +03:00
|
|
|
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 = "";
|
|
|
|
}
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2017-09-13 19:42:16 +03:00
|
|
|
|
2018-06-15 00:14:10 +03:00
|
|
|
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) {
|
2021-02-02 04:54:34 +03:00
|
|
|
if (typeName.match(/^Number$/gi) !== null) {
|
2018-06-15 00:14:10 +03:00
|
|
|
if (typeof value !== "number") {
|
|
|
|
throw new Error(`${objectName} with value ${value} must be of type number.`);
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (typeName.match(/^String$/gi) !== null) {
|
2018-06-15 00:14:10 +03:00
|
|
|
if (typeof value.valueOf() !== "string") {
|
|
|
|
throw new Error(`${objectName} with value "${value}" must be of type string.`);
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (typeName.match(/^Uuid$/gi) !== null) {
|
2018-06-15 00:14:10 +03:00
|
|
|
if (!(typeof value.valueOf() === "string" && utils.isValidUuid(value))) {
|
2021-02-02 04:54:34 +03:00
|
|
|
throw new Error(
|
|
|
|
`${objectName} with value "${value}" must be of type string and a valid uuid.`
|
|
|
|
);
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (typeName.match(/^Boolean$/gi) !== null) {
|
2018-06-15 00:14:10 +03:00
|
|
|
if (typeof value !== "boolean") {
|
|
|
|
throw new Error(`${objectName} with value ${value} must be of type boolean.`);
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (typeName.match(/^Stream$/gi) !== null) {
|
2018-06-15 00:14:10 +03:00
|
|
|
const objectType = typeof value;
|
2021-02-02 04:54:34 +03:00
|
|
|
if (
|
|
|
|
objectType !== "string" &&
|
2018-10-17 04:49:18 +03:00
|
|
|
objectType !== "function" &&
|
|
|
|
!(value instanceof ArrayBuffer) &&
|
|
|
|
!ArrayBuffer.isView(value) &&
|
2021-02-02 04:54:34 +03:00
|
|
|
!(typeof Blob === "function" && value instanceof Blob)
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
`${objectName} must be a string, Blob, ArrayBuffer, ArrayBufferView, or a function returning NodeJS.ReadableStream.`
|
|
|
|
);
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-15 00:14:10 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function serializeEnumType(objectName: string, allowedValues: Array<any>, value: any): any {
|
|
|
|
if (!allowedValues) {
|
2021-02-02 04:54:34 +03:00
|
|
|
throw new Error(
|
|
|
|
`Please provide a set of allowedValues to validate ${objectName} as an Enum Type.`
|
|
|
|
);
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
const isPresent = allowedValues.some((item) => {
|
|
|
|
if (typeof item.valueOf() === "string") {
|
|
|
|
return item.toLowerCase() === value.toLowerCase();
|
|
|
|
}
|
|
|
|
return item === value;
|
|
|
|
});
|
|
|
|
if (!isPresent) {
|
2021-02-02 04:54:34 +03:00
|
|
|
throw new Error(
|
|
|
|
`${value} is not a valid value for ${objectName}. The valid values are: ${JSON.stringify(
|
|
|
|
allowedValues
|
|
|
|
)}.`
|
|
|
|
);
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function serializeByteArrayType(objectName: string, value: any): any {
|
2018-06-22 06:45:36 +03:00
|
|
|
if (value != undefined) {
|
2018-06-15 00:14:10 +03:00
|
|
|
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 {
|
2018-06-22 06:45:36 +03:00
|
|
|
if (value != undefined) {
|
2018-06-15 00:14:10 +03:00
|
|
|
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) {
|
2021-02-02 04:54:34 +03:00
|
|
|
if (typeName.match(/^Date$/gi) !== null) {
|
|
|
|
if (
|
|
|
|
!(
|
|
|
|
value instanceof Date ||
|
|
|
|
(typeof value.valueOf() === "string" && !isNaN(Date.parse(value)))
|
|
|
|
)
|
|
|
|
) {
|
2018-06-15 00:14:10 +03:00
|
|
|
throw new Error(`${objectName} must be an instanceof Date or a string in ISO8601 format.`);
|
|
|
|
}
|
2021-02-02 04:54:34 +03:00
|
|
|
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)))
|
|
|
|
)
|
|
|
|
) {
|
2018-06-15 00:14:10 +03:00
|
|
|
throw new Error(`${objectName} must be an instanceof Date or a string in ISO8601 format.`);
|
|
|
|
}
|
2021-02-02 04:54:34 +03:00
|
|
|
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)))
|
|
|
|
)
|
|
|
|
) {
|
2018-06-15 00:14:10 +03:00
|
|
|
throw new Error(`${objectName} must be an instanceof Date or a string in RFC-1123 format.`);
|
|
|
|
}
|
2021-02-02 04:54:34 +03:00
|
|
|
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.`
|
|
|
|
);
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
value = dateToUnixTime(value);
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (typeName.match(/^TimeSpan$/gi) !== null) {
|
2018-06-15 00:14:10 +03:00
|
|
|
if (!utils.isDuration(value)) {
|
2021-02-02 04:54:34 +03:00
|
|
|
throw new Error(
|
|
|
|
`${objectName} must be a string in ISO 8601 format. Instead was "${value}".`
|
|
|
|
);
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
function serializeSequenceType(
|
|
|
|
serializer: Serializer,
|
|
|
|
mapper: SequenceMapper,
|
|
|
|
object: any,
|
|
|
|
objectName: string
|
|
|
|
) {
|
2018-06-15 00:14:10 +03:00
|
|
|
if (!Array.isArray(object)) {
|
|
|
|
throw new Error(`${objectName} must be of type Array.`);
|
|
|
|
}
|
2018-06-22 06:45:36 +03:00
|
|
|
const elementType = mapper.type.element;
|
|
|
|
if (!elementType || typeof elementType !== "object") {
|
2021-02-02 04:54:34 +03:00
|
|
|
throw new Error(
|
|
|
|
`element" metadata for an Array must be defined in the ` +
|
|
|
|
`mapper and it must of type "object" in ${objectName}.`
|
|
|
|
);
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
const tempArray = [];
|
|
|
|
for (let i = 0; i < object.length; i++) {
|
2018-06-22 06:45:36 +03:00
|
|
|
tempArray[i] = serializer.serialize(elementType, object[i], objectName);
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
return tempArray;
|
|
|
|
}
|
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
function serializeDictionaryType(
|
|
|
|
serializer: Serializer,
|
|
|
|
mapper: DictionaryMapper,
|
|
|
|
object: any,
|
|
|
|
objectName: string
|
|
|
|
) {
|
2018-06-15 00:14:10 +03:00
|
|
|
if (typeof object !== "object") {
|
|
|
|
throw new Error(`${objectName} must be of type object.`);
|
|
|
|
}
|
2018-06-22 06:45:36 +03:00
|
|
|
const valueType = mapper.type.value;
|
|
|
|
if (!valueType || typeof valueType !== "object") {
|
2021-02-02 04:54:34 +03:00
|
|
|
throw new Error(
|
|
|
|
`"value" metadata for a Dictionary must be defined in the ` +
|
|
|
|
`mapper and it must of type "object" in ${objectName}.`
|
|
|
|
);
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
const tempDictionary: { [key: string]: any } = {};
|
2018-06-19 23:09:51 +03:00
|
|
|
for (const key of Object.keys(object)) {
|
2018-06-22 06:45:36 +03:00
|
|
|
tempDictionary[key] = serializer.serialize(valueType, object[key], objectName + "." + key);
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
return tempDictionary;
|
|
|
|
}
|
|
|
|
|
2018-09-10 23:44:24 +03:00
|
|
|
/**
|
|
|
|
* Resolves a composite mapper's modelProperties.
|
|
|
|
* @param serializer the serializer containing the entire set of mappers
|
|
|
|
* @param mapper the composite mapper to resolve
|
|
|
|
*/
|
2021-02-02 04:54:34 +03:00
|
|
|
function resolveModelProperties(
|
|
|
|
serializer: Serializer,
|
|
|
|
mapper: CompositeMapper,
|
|
|
|
objectName: string
|
|
|
|
): { [propertyName: string]: Mapper } {
|
2018-09-10 23:44:24 +03:00
|
|
|
let modelProps = mapper.type.modelProperties;
|
|
|
|
if (!modelProps) {
|
|
|
|
const className = mapper.type.className;
|
|
|
|
if (!className) {
|
2021-02-02 04:54:34 +03:00
|
|
|
throw new Error(
|
|
|
|
`Class name for model "${objectName}" is not provided in the mapper "${JSON.stringify(
|
|
|
|
mapper,
|
|
|
|
undefined,
|
|
|
|
2
|
|
|
|
)}".`
|
|
|
|
);
|
2018-09-10 23:44:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-02-02 04:54:34 +03:00
|
|
|
throw new Error(
|
|
|
|
`modelProperties cannot be null or undefined in the ` +
|
|
|
|
`mapper "${JSON.stringify(
|
|
|
|
modelMapper
|
|
|
|
)}" of type "${className}" for object "${objectName}".`
|
|
|
|
);
|
2018-09-10 23:44:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modelProps;
|
|
|
|
}
|
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
function serializeCompositeType(
|
|
|
|
serializer: Serializer,
|
|
|
|
mapper: CompositeMapper,
|
|
|
|
object: any,
|
|
|
|
objectName: string
|
|
|
|
) {
|
2018-11-07 21:55:35 +03:00
|
|
|
if (getPolymorphicDiscriminatorRecursively(serializer, mapper)) {
|
2018-09-13 00:41:14 +03:00
|
|
|
mapper = getPolymorphicMapper(serializer, mapper, object, "clientName");
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
|
2018-06-28 22:17:31 +03:00
|
|
|
if (object != undefined) {
|
2018-09-10 23:44:24 +03:00
|
|
|
const payload: any = {};
|
|
|
|
const modelProps = resolveModelProperties(serializer, mapper, objectName);
|
2018-06-15 00:14:10 +03:00
|
|
|
for (const key of Object.keys(modelProps)) {
|
|
|
|
const propertyMapper = modelProps[key];
|
2018-09-10 23:44:24 +03:00
|
|
|
if (propertyMapper.readOnly) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-15 00:14:10 +03:00
|
|
|
|
|
|
|
let propName: string | undefined;
|
|
|
|
let parentObject: any = payload;
|
|
|
|
if (serializer.isXML) {
|
|
|
|
if (propertyMapper.xmlIsWrapped) {
|
|
|
|
propName = propertyMapper.xmlName;
|
|
|
|
} else {
|
|
|
|
propName = propertyMapper.xmlElementName || propertyMapper.xmlName;
|
|
|
|
}
|
|
|
|
} else {
|
2018-07-31 23:08:24 +03:00
|
|
|
const paths = splitSerializeName(propertyMapper.serializedName!);
|
2018-06-15 00:14:10 +03:00
|
|
|
propName = paths.pop();
|
2017-09-13 19:42:16 +03:00
|
|
|
|
2018-06-15 00:14:10 +03:00
|
|
|
for (const pathName of paths) {
|
|
|
|
const childObject = parentObject[pathName];
|
2021-02-02 04:54:34 +03:00
|
|
|
if (childObject == undefined && object[key] != undefined) {
|
2018-06-15 00:14:10 +03:00
|
|
|
parentObject[pathName] = {};
|
|
|
|
}
|
|
|
|
parentObject = parentObject[pathName];
|
|
|
|
}
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
2018-06-15 00:14:10 +03:00
|
|
|
|
2018-06-28 22:17:31 +03:00
|
|
|
if (parentObject != undefined) {
|
2021-02-02 04:54:34 +03:00
|
|
|
const propertyObjectName =
|
|
|
|
propertyMapper.serializedName !== ""
|
|
|
|
? objectName + "." + propertyMapper.serializedName
|
|
|
|
: objectName;
|
2018-06-15 00:14:10 +03:00
|
|
|
|
2018-09-13 00:41:14 +03:00
|
|
|
let toSerialize = object[key];
|
2018-11-07 21:55:35 +03:00
|
|
|
const polymorphicDiscriminator = getPolymorphicDiscriminatorRecursively(serializer, mapper);
|
2021-02-02 04:54:34 +03:00
|
|
|
if (
|
|
|
|
polymorphicDiscriminator &&
|
|
|
|
polymorphicDiscriminator.clientName === key &&
|
|
|
|
toSerialize == undefined
|
|
|
|
) {
|
2018-09-13 00:41:14 +03:00
|
|
|
toSerialize = mapper.serializedName;
|
|
|
|
}
|
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
const serializedValue = serializer.serialize(
|
|
|
|
propertyMapper,
|
|
|
|
toSerialize,
|
|
|
|
propertyObjectName
|
|
|
|
);
|
2018-06-28 22:17:31 +03:00
|
|
|
if (serializedValue !== undefined && propName != undefined) {
|
2018-06-15 00:14:10 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
2018-09-10 23:44:24 +03:00
|
|
|
|
|
|
|
const additionalPropertiesMapper = mapper.type.additionalProperties;
|
|
|
|
if (additionalPropertiesMapper) {
|
|
|
|
const propNames = Object.keys(modelProps);
|
|
|
|
for (const clientPropName in object) {
|
2021-02-02 04:54:34 +03:00
|
|
|
const isAdditionalProperty = propNames.every((pn) => pn !== clientPropName);
|
2018-09-10 23:44:24 +03:00
|
|
|
if (isAdditionalProperty) {
|
2021-02-02 04:54:34 +03:00
|
|
|
payload[clientPropName] = serializer.serialize(
|
|
|
|
additionalPropertiesMapper,
|
|
|
|
object[clientPropName],
|
|
|
|
objectName + '["' + clientPropName + '"]'
|
|
|
|
);
|
2018-09-10 23:44:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 00:14:10 +03:00
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2018-12-04 07:11:31 +03:00
|
|
|
function isSpecialXmlProperty(propertyName: string): boolean {
|
|
|
|
return ["$", "_"].includes(propertyName);
|
|
|
|
}
|
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
function deserializeCompositeType(
|
|
|
|
serializer: Serializer,
|
|
|
|
mapper: CompositeMapper,
|
|
|
|
responseBody: any,
|
|
|
|
objectName: string
|
|
|
|
): any {
|
2018-11-06 00:55:00 +03:00
|
|
|
if (getPolymorphicDiscriminatorRecursively(serializer, mapper)) {
|
2018-09-13 00:41:14 +03:00
|
|
|
mapper = getPolymorphicMapper(serializer, mapper, responseBody, "serializedName");
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
|
2018-09-10 23:44:24 +03:00
|
|
|
const modelProps = resolveModelProperties(serializer, mapper, objectName);
|
2018-07-31 23:08:24 +03:00
|
|
|
let instance: { [key: string]: any } = {};
|
2018-12-04 07:11:31 +03:00
|
|
|
const handledPropertyNames: string[] = [];
|
|
|
|
|
2018-06-15 00:14:10 +03:00
|
|
|
for (const key of Object.keys(modelProps)) {
|
|
|
|
const propertyMapper = modelProps[key];
|
2018-12-04 07:11:31 +03:00
|
|
|
const paths = splitSerializeName(modelProps[key].serializedName!);
|
|
|
|
handledPropertyNames.push(paths[0]);
|
2018-07-10 22:16:51 +03:00
|
|
|
const { serializedName, xmlName, xmlElementName } = propertyMapper;
|
2018-06-15 00:14:10 +03:00
|
|
|
let propertyObjectName = objectName;
|
2018-09-05 05:02:45 +03:00
|
|
|
if (serializedName !== "" && serializedName !== undefined) {
|
2018-07-10 22:16:51 +03:00
|
|
|
propertyObjectName = objectName + "." + serializedName;
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
|
2018-06-19 00:58:26 +03:00
|
|
|
const headerCollectionPrefix = (propertyMapper as DictionaryMapper).headerCollectionPrefix;
|
|
|
|
if (headerCollectionPrefix) {
|
|
|
|
const dictionary: any = {};
|
|
|
|
for (const headerKey of Object.keys(responseBody)) {
|
|
|
|
if (headerKey.startsWith(headerCollectionPrefix)) {
|
2021-02-02 04:54:34 +03:00
|
|
|
dictionary[headerKey.substring(headerCollectionPrefix.length)] = serializer.deserialize(
|
|
|
|
(propertyMapper as DictionaryMapper).type.value,
|
|
|
|
responseBody[headerKey],
|
|
|
|
propertyObjectName
|
|
|
|
);
|
2018-06-19 00:58:26 +03:00
|
|
|
}
|
2018-12-04 07:11:31 +03:00
|
|
|
|
|
|
|
handledPropertyNames.push(headerKey);
|
2018-06-19 00:58:26 +03:00
|
|
|
}
|
|
|
|
instance[key] = dictionary;
|
|
|
|
} else if (serializer.isXML) {
|
2018-06-15 00:14:10 +03:00
|
|
|
if (propertyMapper.xmlIsAttribute && responseBody.$) {
|
2021-02-02 04:54:34 +03:00
|
|
|
instance[key] = serializer.deserialize(
|
|
|
|
propertyMapper,
|
|
|
|
responseBody.$[xmlName!],
|
|
|
|
propertyObjectName
|
|
|
|
);
|
2017-09-13 19:42:16 +03:00
|
|
|
} else {
|
2018-07-10 22:16:51 +03:00
|
|
|
const propertyName = xmlElementName || xmlName || serializedName;
|
2018-06-15 00:14:10 +03:00
|
|
|
let unwrappedProperty = responseBody[propertyName!];
|
|
|
|
if (propertyMapper.xmlIsWrapped) {
|
2018-07-10 22:16:51 +03:00
|
|
|
unwrappedProperty = responseBody[xmlName!];
|
|
|
|
unwrappedProperty = unwrappedProperty && unwrappedProperty[xmlElementName!];
|
2018-09-10 23:44:24 +03:00
|
|
|
|
|
|
|
const isEmptyWrappedList = unwrappedProperty === undefined;
|
|
|
|
if (isEmptyWrappedList) {
|
2018-06-15 00:14:10 +03:00
|
|
|
unwrappedProperty = [];
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 04:54:34 +03:00
|
|
|
instance[key] = serializer.deserialize(
|
|
|
|
propertyMapper,
|
|
|
|
unwrappedProperty,
|
|
|
|
propertyObjectName
|
|
|
|
);
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
2018-06-15 00:14:10 +03:00
|
|
|
} 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;
|
2018-09-13 00:41:14 +03:00
|
|
|
const polymorphicDiscriminator = mapper.type.polymorphicDiscriminator;
|
2019-07-11 06:06:22 +03:00
|
|
|
// 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.
|
2021-02-02 04:54:34 +03:00
|
|
|
if (
|
|
|
|
polymorphicDiscriminator &&
|
|
|
|
key === polymorphicDiscriminator.clientName &&
|
|
|
|
propertyInstance == undefined
|
|
|
|
) {
|
2018-09-13 00:41:14 +03:00
|
|
|
propertyInstance = mapper.serializedName;
|
|
|
|
}
|
|
|
|
|
2018-06-15 00:14:10 +03:00
|
|
|
let serializedValue;
|
|
|
|
// paging
|
|
|
|
if (Array.isArray(responseBody[key]) && modelProps[key].serializedName === "") {
|
|
|
|
propertyInstance = responseBody[key];
|
|
|
|
instance = serializer.deserialize(propertyMapper, propertyInstance, propertyObjectName);
|
2019-07-11 02:44:40 +03:00
|
|
|
} else if (propertyInstance !== undefined || propertyMapper.defaultValue !== undefined) {
|
2021-02-02 04:54:34 +03:00
|
|
|
serializedValue = serializer.deserialize(
|
|
|
|
propertyMapper,
|
|
|
|
propertyInstance,
|
|
|
|
propertyObjectName
|
|
|
|
);
|
2018-06-15 00:14:10 +03:00
|
|
|
instance[key] = serializedValue;
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
|
|
|
}
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
2018-09-10 23:44:24 +03:00
|
|
|
|
|
|
|
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)) {
|
2021-02-02 04:54:34 +03:00
|
|
|
instance[responsePropName] = serializer.deserialize(
|
|
|
|
additionalPropertiesMapper,
|
|
|
|
responseBody[responsePropName],
|
|
|
|
objectName + '["' + responsePropName + '"]'
|
|
|
|
);
|
2018-09-10 23:44:24 +03:00
|
|
|
}
|
|
|
|
}
|
2018-12-26 21:35:17 +03:00
|
|
|
} else if (responseBody) {
|
2018-12-04 07:11:31 +03:00
|
|
|
for (const key of Object.keys(responseBody)) {
|
2021-02-02 04:54:34 +03:00
|
|
|
if (
|
|
|
|
instance[key] === undefined &&
|
|
|
|
!handledPropertyNames.includes(key) &&
|
|
|
|
!isSpecialXmlProperty(key)
|
|
|
|
) {
|
2018-12-04 07:11:31 +03:00
|
|
|
instance[key] = responseBody[key];
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 23:44:24 +03:00
|
|
|
}
|
|
|
|
|
2018-06-15 00:14:10 +03:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
function deserializeDictionaryType(
|
|
|
|
serializer: Serializer,
|
|
|
|
mapper: DictionaryMapper,
|
|
|
|
responseBody: any,
|
|
|
|
objectName: string
|
|
|
|
): any {
|
2018-06-15 00:14:10 +03:00
|
|
|
/*jshint validthis: true */
|
2018-06-22 02:26:46 +03:00
|
|
|
const value = mapper.type.value;
|
|
|
|
if (!value || typeof value !== "object") {
|
2021-02-02 04:54:34 +03:00
|
|
|
throw new Error(
|
|
|
|
`"value" metadata for a Dictionary must be defined in the ` +
|
|
|
|
`mapper and it must of type "object" in ${objectName}`
|
|
|
|
);
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
if (responseBody) {
|
|
|
|
const tempDictionary: { [key: string]: any } = {};
|
2018-06-22 02:26:46 +03:00
|
|
|
for (const key of Object.keys(responseBody)) {
|
|
|
|
tempDictionary[key] = serializer.deserialize(value, responseBody[key], objectName);
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
return tempDictionary;
|
|
|
|
}
|
|
|
|
return responseBody;
|
|
|
|
}
|
2018-04-02 23:01:32 +03:00
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
function deserializeSequenceType(
|
|
|
|
serializer: Serializer,
|
|
|
|
mapper: SequenceMapper,
|
|
|
|
responseBody: any,
|
|
|
|
objectName: string
|
|
|
|
): any {
|
2018-06-15 00:14:10 +03:00
|
|
|
/*jshint validthis: true */
|
2018-06-22 02:26:46 +03:00
|
|
|
const element = mapper.type.element;
|
|
|
|
if (!element || typeof element !== "object") {
|
2021-02-02 04:54:34 +03:00
|
|
|
throw new Error(
|
|
|
|
`element" metadata for an Array must be defined in the ` +
|
|
|
|
`mapper and it must of type "object" in ${objectName}`
|
|
|
|
);
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
2018-06-15 00:14:10 +03:00
|
|
|
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++) {
|
2018-11-07 21:55:35 +03:00
|
|
|
tempArray[i] = serializer.deserialize(element, responseBody[i], `${objectName}[${i}]`);
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
return tempArray;
|
|
|
|
}
|
|
|
|
return responseBody;
|
|
|
|
}
|
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
function getPolymorphicMapper(
|
|
|
|
serializer: Serializer,
|
|
|
|
mapper: CompositeMapper,
|
|
|
|
object: any,
|
|
|
|
polymorphicPropertyName: "clientName" | "serializedName"
|
|
|
|
): CompositeMapper {
|
2018-11-06 00:55:00 +03:00
|
|
|
const polymorphicDiscriminator = getPolymorphicDiscriminatorRecursively(serializer, mapper);
|
2018-06-22 02:26:46 +03:00
|
|
|
if (polymorphicDiscriminator) {
|
2018-09-13 00:41:14 +03:00
|
|
|
const discriminatorName = polymorphicDiscriminator[polymorphicPropertyName];
|
|
|
|
if (discriminatorName != undefined) {
|
|
|
|
const discriminatorValue = object[discriminatorName];
|
|
|
|
if (discriminatorValue != undefined) {
|
2018-11-07 21:55:35 +03:00
|
|
|
const typeName = mapper.type.uberParent || mapper.type.className;
|
2021-02-02 04:54:34 +03:00
|
|
|
const indexDiscriminator =
|
|
|
|
discriminatorValue === typeName
|
|
|
|
? discriminatorValue
|
|
|
|
: typeName + "." + discriminatorValue;
|
2018-09-13 00:41:14 +03:00
|
|
|
const polymorphicMapper = serializer.modelMappers.discriminators[indexDiscriminator];
|
|
|
|
if (polymorphicMapper) {
|
|
|
|
mapper = polymorphicMapper;
|
|
|
|
}
|
2018-09-12 01:04:02 +03:00
|
|
|
}
|
2018-06-15 00:14:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mapper;
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
function getPolymorphicDiscriminatorRecursively(
|
|
|
|
serializer: Serializer,
|
|
|
|
mapper: CompositeMapper
|
|
|
|
): PolymorphicDiscriminator | undefined {
|
|
|
|
return (
|
|
|
|
mapper.type.polymorphicDiscriminator ||
|
|
|
|
getPolymorphicDiscriminatorSafely(serializer, mapper.type.uberParent) ||
|
|
|
|
getPolymorphicDiscriminatorSafely(serializer, mapper.type.className)
|
|
|
|
);
|
2018-11-07 21:55:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPolymorphicDiscriminatorSafely(serializer: Serializer, typeName?: string) {
|
2021-02-02 04:54:34 +03:00
|
|
|
return (
|
|
|
|
typeName &&
|
|
|
|
serializer.modelMappers[typeName] &&
|
|
|
|
serializer.modelMappers[typeName].type.polymorphicDiscriminator
|
|
|
|
);
|
2018-11-06 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
2017-09-13 19:42:16 +03:00
|
|
|
export interface MapperConstraints {
|
|
|
|
InclusiveMaximum?: number;
|
|
|
|
ExclusiveMaximum?: number;
|
|
|
|
InclusiveMinimum?: number;
|
|
|
|
ExclusiveMinimum?: number;
|
|
|
|
MaxLength?: number;
|
|
|
|
MinLength?: number;
|
2018-05-31 22:51:44 +03:00
|
|
|
Pattern?: RegExp;
|
2017-09-13 19:42:16 +03:00
|
|
|
MaxItems?: number;
|
|
|
|
MinItems?: number;
|
|
|
|
UniqueItems?: true;
|
|
|
|
MultipleOf?: number;
|
|
|
|
}
|
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
export type MapperType =
|
|
|
|
| SimpleMapperType
|
|
|
|
| CompositeMapperType
|
|
|
|
| SequenceMapperType
|
|
|
|
| DictionaryMapperType
|
|
|
|
| EnumMapperType;
|
2018-07-31 23:08:24 +03:00
|
|
|
|
|
|
|
export interface SimpleMapperType {
|
2021-02-02 04:54:34 +03:00
|
|
|
name:
|
|
|
|
| "Base64Url"
|
|
|
|
| "Boolean"
|
|
|
|
| "ByteArray"
|
|
|
|
| "Date"
|
|
|
|
| "DateTime"
|
|
|
|
| "DateTimeRfc1123"
|
|
|
|
| "Object"
|
|
|
|
| "Stream"
|
|
|
|
| "String"
|
|
|
|
| "TimeSpan"
|
|
|
|
| "UnixTime"
|
|
|
|
| "Uuid"
|
|
|
|
| "Number"
|
|
|
|
| "any";
|
2018-07-31 23:08:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface CompositeMapperType {
|
|
|
|
name: "Composite";
|
|
|
|
|
|
|
|
// Only one of the two below properties should be present.
|
|
|
|
// Use className to reference another type definition,
|
2018-09-10 23:44:24 +03:00
|
|
|
// and use modelProperties/additionalProperties when the reference to the other type has been resolved.
|
2018-07-31 23:08:24 +03:00
|
|
|
className?: string;
|
2018-09-10 23:44:24 +03:00
|
|
|
|
2018-07-31 23:08:24 +03:00
|
|
|
modelProperties?: { [propertyName: string]: Mapper };
|
2018-09-10 23:44:24 +03:00
|
|
|
additionalProperties?: Mapper;
|
2018-07-31 23:08:24 +03:00
|
|
|
|
|
|
|
uberParent?: string;
|
2018-09-12 01:04:02 +03:00
|
|
|
polymorphicDiscriminator?: PolymorphicDiscriminator;
|
2018-07-31 23:08:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface SequenceMapperType {
|
|
|
|
name: "Sequence";
|
|
|
|
element: Mapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DictionaryMapperType {
|
|
|
|
name: "Dictionary";
|
|
|
|
value: Mapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface EnumMapperType {
|
|
|
|
name: "Enum";
|
|
|
|
allowedValues: any[];
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
|
|
|
|
2018-06-19 20:47:32 +03:00
|
|
|
export interface BaseMapper {
|
2018-03-27 03:54:26 +03:00
|
|
|
xmlName?: string;
|
|
|
|
xmlIsAttribute?: boolean;
|
|
|
|
xmlElementName?: string;
|
2018-03-28 00:55:32 +03:00
|
|
|
xmlIsWrapped?: boolean;
|
2017-09-13 19:42:16 +03:00
|
|
|
readOnly?: boolean;
|
|
|
|
isConstant?: boolean;
|
2018-05-31 22:51:44 +03:00
|
|
|
required?: boolean;
|
2018-06-28 22:17:31 +03:00
|
|
|
nullable?: boolean;
|
2018-07-31 23:08:24 +03:00
|
|
|
serializedName?: string;
|
|
|
|
type: MapperType;
|
2017-09-13 19:42:16 +03:00
|
|
|
defaultValue?: any;
|
|
|
|
constraints?: MapperConstraints;
|
|
|
|
}
|
|
|
|
|
2018-06-19 20:47:32 +03:00
|
|
|
export type Mapper = BaseMapper | CompositeMapper | SequenceMapper | DictionaryMapper | EnumMapper;
|
|
|
|
|
2017-09-13 19:42:16 +03:00
|
|
|
export interface PolymorphicDiscriminator {
|
|
|
|
serializedName: string;
|
|
|
|
clientName: string;
|
|
|
|
[key: string]: string;
|
|
|
|
}
|
|
|
|
|
2018-06-19 20:47:32 +03:00
|
|
|
export interface CompositeMapper extends BaseMapper {
|
2018-07-31 23:08:24 +03:00
|
|
|
type: CompositeMapperType;
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
|
|
|
|
2018-06-19 20:47:32 +03:00
|
|
|
export interface SequenceMapper extends BaseMapper {
|
2018-07-31 23:08:24 +03:00
|
|
|
type: SequenceMapperType;
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
|
|
|
|
2018-06-19 20:47:32 +03:00
|
|
|
export interface DictionaryMapper extends BaseMapper {
|
2018-07-31 23:08:24 +03:00
|
|
|
type: DictionaryMapperType;
|
2018-06-19 00:58:26 +03:00
|
|
|
headerCollectionPrefix?: string;
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
|
|
|
|
2018-06-19 20:47:32 +03:00
|
|
|
export interface EnumMapper extends BaseMapper {
|
2018-07-31 23:08:24 +03:00
|
|
|
type: EnumMapperType;
|
2017-09-13 19:42:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface UrlParameterValue {
|
|
|
|
value: string;
|
|
|
|
skipUrlEncoding: boolean;
|
|
|
|
}
|
|
|
|
|
2018-06-22 06:45:36 +03:00
|
|
|
// TODO: why is this here?
|
2017-09-13 19:42:16 +03:00
|
|
|
export function serializeObject(toSerialize: any): any {
|
2018-06-22 06:45:36 +03:00
|
|
|
if (toSerialize == undefined) return undefined;
|
2018-05-10 21:20:38 +03:00
|
|
|
if (toSerialize instanceof Uint8Array) {
|
|
|
|
toSerialize = base64.encodeByteArray(toSerialize);
|
2017-09-13 19:42:16 +03:00
|
|
|
return toSerialize;
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (toSerialize instanceof Date) {
|
2017-09-13 19:42:16 +03:00
|
|
|
return toSerialize.toISOString();
|
2021-02-02 04:54:34 +03:00
|
|
|
} else if (Array.isArray(toSerialize)) {
|
2017-09-13 19:42:16 +03:00
|
|
|
const array = [];
|
|
|
|
for (let i = 0; i < toSerialize.length; i++) {
|
|
|
|
array.push(serializeObject(toSerialize[i]));
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
} else if (typeof toSerialize === "object") {
|
|
|
|
const dictionary: { [key: string]: any } = {};
|
|
|
|
for (const property in toSerialize) {
|
|
|
|
dictionary[property] = serializeObject(toSerialize[property]);
|
|
|
|
}
|
|
|
|
return dictionary;
|
|
|
|
}
|
|
|
|
return toSerialize;
|
|
|
|
}
|
|
|
|
|
2018-05-23 19:50:08 +03:00
|
|
|
/**
|
|
|
|
* Utility function to create a K:V from a list of strings
|
|
|
|
*/
|
2018-05-31 22:51:44 +03:00
|
|
|
function strEnum<T extends string>(o: Array<T>): { [K in T]: K } {
|
2018-05-23 19:50:08 +03:00
|
|
|
const result: any = {};
|
|
|
|
for (const key of o) {
|
|
|
|
result[key] = key;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const MapperType = strEnum([
|
2017-09-13 19:42:16 +03:00
|
|
|
"Base64Url",
|
|
|
|
"Boolean",
|
|
|
|
"ByteArray",
|
|
|
|
"Composite",
|
|
|
|
"Date",
|
|
|
|
"DateTime",
|
|
|
|
"DateTimeRfc1123",
|
|
|
|
"Dictionary",
|
|
|
|
"Enum",
|
|
|
|
"Number",
|
|
|
|
"Object",
|
|
|
|
"Sequence",
|
|
|
|
"String",
|
|
|
|
"Stream",
|
|
|
|
"TimeSpan",
|
2021-02-02 04:54:34 +03:00
|
|
|
"UnixTime",
|
2019-06-26 02:47:13 +03:00
|
|
|
]);
|