Skip to content

Commit

Permalink
Merge pull request Azure#165 from Azure/daschult/xmlStreamBodies
Browse files Browse the repository at this point in the history
Fix bug where we were stringifying XML stream request bodies
  • Loading branch information
Dan Schulte authored Jul 9, 2018
2 parents 8748897 + cd42407 commit e04821f
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 14 deletions.
7 changes: 4 additions & 3 deletions lib/serviceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export class ServiceClient {
}
}

function serializeRequestBody(httpRequest: WebResource, operationArguments: OperationArguments, operationSpec: OperationSpec): void {
export function serializeRequestBody(httpRequest: WebResource, operationArguments: OperationArguments, operationSpec: OperationSpec): void {
if (operationSpec.requestBody && operationSpec.requestBody.mapper) {
httpRequest.body = getOperationArgumentValueFromParameter(operationArguments, operationSpec.requestBody, operationSpec.serializer);

Expand All @@ -314,14 +314,15 @@ function serializeRequestBody(httpRequest: WebResource, operationArguments: Oper
if (httpRequest.body != undefined || required) {
const requestBodyParameterPathString: string = getPathStringFromParameter(operationSpec.requestBody);
httpRequest.body = operationSpec.serializer.serialize(bodyMapper, httpRequest.body, requestBodyParameterPathString);
const isStream = typeName === MapperType.Stream;
if (operationSpec.isXML) {
if (typeName === MapperType.Sequence) {
httpRequest.body = utils.stringifyXML(utils.prepareXMLRootList(httpRequest.body, xmlElementName || xmlName || serializedName), { rootName: xmlName || serializedName });
}
else {
else if (!isStream) {
httpRequest.body = utils.stringifyXML(httpRequest.body, { rootName: xmlName || serializedName });
}
} else if (typeName !== MapperType.Stream) {
} else if (!isStream) {
httpRequest.body = JSON.stringify(httpRequest.body);
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ export function stringifyXML(obj: any, opts?: { rootName?: string }) {
const builder = new xml2js.Builder({
explicitArray: false,
explicitCharkey: false,
rootName: (opts || {}).rootName
rootName: (opts || {}).rootName,
renderOpts: {
pretty: false
}
});
return builder.buildObject(obj);
}
Expand Down
30 changes: 22 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

184 changes: 182 additions & 2 deletions test/shared/serviceClientTests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ServiceClient, WebResource, Serializer, HttpOperationResponse, DictionaryMapper, QueryCollectionFormat, SequenceMapper, HttpClient } from "../../lib/msRest";
import { ServiceClient, WebResource, Serializer, HttpOperationResponse, DictionaryMapper, QueryCollectionFormat, SequenceMapper, HttpClient, MapperType } from "../../lib/msRest";
import { serializeRequestBody } from "../../lib/serviceClient";
import * as assert from "assert";

describe("ServiceClient", function () {
Expand Down Expand Up @@ -156,4 +157,183 @@ describe("ServiceClient", function () {
});
assert.strictEqual(request!.withCredentials, true);
});
});

describe("serializeRequestBody()", () => {
it("should serialize a JSON String request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
httpRequest,
{
arguments: {
bodyArg: "body value"
}
},
{
httpMethod: "POST",
requestBody: {
parameterPath: "bodyArg",
mapper: {
required: true,
serializedName: "bodyArg",
type: {
name: MapperType.String
}
}
},
responses: { 200: {} },
serializer: new Serializer()
});
assert.strictEqual(httpRequest.body, `"body value"`);
});

it("should serialize a JSON ByteArray request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
httpRequest,
{
arguments: {
bodyArg: stringToByteArray("Javascript")
}
},
{
httpMethod: "POST",
requestBody: {
parameterPath: "bodyArg",
mapper: {
required: true,
serializedName: "bodyArg",
type: {
name: MapperType.ByteArray
}
}
},
responses: { 200: {} },
serializer: new Serializer()
});
assert.strictEqual(httpRequest.body, `"SmF2YXNjcmlwdA=="`);
});

it("should serialize a JSON Stream request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
httpRequest,
{
arguments: {
bodyArg: "body value"
}
},
{
httpMethod: "POST",
requestBody: {
parameterPath: "bodyArg",
mapper: {
required: true,
serializedName: "bodyArg",
type: {
name: MapperType.Stream
}
}
},
responses: { 200: {} },
serializer: new Serializer()
});
assert.strictEqual(httpRequest.body, "body value");
});

it("should serialize an XML String request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
httpRequest,
{
arguments: {
bodyArg: "body value"
}
},
{
httpMethod: "POST",
requestBody: {
parameterPath: "bodyArg",
mapper: {
required: true,
serializedName: "bodyArg",
type: {
name: MapperType.String
}
}
},
responses: { 200: {} },
serializer: new Serializer(),
isXML: true
});
assert.strictEqual(
httpRequest.body,
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><bodyArg>body value</bodyArg>`);
});

it("should serialize an XML ByteArray request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
httpRequest,
{
arguments: {
bodyArg: stringToByteArray("Javascript")
}
},
{
httpMethod: "POST",
requestBody: {
parameterPath: "bodyArg",
mapper: {
required: true,
serializedName: "bodyArg",
type: {
name: MapperType.ByteArray
}
}
},
responses: { 200: {} },
serializer: new Serializer(),
isXML: true
});
assert.strictEqual(
httpRequest.body,
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><bodyArg>SmF2YXNjcmlwdA==</bodyArg>`);
});

it("should serialize an XML Stream request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
httpRequest,
{
arguments: {
bodyArg: "body value"
}
},
{
httpMethod: "POST",
requestBody: {
parameterPath: "bodyArg",
mapper: {
required: true,
serializedName: "bodyArg",
type: {
name: MapperType.Stream
}
}
},
responses: { 200: {} },
serializer: new Serializer(),
isXML: true
});
assert.strictEqual(httpRequest.body, "body value");
});
});
});

function stringToByteArray(str: string): Uint8Array {
if (typeof Buffer === "function") {
return Buffer.from(str, "utf-8");
} else {
return new TextEncoder().encode(str);
}
}

0 comments on commit e04821f

Please sign in to comment.