Skip to content

Commit 5c4ccff

Browse files
Avoids NullPointerException when no config nor credentials files are found in ~/.aws/ and ProfileFile.profiles() method is called (#3942)
Co-authored-by: Matthew Miller <[email protected]>
1 parent 4dd34b1 commit 5c4ccff

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"category": "AWS SDK for Java v2",
3+
"contributor": "martinKindall",
4+
"type": "bugfix",
5+
"description": "Avoids NullPointerException when config and credentials files were not found for profiles. Now it returns an empty map, as it used to."
6+
}

core/profiles/src/main/java/software/amazon/awssdk/profiles/ProfileFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public Optional<Profile> profile(String profileName) {
117117
*/
118118
public Map<String, Profile> profiles() {
119119
Map<String, Profile> profileMap = profilesAndSectionsMap.get(PROFILES_SECTION_TITLE);
120-
return profileMap != null ? Collections.unmodifiableMap(profileMap) : profileMap;
120+
return profileMap != null ? Collections.unmodifiableMap(profileMap) : Collections.emptyMap();
121121
}
122122

123123
@Override

core/profiles/src/test/java/software/amazon/awssdk/profiles/ProfileFileTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,15 @@ public void loadingDefaultProfileFileWorks() {
554554
ProfileFile.defaultProfileFile();
555555
}
556556

557+
@Test
558+
public void returnsEmptyMap_when_AwsFilesDoNotExist() {
559+
ProfileFile missingProfile = ProfileFile.aggregator()
560+
.build();
561+
562+
assertThat(missingProfile.profiles()).isEmpty();
563+
assertThat(missingProfile.profiles()).isInstanceOf(Map.class);
564+
}
565+
557566
private ProfileFile configFile(String configFile) {
558567
return ProfileFile.builder()
559568
.content(new StringInputStream(configFile))

0 commit comments

Comments
 (0)