Skip to content

Commit dddff8d

Browse files
authored
fix: Prevent NPE when bucket doesn't exist #857 (#860)
Fixes a NullPointerException when creating a Path object with a bucket that doesn't exist. This only occurred when autoDetectRequesterPays = true. Refs: #857 Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-storage-nio/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes #857☕️ If you write sample code, please follow the [samples format]( https://github.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md).
1 parent 3f92f0c commit dddff8d

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,8 +963,13 @@ public String toString() {
963963
public boolean requesterPays(String bucketName) {
964964
initStorage();
965965
try {
966-
// instead of true/false, this method returns true/null.
967-
Boolean isRP = storage.get(bucketName).requesterPays();
966+
final Bucket bucket = storage.get(bucketName);
967+
// If the bucket doesn't exist it can't be requester pays.
968+
if (bucket == null) {
969+
return false;
970+
}
971+
// instead of true/false, this method returns true/null
972+
Boolean isRP = bucket.requesterPays();
968973
return isRP != null && isRP.booleanValue();
969974
} catch (StorageException ex) {
970975
if ("userProjectMissing".equals(ex.getReason())) {

java-storage-nio/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,26 @@ public void testAutodetectWhenNotRequesterPays() throws IOException {
328328
}
329329

330330
@Test
331-
public void testFilesExistDoesntCrashWhenRequesterPays() throws IOException {
331+
public void testRequesterPaysOnNonexistentBucket() {
332+
CloudStorageConfiguration config =
333+
CloudStorageConfiguration.builder()
334+
.autoDetectRequesterPays(true)
335+
.userProject(project)
336+
.usePseudoDirectories(true)
337+
.build();
338+
339+
final String bucketThatDoesntExist = "abuckethatdoesntexist";
340+
final String subPath = "hello";
341+
CloudStorageFileSystem testBucket =
342+
CloudStorageFileSystem.forBucket(bucketThatDoesntExist, config, storageOptions);
343+
final CloudStoragePath aPathThatDoesntExist = testBucket.getPath(subPath);
344+
Assert.assertEquals(
345+
aPathThatDoesntExist.toUri().toString(), "gs://" + bucketThatDoesntExist + "/" + subPath);
346+
Assert.assertFalse(testBucket.provider().requesterPays(bucketThatDoesntExist));
347+
}
348+
349+
@Test
350+
public void testFilesExistBehaviorRequesterPays() {
332351
CloudStorageConfiguration config =
333352
CloudStorageConfiguration.builder()
334353
.autoDetectRequesterPays(true)

0 commit comments

Comments
 (0)