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

Make sure parsed body is an array when expecting Sequence #385

Merged
merged 6 commits into from
Apr 30, 2020
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
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Fixes encoding query parameters in an array before joining them.(PR [#382](https://github.com/Azure/ms-rest-js/pull/382))
- Replace public usage of `RequestPolicyOptions` to an interface `RequestPolicyOptionsLike` to avoid compatibility issues with private members.
- Fix issue with null/undefined values in array and tabs/space delimiter arrays during sendOperationRequest. [PR #390](https://github.com/Azure/ms-rest-js/pull/390)
- Fix in flattenResponse when expecting an array, checking for parsedBody to be an array before proceeding with flattening. (PR [#385](https://github.com/Azure/ms-rest-js/pull/385))

## 2.0.6 - 2020-04-15
- A new interface `WebResourceLike` was introduced to avoid a direct dependency on the class `WebResource` in public interfaces. `HttpHeadersLike` was also added to replace references to `HttpHeaders`. This change was added to improve compatibility between `@azure/core-http` and `@azure/ms-rest-nodeauth`.
Expand Down
8 changes: 7 additions & 1 deletion lib/serviceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,13 @@ export function flattenResponse(_response: HttpOperationResponse, responseSpec:
const modelProperties = typeName === "Composite" && (bodyMapper as CompositeMapper).type.modelProperties || {};
const isPageableResponse = Object.keys(modelProperties).some(k => modelProperties[k].serializedName === "");
if (typeName === "Sequence" || isPageableResponse) {
const arrayResponse = [...(_response.parsedBody || [])] as RestResponse & any[];
// We're expecting a sequece(array) make sure that the response body is in the
// correct format, if not make it an empty array []
const parsedBody =
Array.isArray(_response.parsedBody)
? _response.parsedBody
: [];
const arrayResponse = [...parsedBody] as RestResponse & any[];
ramya-rao-a marked this conversation as resolved.
Show resolved Hide resolved

for (const key of Object.keys(modelProperties)) {
if (modelProperties[key].serializedName) {
Expand Down
73 changes: 73 additions & 0 deletions test/serviceClientTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,79 @@ describe("ServiceClient", function () {
assert.deepStrictEqual(res.slice(), [1, 2, 3]);
});

it("should deserialize response bodies with undefined array", async function () {
let request: WebResourceLike;
const httpClient: HttpClient = {
sendRequest: (req) => {
request = req;
return Promise.resolve({
request,
status: 200,
headers: new HttpHeaders(),
bodyAsText: JSON.stringify({ nextLink: "mockLink" }),
});
},
};

const client1 = new ServiceClient(undefined, {
httpClient,
requestPolicyFactories: [deserializationPolicy()],
});

const res = (await client1.sendOperationRequest(
{},
{
serializer: new Serializer(),
httpMethod: "GET",
baseUrl: "httpbin.org",
responses: {
"200": {
bodyMapper: {
type: {
name: "Composite",
modelProperties: {
value: {
serializedName: "",
type: {
name: "Sequence",
element: {
type: { name: "String" },
},
},
},
nextLink: {
serializedName: "nextLink",
type: { name: "String" },
},
},
},
},
},
default: {
bodyMapper: {
serializedName: "ErrorResponse",
type: {
name: "Composite",
className: "ErrorResponse",
modelProperties: {
code: { serializedName: "code", type: { name: "String" } },
message: {
serializedName: "message",
type: { name: "String" },
},
},
},
},
},
},
}
)) as any;

assert.strictEqual(res._response.status, 200);
assert.deepStrictEqual(res.nextLink, "mockLink");
assert.deepStrictEqual(res, []);
});

it("should use userAgent header name value from options", async function () {
const httpClient: HttpClient = {
sendRequest: (request: WebResourceLike) => {
Expand Down