Skip to content

Commit cadf081

Browse files
authored
fix: check generation for nullability before incrementing it (#888)
1 parent 34e1c59 commit cadf081

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpc.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,10 @@ public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws Storage
413413
// if this is a new file, set generation to 1, else increment the existing generation
414414
long generation = 1;
415415
if (metadata.containsKey(destKey)) {
416-
generation = metadata.get(destKey).getGeneration() + 1;
416+
Long storedGeneration = metadata.get(destKey).getGeneration();
417+
if (null != storedGeneration) {
418+
generation = storedGeneration + 1;
419+
}
417420
}
418421

419422
checkGeneration(destKey, generationMatch);

google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelperTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,28 @@ public void testStorageOptionIsSerializable_customOptions() throws Exception {
156156
assertThat(ois.readObject()).isEqualTo(storageOptions);
157157
}
158158
}
159+
160+
@Test
161+
public void testCopyOperationOverwritesExistingFile() {
162+
String bucket = "bucket";
163+
String original = "original";
164+
String replacement = "replacement";
165+
byte[] originalContent = "original content".getBytes();
166+
byte[] replacementContent = "replacement content".getBytes();
167+
168+
localStorageService.create(BlobInfo.newBuilder(bucket, original).build(), originalContent);
169+
localStorageService.create(
170+
BlobInfo.newBuilder(bucket, replacement).build(), replacementContent);
171+
172+
final Storage.CopyRequest request =
173+
Storage.CopyRequest.newBuilder()
174+
.setSource(BlobId.of(bucket, replacement))
175+
.setTarget(BlobId.of(bucket, original))
176+
.build();
177+
178+
localStorageService.copy(request).getResult();
179+
180+
assertThat(localStorageService.readAllBytes(BlobId.of(bucket, original)))
181+
.isEqualTo(replacementContent);
182+
}
159183
}

0 commit comments

Comments
 (0)