Emit no rel from enum/intEnum to Unit

This commit is contained in:
Michael Dowling 2022-08-04 15:36:46 -07:00 committed by Michael Dowling
parent 53c7c77021
commit 89e292edff
3 changed files with 46 additions and 0 deletions

View File

@ -206,6 +206,13 @@ final class NeighborVisitor extends ShapeVisitor.Default<List<Relationship>> imp
@Override
public List<Relationship> memberShape(MemberShape shape) {
Shape container = model.getShape(shape.getContainer()).orElse(null);
// Don't emit relationships from enum/intEnum to the Unit shape.
if (container != null && (container.isEnumShape() || container.isIntEnumShape())) {
return Collections.emptyList();
}
List<Relationship> result = initializeRelationships(shape, 2);
result.add(relationship(shape, RelationshipType.MEMBER_CONTAINER, shape.getContainer()));
result.add(relationship(shape, RelationshipType.MEMBER_TARGET, shape.getTarget()));

View File

@ -322,6 +322,11 @@ public final class EnumShape extends StringShape {
return this;
}
@Override
public Builder id(String id) {
return (Builder) super.id(id);
}
/**
* Sets enum members from an {@link EnumTrait}.
*

View File

@ -19,6 +19,8 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import java.util.List;
import java.util.stream.Collectors;
@ -41,11 +43,13 @@ import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.shapes.TimestampShape;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.DeprecatedTrait;
import software.amazon.smithy.model.traits.EnumValueTrait;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.model.traits.IdempotentTrait;
import software.amazon.smithy.model.traits.MixinTrait;
import software.amazon.smithy.model.traits.ReadonlyTrait;
import software.amazon.smithy.model.traits.SensitiveTrait;
import software.amazon.smithy.model.traits.UnitTypeTrait;
public class NeighborVisitorTest {
@ -439,6 +443,36 @@ public class NeighborVisitorTest {
Relationship.create(method, RelationshipType.ERROR, error)));
}
@Test
public void operationShapeDoesNotEmitUnitRelationships() {
OperationShape method = OperationShape.builder().id("ns.foo#Foo").build();
Model model = Model.builder().addShapes(method).build();
NeighborVisitor neighborVisitor = new NeighborVisitor(model);
List<Relationship> relationships = method.accept(neighborVisitor);
assertThat(relationships, empty());
}
@Test
public void unitTypeRelsNotEmittedFromEnums() {
MemberShape member = MemberShape.builder()
.id("smithy.api#Example$foo")
.target(UnitTypeTrait.UNIT)
.addTrait(EnumValueTrait.builder().stringValue("hi").build())
.build();
EnumShape enumShape = EnumShape.builder()
.id("smithy.api#Example")
.addMember(member)
.build();
Model model = Model.builder().addShapes(enumShape).build();
NeighborVisitor neighborVisitor = new NeighborVisitor(model);
List<Relationship> relationships = enumShape.accept(neighborVisitor);
assertThat(relationships, hasSize(1));
assertThat(relationships.get(0), equalTo(Relationship.create(enumShape, RelationshipType.ENUM_MEMBER, member)));
}
@Test
public void memberShape() {
StringShape string = StringShape.builder()