Skip to content

Commit 36c2775

Browse files
authored
[core-client] wrap response body for more mapper types (Azure#15005)
This PR add more mapper types that are consider primitive thus require wrapping.
1 parent 5963b6a commit 36c2775

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

sdk/core/core-client/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 1.1.1 (Unreleased)
44

55
- Expose `allowInsecureConnection` in `ServiceClientOptions` and `OperationRequestOptions` to allow operation requests to HTTP endpoints
6+
- Consider more mapper types as primitive thus requires wrapping
67

78
## 1.1.0 (2021-03-30)
89

sdk/core/core-client/src/utils.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,25 @@ import { CompositeMapper, FullOperationResponse, OperationResponseMap } from "./
77
* The union of all possible types for a primitive response body.
88
* @internal
99
*/
10-
export type BodyPrimitive = number | string | boolean | undefined | null;
10+
export type BodyPrimitive = number | string | boolean | Date | Uint8Array | undefined | null;
1111

1212
/**
1313
* A type guard for a primitive response body.
1414
* @param value - Value to test
1515
*
1616
* @internal
1717
*/
18-
export function isPrimitiveBody(value: unknown): value is BodyPrimitive {
18+
export function isPrimitiveBody(value: unknown, mapperTypeName?: string): value is BodyPrimitive {
1919
return (
20-
typeof value === "string" ||
21-
typeof value === "number" ||
22-
typeof value === "boolean" ||
23-
value === undefined ||
24-
value === null
20+
mapperTypeName !== "Composite" &&
21+
mapperTypeName !== "Dictionary" &&
22+
(typeof value === "string" ||
23+
typeof value === "number" ||
24+
typeof value === "boolean" ||
25+
mapperTypeName?.match(/^(Date|DateTime|DateTimeRfc1123|UnixTime|ByteArray|Base64Url)$/i) !==
26+
null ||
27+
value === undefined ||
28+
value === null)
2529
);
2630
}
2731

@@ -173,9 +177,6 @@ export function flattenResponse(
173177
body: fullResponse.parsedBody,
174178
headers: parsedHeaders,
175179
hasNullableType: isNullable,
176-
shouldWrapBody:
177-
expectedBodyTypeName !== "Composite" &&
178-
expectedBodyTypeName !== "Dictionary" &&
179-
isPrimitiveBody(fullResponse.parsedBody)
180+
shouldWrapBody: isPrimitiveBody(fullResponse.parsedBody, expectedBodyTypeName)
180181
});
181182
}

sdk/core/core-client/test/serviceClient.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,35 @@ describe("ServiceClient", function() {
11651165
operationSpec
11661166
);
11671167
});
1168+
1169+
it("should wrap body when bodyWrapper is specified", async function() {
1170+
const operationSpec: OperationSpec = {
1171+
path: "/datetime/invalid",
1172+
httpMethod: "GET",
1173+
responses: {
1174+
200: {
1175+
bodyMapper: { type: { name: "DateTime" } }
1176+
}
1177+
},
1178+
baseUrl: "http://example.com",
1179+
serializer: createSerializer()
1180+
};
1181+
1182+
const client = new ServiceClient({
1183+
httpClient: {
1184+
sendRequest: (req) => {
1185+
return Promise.resolve({
1186+
request: req,
1187+
status: 200,
1188+
headers: createHttpHeaders(),
1189+
bodyAsText: `"201O-18-90D00:89:56.9AX"`
1190+
});
1191+
}
1192+
}
1193+
});
1194+
const response = await client.sendOperationRequest<{ body: Date }>({}, operationSpec);
1195+
assert.ok(response.body);
1196+
});
11681197
});
11691198

11701199
async function testSendOperationRequest(

0 commit comments

Comments
 (0)