Skip to content

Commit

Permalink
Modify ARN toString to print a valid ARN when there's no region or ac…
Browse files Browse the repository at this point in the history
…ountId (#4944)

* Modify ARN toString to print a valid ARN when there's no region or accountId

Fixes #2416

* Run  script

---------

Co-authored-by: Luis Madrigal 🐧 <lmadrig@amazon.com>
Co-authored-by: Anna-Karin Salander <salande@amazon.com>
  • Loading branch information
3 people committed Mar 7, 2024
1 parent 23bc04a commit 5b7ec50
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-51797eb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS SDK for Java v2",
"contributor": "Madrigal",
"description": "Modify ARN toString to print a valid ARN when there's no region or acountId"
}
4 changes: 2 additions & 2 deletions core/arns/src/main/java/software/amazon/awssdk/arns/Arn.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ public String toString() {
+ ":"
+ this.service
+ ":"
+ region
+ (region == null ? "" : region)
+ ":"
+ this.accountId
+ (this.accountId == null ? "" : this.accountId)
+ ":"
+ this.resource;
}
Expand Down
16 changes: 16 additions & 0 deletions core/arns/src/test/java/software/amazon/awssdk/arns/ArnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ public void arnWithQualifiedResource_ParsesBackToString() {
assertThat(arn.resourceAsString()).isEqualTo("myresource:foobar:1");
}

@Test
public void arnWithMinimalResources_ParsesBackToString() {
String arnString = "arn:aws:s3:::bucket";
Arn arn = Arn.fromString(arnString);
assertThat(arn.toString()).isEqualTo(arnString);
assertThat(arn.resourceAsString()).isEqualTo("bucket");
}

@Test
public void arnWithoutRegion_ParsesBackToString() {
String arnString = "arn:aws:iam::123456789012:root";
Arn arn = Arn.fromString(arnString);
assertThat(arn.toString()).isEqualTo(arnString);
assertThat(arn.resourceAsString()).isEqualTo("root");
}

@Test
public void arnWithResourceTypeAndResource_ParsesCorrectly() {
String arnString = "arn:aws:s3:us-east-1:12345678910:bucket:foobar";
Expand Down

0 comments on commit 5b7ec50

Please sign in to comment.