Skip to content

Commit

Permalink
fix: Prevent NPE when bucket doesn't exist #857 (#860)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
lbergelson committed Mar 17, 2022
1 parent d6b7b5e commit 69cab9e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Expand Up @@ -963,8 +963,13 @@ public String toString() {
public boolean requesterPays(String bucketName) {
initStorage();
try {
// instead of true/false, this method returns true/null.
Boolean isRP = storage.get(bucketName).requesterPays();
final Bucket bucket = storage.get(bucketName);
// If the bucket doesn't exist it can't be requester pays.
if (bucket == null) {
return false;
}
// instead of true/false, this method returns true/null
Boolean isRP = bucket.requesterPays();
return isRP != null && isRP.booleanValue();
} catch (StorageException ex) {
if ("userProjectMissing".equals(ex.getReason())) {
Expand Down
Expand Up @@ -328,7 +328,26 @@ public void testAutodetectWhenNotRequesterPays() throws IOException {
}

@Test
public void testFilesExistDoesntCrashWhenRequesterPays() throws IOException {
public void testRequesterPaysOnNonexistentBucket() {
CloudStorageConfiguration config =
CloudStorageConfiguration.builder()
.autoDetectRequesterPays(true)
.userProject(project)
.usePseudoDirectories(true)
.build();

final String bucketThatDoesntExist = "abuckethatdoesntexist";
final String subPath = "hello";
CloudStorageFileSystem testBucket =
CloudStorageFileSystem.forBucket(bucketThatDoesntExist, config, storageOptions);
final CloudStoragePath aPathThatDoesntExist = testBucket.getPath(subPath);
Assert.assertEquals(
aPathThatDoesntExist.toUri().toString(), "gs://" + bucketThatDoesntExist + "/" + subPath);
Assert.assertFalse(testBucket.provider().requesterPays(bucketThatDoesntExist));
}

@Test
public void testFilesExistBehaviorRequesterPays() {
CloudStorageConfiguration config =
CloudStorageConfiguration.builder()
.autoDetectRequesterPays(true)
Expand Down

0 comments on commit 69cab9e

Please sign in to comment.