Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core-http] Support xml namespaces #11201

Merged
merged 9 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions sdk/core/core-http/review/core-http.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export interface BaseMapper {
xmlIsWrapped?: boolean;
// (undocumented)
xmlName?: string;
// (undocumented)
joheredi marked this conversation as resolved.
Show resolved Hide resolved
xmlNamespace?: string;
// (undocumented)
xmlNamespacePrefix?: string;
}

// @public (undocumented)
Expand Down
20 changes: 18 additions & 2 deletions sdk/core/core-http/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ function serializeBasicTypes(typeName: string, objectName: string, value: any):
}
}
}

return value;
}

Expand Down Expand Up @@ -589,6 +590,12 @@ function serializeCompositeType(
}

if (parentObject != undefined) {
if (mapper.xmlNamespace) {
const xmlnsKey = mapper.xmlNamespacePrefix
? `xmlns:${mapper.xmlNamespacePrefix}`
: "xmlns";
parentObject.$ = { [xmlnsKey]: mapper.xmlNamespace };
}
const propertyObjectName =
propertyMapper.serializedName !== ""
? objectName + "." + propertyMapper.serializedName
Expand All @@ -610,16 +617,23 @@ function serializeCompositeType(
propertyObjectName
);
if (serializedValue !== undefined && propName != undefined) {
const xmlnsKey = propertyMapper.xmlNamespacePrefix
? `xmlns:${propertyMapper.xmlNamespacePrefix}`
: "xmlns";
const xmlNamespace = { [xmlnsKey]: propertyMapper.xmlNamespace };
const value = propertyMapper.xmlNamespace
? { _: serializedValue, $: xmlNamespace }
: serializedValue;
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 };
parentObject[propName] = { [propertyMapper.xmlElementName!]: value };
} else {
parentObject[propName] = serializedValue;
parentObject[propName] = value;
}
}
}
Expand Down Expand Up @@ -961,6 +975,8 @@ export interface EnumMapperType {

export interface BaseMapper {
xmlName?: string;
xmlNamespace?: string;
xmlNamespacePrefix?: string;
xmlIsAttribute?: boolean;
xmlElementName?: string;
xmlIsWrapped?: boolean;
Expand Down
10 changes: 7 additions & 3 deletions sdk/core/core-http/src/serviceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ export class ServiceClient {
);
}
}
console.log("request info = ", httpRequest.url, httpRequest.operationSpec?.requestBody);
joheredi marked this conversation as resolved.
Show resolved Hide resolved
return httpPipeline.sendRequest(httpRequest);
}

Expand Down Expand Up @@ -489,6 +490,7 @@ export class ServiceClient {
let rawResponse: HttpOperationResponse;
let sendRequestError;
try {
console.log(httpRequest);
joheredi marked this conversation as resolved.
Show resolved Hide resolved
rawResponse = await this.sendRequest(httpRequest);
} catch (error) {
sendRequestError = error;
Expand Down Expand Up @@ -538,7 +540,7 @@ export function serializeRequestBody(
);

const bodyMapper = operationSpec.requestBody.mapper;
const { required, xmlName, xmlElementName, serializedName } = bodyMapper;
const { required, xmlName, xmlElementName, serializedName, xmlNamespace, xmlNamespacePrefix} = bodyMapper;
const typeName = bodyMapper.type.name;

try {
Expand All @@ -555,16 +557,18 @@ export function serializeRequestBody(
const isStream = typeName === MapperType.Stream;

if (operationSpec.isXML) {
const xmlnsKey = xmlNamespacePrefix ? `xmlns:${xmlNamespacePrefix}` : "xmlns";
const value = xmlNamespace && !["Composite", "Sequence"].includes(typeName) ? {_: httpRequest.body, $: {[xmlnsKey]: xmlNamespace}} : httpRequest.body;
joheredi marked this conversation as resolved.
Show resolved Hide resolved
if (typeName === MapperType.Sequence) {
httpRequest.body = stringifyXML(
utils.prepareXMLRootList(
httpRequest.body,
value,
xmlElementName || xmlName || serializedName!
),
{ rootName: xmlName || serializedName }
);
} else if (!isStream) {
httpRequest.body = stringifyXML(httpRequest.body, {
httpRequest.body = stringifyXML(value, {
rootName: xmlName || serializedName
});
}
Expand Down
232 changes: 232 additions & 0 deletions sdk/core/core-http/test/serviceClientTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,38 @@ describe("ServiceClient", function() {
);
});

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

it("should serialize an XML ByteArray request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
Expand Down Expand Up @@ -598,6 +630,206 @@ describe("ServiceClient", function() {
);
});

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

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

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

it.skip("should serialize an XML Array request body with namespace and prefix", () => {
const httpRequest = new WebResource();
serializeRequestBody(
new ServiceClient(),
httpRequest,
{
bodyArg: ["Foo", "Bar"]
},
{
httpMethod: "POST",
requestBody: {
parameterPath: "bodyArg",
mapper: {
required: true,
serializedName: "bodyArg",
xmlNamespace: "https://microsoft.com",
xmlElementName: "testItem",
type: {
name: MapperType.Sequence,
element: {
type: {name: "String"},
xmlNamespace: "https://microsoft.com"
}
}
}
},
responses: { 200: {} },
serializer: new Serializer(undefined, true),
isXML: true
}
);
assert.strictEqual(
httpRequest.body,
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><bodyArg xmlns="https://microsoft.com"><Foo xmlns="https://microsoft.com/foo">Foo</Foo><Bar xmlns:bar="https://microsoft.com/bar">Bar</Bar></bodyArg>`
);
})

it("should serialize an XML Composite request body with namespace and prefix", () => {
const httpRequest = new WebResource();
serializeRequestBody(
new ServiceClient(),
httpRequest,
{
bodyArg: {foo: "Foo", bar: "Bar"}
},
{
httpMethod: "POST",
requestBody: {
parameterPath: "bodyArg",
mapper: {
required: true,
serializedName: "bodyArg",
xmlNamespace: "https://microsoft.com",
type: {
name: MapperType.Composite,
modelProperties: {
foo: {
serializedName: "foo",
xmlNamespace: "https://microsoft.com/foo",
xmlName: "Foo",
type: {
name: "String"
}
},
bar: {
xmlNamespacePrefix: "bar",
xmlNamespace: "https://microsoft.com/bar",
xmlName: "Bar",
serializedName: "bar",
type: {
name: "String"
}
}
}
}
}
},
responses: { 200: {} },
serializer: new Serializer(undefined, true),
isXML: true
}
);
assert.strictEqual(
httpRequest.body,
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><bodyArg xmlns="https://microsoft.com"><Foo xmlns="https://microsoft.com/foo">Foo</Foo><Bar xmlns:bar="https://microsoft.com/bar">Bar</Bar></bodyArg>`
);
})

it("should serialize an XML Stream request body", () => {
const httpRequest = new WebResource();
serializeRequestBody(
Expand Down