Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions sdk/core/core-http/lib/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class Serializer {
if (!objectName) {
objectName = mapper.serializedName!;
}
if (mapperType.match(/^Sequence$/gi) !== null) {
if (mapperType.match(/^Sequence$/i) !== null) {
payload = [];
}

Expand Down Expand Up @@ -124,26 +124,26 @@ export class Serializer {
} else {
// Validate Constraints if any
this.validateConstraints(mapper, object, objectName);
if (mapperType.match(/^any$/gi) !== null) {
if (mapperType.match(/^any$/i) !== null) {
payload = object;
} else if (mapperType.match(/^(Number|String|Boolean|Object|Stream|Uuid)$/gi) !== null) {
} else if (mapperType.match(/^(Number|String|Boolean|Object|Stream|Uuid)$/i) !== null) {
payload = serializeBasicTypes(mapperType, objectName, object);
} else if (mapperType.match(/^Enum$/gi) !== null) {
} else if (mapperType.match(/^Enum$/i) !== 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
mapperType.match(/^(Date|DateTime|TimeSpan|DateTimeRfc1123|UnixTime)$/i) !== null
) {
payload = serializeDateTypes(mapperType, object, objectName);
} else if (mapperType.match(/^ByteArray$/gi) !== null) {
} else if (mapperType.match(/^ByteArray$/i) !== null) {
payload = serializeByteArrayType(objectName, object);
} else if (mapperType.match(/^Base64Url$/gi) !== null) {
} else if (mapperType.match(/^Base64Url$/i) !== null) {
payload = serializeBase64UrlType(objectName, object);
} else if (mapperType.match(/^Sequence$/gi) !== null) {
} else if (mapperType.match(/^Sequence$/i) !== null) {
payload = serializeSequenceType(this, mapper as SequenceMapper, object, objectName);
} else if (mapperType.match(/^Dictionary$/gi) !== null) {
} else if (mapperType.match(/^Dictionary$/i) !== null) {
payload = serializeDictionaryType(this, mapper as DictionaryMapper, object, objectName);
} else if (mapperType.match(/^Composite$/gi) !== null) {
} else if (mapperType.match(/^Composite$/i) !== null) {
payload = serializeCompositeType(this, mapper as CompositeMapper, object, objectName);
}
}
Expand Down Expand Up @@ -182,7 +182,7 @@ export class Serializer {
objectName = mapper.serializedName!;
}

if (mapperType.match(/^Composite$/gi) !== null) {
if (mapperType.match(/^Composite$/i) !== null) {
payload = deserializeCompositeType(this, mapper as CompositeMapper, responseBody, objectName);
} else {
if (this.isXML) {
Expand All @@ -196,32 +196,32 @@ export class Serializer {
}
}

if (mapperType.match(/^Number$/gi) !== null) {
if (mapperType.match(/^Number$/i) !== null) {
payload = parseFloat(responseBody);
if (isNaN(payload)) {
payload = responseBody;
}
} else if (mapperType.match(/^Boolean$/gi) !== null) {
} else if (mapperType.match(/^Boolean$/i) !== 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) {
} else if (mapperType.match(/^(String|Enum|Object|Stream|Uuid|TimeSpan|any)$/i) !== null) {
payload = responseBody;
} else if (mapperType.match(/^(Date|DateTime|DateTimeRfc1123)$/gi) !== null) {
} else if (mapperType.match(/^(Date|DateTime|DateTimeRfc1123)$/i) !== null) {
payload = new Date(responseBody);
} else if (mapperType.match(/^UnixTime$/gi) !== null) {
} else if (mapperType.match(/^UnixTime$/i) !== null) {
payload = unixTimeToDate(responseBody);
} else if (mapperType.match(/^ByteArray$/gi) !== null) {
} else if (mapperType.match(/^ByteArray$/i) !== null) {
payload = base64.decodeString(responseBody);
} else if (mapperType.match(/^Base64Url$/gi) !== null) {
} else if (mapperType.match(/^Base64Url$/i) !== null) {
payload = base64UrlToByteArray(responseBody);
} else if (mapperType.match(/^Sequence$/gi) !== null) {
} else if (mapperType.match(/^Sequence$/i) !== null) {
payload = deserializeSequenceType(this, mapper as SequenceMapper, responseBody, objectName);
} else if (mapperType.match(/^Dictionary$/gi) !== null) {
} else if (mapperType.match(/^Dictionary$/i) !== null) {
payload = deserializeDictionaryType(
this,
mapper as DictionaryMapper,
Expand Down Expand Up @@ -315,25 +315,25 @@ function unixTimeToDate(n: number): Date | undefined {

function serializeBasicTypes(typeName: string, objectName: string, value: any): any {
if (value !== null && value !== undefined) {
if (typeName.match(/^Number$/gi) !== null) {
if (typeName.match(/^Number$/i) !== null) {
if (typeof value !== "number") {
throw new Error(`${objectName} with value ${value} must be of type number.`);
}
} else if (typeName.match(/^String$/gi) !== null) {
} else if (typeName.match(/^String$/i) !== 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) {
} else if (typeName.match(/^Uuid$/i) !== 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) {
} else if (typeName.match(/^Boolean$/i) !== null) {
if (typeof value !== "boolean") {
throw new Error(`${objectName} with value ${value} must be of type boolean.`);
}
} else if (typeName.match(/^Stream$/gi) !== null) {
} else if (typeName.match(/^Stream$/i) !== null) {
const objectType = typeof value;
if (
objectType !== "string" &&
Expand Down Expand Up @@ -395,7 +395,7 @@ function serializeBase64UrlType(objectName: string, value: any): any {

function serializeDateTypes(typeName: string, value: any, objectName: string) {
if (value != undefined) {
if (typeName.match(/^Date$/gi) !== null) {
if (typeName.match(/^Date$/i) !== null) {
if (
!(
value instanceof Date ||
Expand All @@ -408,7 +408,7 @@ function serializeDateTypes(typeName: string, value: any, objectName: string) {
value instanceof Date
? value.toISOString().substring(0, 10)
: new Date(value).toISOString().substring(0, 10);
} else if (typeName.match(/^DateTime$/gi) !== null) {
} else if (typeName.match(/^DateTime$/i) !== null) {
if (
!(
value instanceof Date ||
Expand All @@ -418,7 +418,7 @@ function serializeDateTypes(typeName: string, value: any, objectName: string) {
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) {
} else if (typeName.match(/^DateTimeRfc1123$/i) !== null) {
if (
!(
value instanceof Date ||
Expand All @@ -428,7 +428,7 @@ function serializeDateTypes(typeName: string, value: any, objectName: string) {
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) {
} else if (typeName.match(/^UnixTime$/i) !== null) {
if (
!(
value instanceof Date ||
Expand All @@ -441,7 +441,7 @@ function serializeDateTypes(typeName: string, value: any, objectName: string) {
);
}
value = dateToUnixTime(value);
} else if (typeName.match(/^TimeSpan$/gi) !== null) {
} else if (typeName.match(/^TimeSpan$/i) !== null) {
if (!utils.isDuration(value)) {
throw new Error(
`${objectName} must be a string in ISO 8601 format. Instead was "${value}".`
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-http/lib/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RestError } from "../restError";
import { WebResource } from "../webResource";
import { Constants } from "./constants";

const validUuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/gi;
const validUuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/i;

/**
* A constant that indicates whether the environment is node.js or browser based.
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-http/rollup.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function browserConfig(test = false, production = false) {
}),
cjs({
namedExports: {
chai: ["assert", "AssertionError", "should"],
chai: ["assert", "AssertionError", "should", "expect"],
events: ["EventEmitter"],
"@opentelemetry/types": ["CanonicalCode", "SpanKind", "TraceFlags"]
}
Expand Down
17 changes: 17 additions & 0 deletions sdk/core/core-http/test/utilsTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { expect } from "chai";
import { isValidUuid } from "../lib/util/utils";

describe("utils", function() {
describe("isValidUuid()", function() {
it("should return true on a valid Uuid in multiple calls", function() {

const uuidString = "ae9b0564-7aa1-47d1-8061-0ffd8f12f723";

expect(isValidUuid(uuidString), "First call failed").to.be.true;
expect(isValidUuid(uuidString), "Second call failed").to.be.true;
})
});
})