Skip to content

Commit

Permalink
Uploading a file without a description crashes the SDK (#264)
Browse files Browse the repository at this point in the history
* test: add negative test

* fix: allow having null / undefined values in multipart requests
  • Loading branch information
jkoenig134 authored Sep 10, 2024
1 parent 4a88c19 commit 436669e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/sdk/src/endpoints/Endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ export abstract class Endpoint {

const value = data[key];

if (typeof value === "undefined") continue;

if (value instanceof Buffer) {
formData.append(key, value, { filename });
} else {
Expand Down
14 changes: 13 additions & 1 deletion test/files.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ConnectorClient, ConnectorFile } from "@nmshd/connector-sdk";
import fs from "fs";
import { DateTime } from "luxon";
import { Launcher } from "./lib/Launcher";
import { QueryParamConditions } from "./lib/QueryParamConditions";
import { getTimeout } from "./lib/setTimeout";
Expand Down Expand Up @@ -27,6 +28,17 @@ describe("File Upload", () => {
file = response.result;
});

test("can upload file without description", async () => {
const response = await client1.files.uploadOwnFile({
title: "File Title",
filename: "test.txt",
file: await fs.promises.readFile(`${__dirname}/__assets__/test.txt`),
expiresAt: DateTime.utc().plus({ minutes: 5 }).toString()
});

expect(response).toBeSuccessful(ValidationSchema.File);
});

test("uploaded files can be accessed under /Files", async () => {
expect(file).toBeDefined();

Expand Down Expand Up @@ -65,7 +77,7 @@ describe("File Upload", () => {

test("cannot upload a file that is null", async () => {
// Cannot use client1.files.uploadOwn because it cannot deal with null values
const _response = await (client1.files as any).httpClient.post("/api/v2/Files/Own", makeUploadRequest({ file: null }));
const _response = await (client1.files as any).httpClient.post("/api/v2/Files/Own", makeUploadRequest({ file: null as any }));
const response = (client1.files as any).makeResult(_response);

expect(response).toBeAnError("must have required property 'content'", "error.runtime.validation.invalidPropertyValue");
Expand Down
2 changes: 1 addition & 1 deletion test/lib/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export async function uploadFile(client: ConnectorClient): Promise<ConnectorFile
return response.result;
}

export async function makeUploadRequest(values: object = {}): Promise<UploadOwnFileRequest> {
export async function makeUploadRequest(values: Partial<UploadOwnFileRequest> = {}): Promise<UploadOwnFileRequest> {
return {
title: "File Title",
filename: "test.txt",
Expand Down

0 comments on commit 436669e

Please sign in to comment.