From 55d9141da907d3f74c455492e29f4d1ad937c292 Mon Sep 17 00:00:00 2001 From: fossand Date: Wed, 15 Feb 2023 12:04:48 -0800 Subject: [PATCH] Fix OR condition in scoped attribute selector --- .../selector/ScopedAttributeSelector.java | 10 ++++++++-- .../smithy/model/selector/SelectorTest.java | 18 ++++++++++++++++++ .../amazon/smithy/model/selector/enums.smithy | 17 +++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 smithy-model/src/test/resources/software/amazon/smithy/model/selector/enums.smithy diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/selector/ScopedAttributeSelector.java b/smithy-model/src/main/java/software/amazon/smithy/model/selector/ScopedAttributeSelector.java index 09f406b2cb7..88dc6867aba 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/selector/ScopedAttributeSelector.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/selector/ScopedAttributeSelector.java @@ -96,12 +96,18 @@ private boolean compareWithScope(AttributeValue scope) { // Ensure that each assertion matches, and provide them the scope. for (Assertion assertion : assertions) { AttributeValue lhs = assertion.lhs.create(scope); + boolean matchedOneRhs = false; for (ScopedFactory factory : assertion.rhs) { AttributeValue rhs = factory.create(scope); - if (!assertion.comparator.compare(lhs, rhs, assertion.caseInsensitive)) { - return false; + if (assertion.comparator.compare(lhs, rhs, assertion.caseInsensitive)) { + matchedOneRhs = true; + break; } } + + if (!matchedOneRhs) { + return false; + } } return true; diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/selector/SelectorTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/selector/SelectorTest.java index a5e6a8bd198..ecd999bdb51 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/selector/SelectorTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/selector/SelectorTest.java @@ -736,6 +736,24 @@ public void evaluatesScopedAttributes() { assertThat(shapes2, equalTo(shapes1)); } + @Test + public void evaluatesScopedAttributesWithOr() { + final Model enumModel = Model.assembler() + .addImport(SelectorTest.class.getResource("enums.smithy")) + .assemble() + .unwrap(); + + Set shapes = ids( + enumModel, "[@trait|enum|(values): @{name} ^= DIA, CLU]"); + + assertThat(shapes, containsInAnyOrder("smithy.example#Suit")); + + shapes = ids( + enumModel, "[@trait|enum|(values): @{name} ^= DIA, BLA]"); + + assertThat(shapes, containsInAnyOrder("smithy.example#Color", "smithy.example#Suit")); + } + @Test public void evaluatesScopedAttributesWithProjections() { // Note that the projection can be on either side. diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/selector/enums.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/selector/enums.smithy new file mode 100644 index 00000000000..0c878aa8eae --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/selector/enums.smithy @@ -0,0 +1,17 @@ +$version: "1.0" + +namespace smithy.example + +@enum([ + { name: "DIAMOND", value: "diamond"}, + { name: "CLUB", value: "club"}, + { name: "HEART", value: "heart"}, + { name: "SPADE", value: "spade"}, +]) +string Suit + +@enum([ + { name: "RED", value: "red"} + { name: "BLACK", value: "black"} +]) +string Color