Skip to content

Commit

Permalink
Fix malformed multipart body (#1423)
Browse files Browse the repository at this point in the history
* fix malformed body and include additional optional data

* Revert "fix malformed body and include additional optional data"

This reverts commit 26d18e1.

* revert changes, only fix \r\n

* prettify code

* prettify files

* fix code format

* added guard for in

* fix code errors"

* pretify code

* enable nulls

* add unit test for multipart error fix

* fix code format

* reintroduce nulls to writer methods

* feat(multipart-body): fix multipart body serialization

* fix: switches back to arrow function

* chore: linting

---------

Co-authored-by: aliep <[email protected]>
Co-authored-by: Vincent Biret <[email protected]>
  • Loading branch information
3 people authored Oct 24, 2024
1 parent 4ce2e5b commit 1925744
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
12 changes: 8 additions & 4 deletions packages/abstractions/src/multipartBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ interface MultipartEntry {
serializationCallback?: ModelSerializerFunction<Parsable>;
}

export const serializeMultipartBody = (writer: SerializationWriter, multipartBody: Partial<MultipartBody> | undefined | null = new MultipartBody()): void => {
export const serializeMultipartBody = (writer: SerializationWriter, multipartBody: Partial<MultipartBody> = new MultipartBody()): void => {
if (!writer) {
throw new Error("writer cannot be undefined");
}
Expand All @@ -117,13 +117,16 @@ export const serializeMultipartBody = (writer: SerializationWriter, multipartBod
if (first) {
first = false;
} else {
writer.writeStringValue(undefined, "");
writer.writeStringValue(undefined, "\r\n");
}
writer.writeStringValue(undefined, "--" + boundary);
writer.writeStringValue(undefined, "\r\n");
const part = parts[partName];
writer.writeStringValue("Content-Type", part.contentType);
writer.writeStringValue(undefined, "\r\n");
writer.writeStringValue("Content-Disposition", 'form-data; name="' + part.originalName + '"');
writer.writeStringValue(undefined, "");
writer.writeStringValue(undefined, "\r\n");
writer.writeStringValue(undefined, "\r\n");
if (typeof part.content === "string") {
writer.writeStringValue(undefined, part.content);
} else if (part.content instanceof ArrayBuffer) {
Expand All @@ -150,8 +153,9 @@ export const serializeMultipartBody = (writer: SerializationWriter, multipartBod
}
}
}
writer.writeStringValue(undefined, "");
writer.writeStringValue(undefined, "\r\n");
writer.writeStringValue(undefined, "--" + boundary + "--");
writer.writeStringValue(undefined, "\r\n");
};

export const deserializeIntoMultipartBody = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,16 @@ export class MultipartSerializationWriter implements SerializationWriter {
if (key) {
this.writeRawStringValue(key);
}
if (value !== undefined) {
if (value) {
if (key) {
this.writeRawStringValue(": ");
}
this.writeRawStringValue(value);
}
this.writeRawStringValue("\r\n");
};
private writeRawStringValue = (value?: string | null): void => {
if (value !== undefined) {
const isNullValue = value === null;

this.writeByteArrayValue(undefined, new TextEncoder().encode(isNullValue ? "null" : value).buffer);
if (value) {
this.writeByteArrayValue(undefined, new TextEncoder().encode(value).buffer);
}
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ describe("MultipartSerializationWriter", () => {
.join(""),
);
});
it("serializes multipart body with correct CRLF delimiters", () => {
const multipartSerializationWriter = new MultipartSerializationWriter();
const mpBody = new MultipartBody();
mpBody.addOrReplacePart("testPart", "text/plain", "test content");
mpBody.requestAdapter = {
getSerializationWriterFactory: () => new JsonSerializationWriterFactory(),
} as RequestAdapter;

multipartSerializationWriter.writeObjectValue(undefined, mpBody, serializeMultipartBody);
const multipartContent = multipartSerializationWriter.getSerializedContent();
const result = new TextDecoder().decode(multipartContent);

const expectedString = `--${mpBody.getBoundary()}\r\nContent-Type: text/plain\r\nContent-Disposition: form-data; name="testPart"\r\n\r\ntest content\r\n--${mpBody.getBoundary()}--\r\n`;
assert.equal(result, expectedString);
});
it("writes a structured object", () => {
const testEntity = {} as TestEntity;
testEntity.id = "48d31887-5fad-4d73-a9f5-3c356e68a038";
Expand Down

0 comments on commit 1925744

Please sign in to comment.