From ea9544b9558e85f56b272e2b6cc9223ddd91d625 Mon Sep 17 00:00:00 2001 From: fossand Date: Wed, 2 Aug 2023 10:59:45 -0700 Subject: [PATCH] Fix NPE when docId is null when ServiceTrait.equals is called --- .../smithy/aws/traits/ServiceTrait.java | 4 +--- .../smithy/aws/traits/ServiceTraitTest.java | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/ServiceTrait.java b/smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/ServiceTrait.java index 7d4c43afeed..d56938b5ad5 100644 --- a/smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/ServiceTrait.java +++ b/smithy-aws-traits/src/main/java/software/amazon/smithy/aws/traits/ServiceTrait.java @@ -232,9 +232,7 @@ public boolean equals(Object other) { && arnNamespace.equals(os.arnNamespace) && cloudFormationName.equals(os.cloudFormationName) && cloudTrailEventSource.equals(os.cloudTrailEventSource) - && docId == null - ? os.docId == null - : docId.equals(os.docId) + && Objects.equals(docId, os.docId) && endpointPrefix.equals(os.endpointPrefix); } } diff --git a/smithy-aws-traits/src/test/java/software/amazon/smithy/aws/traits/ServiceTraitTest.java b/smithy-aws-traits/src/test/java/software/amazon/smithy/aws/traits/ServiceTraitTest.java index 1d2e870c528..9743c4b9674 100644 --- a/smithy-aws-traits/src/test/java/software/amazon/smithy/aws/traits/ServiceTraitTest.java +++ b/smithy-aws-traits/src/test/java/software/amazon/smithy/aws/traits/ServiceTraitTest.java @@ -19,9 +19,9 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; - import java.util.Optional; import org.junit.jupiter.api.Test; import software.amazon.smithy.model.Model; @@ -126,4 +126,22 @@ public void loadsFromModel() { assertFalse(trait.getDocId().isPresent()); assertThat(trait.resolveDocId(service), equalTo("some-value-2018-03-17")); } + + @Test + public void equality() { + Node node1 = Node.parse("{\"sdkId\": \"Foo1\", \"arnNamespace\": \"service\", \"cloudFormationName\": \"Baz\", " + + "\"endpointPrefix\": \"endpoint-prefix\"}"); + + Node node2 = Node.parse("{\"sdkId\": \"Foo2\", \"arnNamespace\": \"service\", \"cloudFormationName\": \"Baz\", " + + "\"endpointPrefix\": \"endpoint-prefix\"}"); + + TraitFactory provider = TraitFactory.createServiceFactory(); + Optional trait1 = provider.createTrait(ServiceTrait.ID, ShapeId.from("ns.foo#foo1"), node1); + Optional trait2 = provider.createTrait(ServiceTrait.ID, ShapeId.from("ns.foo#foo2"), node2); + + ServiceTrait serviceTrait1 = (ServiceTrait) trait1.get(); + ServiceTrait serviceTrait2 = (ServiceTrait) trait2.get(); + + assertNotEquals(serviceTrait1, serviceTrait2); + } }