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

* Make sure parsed body is an array when expecting Sequence

* Update lib/serviceClient.ts

Co-Authored-By: Ramya Rao <ramya.rao.a@outlook.com>

* Update changelog and constants

* Add test

* update changelog

* Remove unecessary format changes

Co-authored-by: Ramya Rao <ramya.rao.a@outlook.com>
This commit is contained in:
Jose Manuel Heredia Hidalgo 2020-04-30 16:22:14 -07:00 коммит произвёл GitHub
Родитель 41b25a0e0a
Коммит 118158b6f9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 81 добавлений и 1 удалений

Просмотреть файл

@ -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`.

Просмотреть файл

@ -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[];
for (const key of Object.keys(modelProperties)) {
if (modelProperties[key].serializedName) {

Просмотреть файл

@ -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) => {