Skip to content

Commit

Permalink
Updated #copy() to convert null custom metadata values to empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
rhodgkins committed Aug 11, 2021
1 parent 0c6d54d commit 33febe2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
35 changes: 35 additions & 0 deletions scripts/storage-emulator-integration/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,41 @@ describe("Storage emulator", () => {
expect(metadata2).to.deep.equal(metadata1);
});

it("should set null custom metadata values to empty strings", async () => {
const [, source] = await testBucket.upload(smallFilePath);

const file = testBucket.file(copyDestinationFile);
const metadata = { foo: "bar", nullMetadata: null };
const cacheControl = "private,max-age=10,immutable";
// Types for CopyOptions are wrong (@google-cloud/storage sub-dependency needs
// update to include https://github.com/googleapis/nodejs-storage/pull/1406
// and https://github.com/googleapis/nodejs-storage/pull/1426)
const copyOpts: CopyOptions & { [key: string]: unknown } = {
metadata,
cacheControl,
};
const [, { resource: metadata1 }] = await testBucket
.file(smallFilePath.split("/").slice(-1)[0])
.copy(file, copyOpts);

expect(metadata1).to.deep.include({
bucket: source.bucket,
contentType: source.contentType,
etag: source.etag,
crc32c: source.crc32c,
metadata: {
foo: "bar",
// Sets null metadata values to empty strings
nullMetadata: "",
},
cacheControl,
});

// Also double check with a new metadata fetch
const [metadata2] = await file.getMetadata();
expect(metadata2).to.deep.equal(metadata1);
});

it("should not support the use of a rewriteToken", async () => {
await testBucket.upload(smallFilePath);

Expand Down
7 changes: 7 additions & 0 deletions src/emulator/storage/apis/gcloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@ export function createCloudEndpoints(emulator: StorageEmulator): Router {
metadata: md.customMetadata,
...req.body,
};
if (newMetadata.metadata) {
// Convert null metadata values to empty strings
for (const k of Object.keys(newMetadata.metadata)) {
const v = newMetadata.metadata[k];
if (v === null) newMetadata.metadata[k] = "";
}
}
const metadata = storageLayer.oneShotUpload(
req.params.destBucketId,
req.params.destObjectId,
Expand Down

0 comments on commit 33febe2

Please sign in to comment.