Skip to content

Commit

Permalink
[core-client-rest] Update Serialization Check Logic (#30339)
Browse files Browse the repository at this point in the history
### Describe the problem that is addressed by this PR
Currently, all request body that has content types that start with
"application/json" will be serialized by the `getRequestBody` function.
This is an issue for Schema Registry because the request body has
already been serialized and the content type also starts with
"application/json". Reordering the serialization method to check for
`Uint8Array` before checking for "application/json" would eliminate this
problem.

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?
- Add an option called `skipSerialization` in the internal options bag
for sending request. It'll affect the RLC experience and force the
customer to use this option to make a successful request
- Check for specific content types. It'll be limited in scope and affect
other services that also has a similar content type format
  • Loading branch information
minhanh-phan committed Jul 11, 2024
1 parent 6fbc988 commit 3888ad0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
10 changes: 3 additions & 7 deletions sdk/core/core-client-rest/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# Release History

## 2.1.1 (Unreleased)

### Features Added

### Breaking Changes

### Bugs Fixed
## 2.2.0 (2024-07-11)

### Other Changes

- Update serialization to not serialize Uint8Array if the content type is "application/json".

## 2.1.0 (2024-06-27)

### Features Added
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-client-rest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure-rest/core-client",
"version": "2.1.1",
"version": "2.2.0",
"description": "Core library for interfacing with Azure Rest Clients",
"sdk-type": "client",
"type": "module",
Expand Down
10 changes: 4 additions & 6 deletions sdk/core/core-client-rest/src/sendRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,15 @@ function getRequestBody(body?: unknown, contentType: string = ""): RequestBody {
return { body };
}

const firstType = contentType.split(";")[0];

if (firstType === "application/json") {
return { body: JSON.stringify(body) };
}

if (ArrayBuffer.isView(body)) {
return { body: body instanceof Uint8Array ? body : JSON.stringify(body) };
}

const firstType = contentType.split(";")[0];

switch (firstType) {
case "application/json":
return { body: JSON.stringify(body) };
case "multipart/form-data":
if (Array.isArray(body)) {
return { multipartBody: buildMultipartBody(body as PartDescriptor[]) };
Expand Down

0 comments on commit 3888ad0

Please sign in to comment.