diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/knowledge/NeighborProviderIndex.java b/smithy-model/src/main/java/software/amazon/smithy/model/knowledge/NeighborProviderIndex.java index acc7a58eaac..40901019732 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/knowledge/NeighborProviderIndex.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/knowledge/NeighborProviderIndex.java @@ -32,7 +32,6 @@ public final class NeighborProviderIndex implements KnowledgeIndex { private volatile NeighborProvider reversed; private volatile NeighborProvider providerWithTraits; private volatile NeighborProvider reversedWithTraits; - private volatile NeighborProvider providerWithIdRefs; public NeighborProviderIndex(Model model) { provider = NeighborProvider.precomputed(model); @@ -77,28 +76,6 @@ public NeighborProvider getProviderWithTraitRelationships() { return result; } - /** - * Gets the neighbor provider that includes idRef relationships. - * - * @return Returns the provider. - */ - public NeighborProvider getProviderWithIdRefRelationships() { - NeighborProvider result = providerWithIdRefs; - - if (result == null) { - Model model = getOrThrowModel(); - synchronized (this) { - result = providerWithIdRefs; - if (result == null) { - providerWithIdRefs = result = NeighborProvider.cached( - NeighborProvider.withIdRefRelationships(model, provider)); - } - } - } - - return result; - } - /** * Gets a reversed, bottom up neighbor provider. * diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/neighbor/NodeQuery.java b/smithy-model/src/main/java/software/amazon/smithy/model/neighbor/NodeQuery.java index b96055d6126..97ba84d3bad 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/neighbor/NodeQuery.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/neighbor/NodeQuery.java @@ -18,13 +18,36 @@ * and the results are aggregated. */ final class NodeQuery { + private static final Query SELF = Stream::of; + + private static final Query ANY_MEMBER = (node) -> { + if (node == null || !node.isObjectNode()) { + return Stream.empty(); + } + return node.expectObjectNode().getMembers().values().stream(); + }; + + private static final Query ANY_ELEMENT = (node) -> { + if (node == null || !node.isArrayNode()) { + return Stream.empty(); + } + return node.expectArrayNode().getElements().stream(); + }; + + private static final Query ANY_MEMBER_NAME = (node) -> { + if (node == null || !node.isObjectNode()) { + return Stream.empty(); + } + return node.expectObjectNode().getMembers().keySet().stream(); + }; + private final List queries = new ArrayList<>(); NodeQuery() { } NodeQuery self() { - queries.add(Stream::of); + queries.add(SELF); return this; } @@ -39,32 +62,17 @@ NodeQuery member(String name) { } NodeQuery anyMember() { - queries.add(node -> { - if (node == null || !node.isObjectNode()) { - return Stream.empty(); - } - return node.expectObjectNode().getMembers().values().stream(); - }); + queries.add(ANY_MEMBER); return this; } NodeQuery anyElement() { - queries.add(node -> { - if (node == null || !node.isArrayNode()) { - return Stream.empty(); - } - return node.expectArrayNode().getElements().stream(); - }); + queries.add(ANY_ELEMENT); return this; } NodeQuery anyMemberName() { - queries.add(node -> { - if (node == null || !node.isObjectNode()) { - return Stream.empty(); - } - return node.expectObjectNode().getMembers().keySet().stream(); - }); + queries.add(ANY_MEMBER_NAME); return this; } diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/neighbor/UnreferencedShapes.java b/smithy-model/src/main/java/software/amazon/smithy/model/neighbor/UnreferencedShapes.java index 3455d116435..e412f885a48 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/neighbor/UnreferencedShapes.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/neighbor/UnreferencedShapes.java @@ -55,7 +55,9 @@ public UnreferencedShapes(Predicate keepFilter) { * @return Returns the unreferenced shapes. */ public Set compute(Model model) { - Walker shapeWalker = new Walker(NeighborProviderIndex.of(model).getProviderWithIdRefRelationships()); + NeighborProvider baseProvider = NeighborProviderIndex.of(model).getProvider(); + NeighborProvider providerWithIdRefRelationships = NeighborProvider.withIdRefRelationships(model, baseProvider); + Walker shapeWalker = new Walker(providerWithIdRefRelationships); // Find all shapes connected to any service shape. Set connected = new HashSet<>(); diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/neighbor/NeighborProviderTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/neighbor/NeighborProviderTest.java index 9bb9c49e6df..15162e5a240 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/neighbor/NeighborProviderTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/neighbor/NeighborProviderTest.java @@ -97,18 +97,14 @@ public void canGetIdRefRelationshipsThroughTraitDefs() { List relationships = provider.getNeighbors(shape).stream() .filter(relationship -> relationship.getRelationshipType().equals(RelationshipType.ID_REF)) .collect(Collectors.toList()); - - assertThat(relationships, containsInAnyOrder( - equalTo(Relationship.create(shape, RelationshipType.ID_REF, ref)))); - Shape shape1 = model.expectShape(ShapeId.from("com.foo#refStruct$other")); Shape ref1 = model.expectShape(ShapeId.from("com.foo#ReferencedInTraitDef")); List relationships1 = provider.getNeighbors(shape1).stream() .filter(relationship -> relationship.getRelationshipType().equals(RelationshipType.ID_REF)) .collect(Collectors.toList()); - assertThat(relationships1, containsInAnyOrder( - equalTo(Relationship.create(shape1, RelationshipType.ID_REF, ref1)))); + assertThat(relationships, containsInAnyOrder(Relationship.create(shape, RelationshipType.ID_REF, ref))); + assertThat(relationships1, containsInAnyOrder(Relationship.create(shape1, RelationshipType.ID_REF, ref1))); } @Test @@ -125,17 +121,13 @@ public void canGetIdRefRelationshipsThroughMultipleLevelsOfIdRef() { List relationships = provider.getNeighbors(shape).stream() .filter(relationship -> relationship.getRelationshipType().equals(RelationshipType.ID_REF)) .collect(Collectors.toList()); - - assertThat(relationships, containsInAnyOrder( - Relationship.create(shape, RelationshipType.ID_REF, ref))); - Shape shape1 = model.expectShape(ShapeId.from("com.foo#ConnectedThroughReferenced")); Shape ref1 = model.expectShape(ShapeId.from("com.foo#AnotherReferenced")); List relationships1 = provider.getNeighbors(shape1).stream() .filter(relationship -> relationship.getRelationshipType().equals(RelationshipType.ID_REF)) .collect(Collectors.toList()); - assertThat(relationships1, containsInAnyOrder( - Relationship.create(shape1, RelationshipType.ID_REF, ref1))); + assertThat(relationships, containsInAnyOrder(Relationship.create(shape, RelationshipType.ID_REF, ref))); + assertThat(relationships1, containsInAnyOrder(Relationship.create(shape1, RelationshipType.ID_REF, ref1))); } } diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/neighbor/UnreferencedShapesTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/neighbor/UnreferencedShapesTest.java index 382baf327b9..7164cedd623 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/neighbor/UnreferencedShapesTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/neighbor/UnreferencedShapesTest.java @@ -89,6 +89,7 @@ public void checksShapeReferencesThroughIdRef() { .unwrap(); Set shapes = new UnreferencedShapes().compute(m); + assertThat(shapes, empty()); } @@ -100,6 +101,7 @@ public void doesNotCheckShapeReferencesThroughIdRefOnUnconnectedShapes() { .unwrap(); Set ids = new UnreferencedShapes().compute(m).stream().map(Shape::getId).collect(Collectors.toSet()); + assertThat(ids, containsInAnyOrder( ShapeId.from("com.foo#WithTrait"), ShapeId.from("com.foo#Referenced"), diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/neighbor/idref-neighbors.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/neighbor/idref-neighbors.smithy index f5b9e119f52..832224af3cb 100644 --- a/smithy-model/src/test/resources/software/amazon/smithy/model/neighbor/idref-neighbors.smithy +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/neighbor/idref-neighbors.smithy @@ -23,6 +23,7 @@ operation GetFoo { twelve: Twelve thirteen: Thirteen fourteen: Fourteen + fifteen: Fifteen } } @@ -218,3 +219,19 @@ structure Fourteen {} string Ref14 +// -- +@trait +structure withIdRefOnEnum { + refEnum: RefEnum +} + +@idRef(failWhenMissing: true) +enum RefEnum { + REF15 = "com.foo#Ref15" +} + +@withIdRefOnEnum(refEnum: "com.foo#Ref15") +structure Fifteen {} + +structure Ref15 {} +