diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/EnumShape.java b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/EnumShape.java index ef3fc22c8fd..95f8d7666a5 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/EnumShape.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/EnumShape.java @@ -54,6 +54,15 @@ private EnumShape(Builder builder) { } } + private EnumShape(Builder builder, Map members) { + super(builder); + this.members = members; + validateMemberShapeIds(); + if (members.size() < 1) { + throw new SourceException("enum shapes must have at least one member", getSourceLocation()); + } + } + /** * Gets a map of enum member names to their corresponding values. * @@ -291,8 +300,11 @@ public static final class Builder extends StringShape.Builder { @Override public EnumShape build() { - addSyntheticEnumTrait(); - return new EnumShape(this); + // Collect members from enum and mixins + Map aggregatedMembers = + NamedMemberUtils.computeMixinMembers(getMixins(), members, getId(), getSourceLocation()); + addSyntheticEnumTrait(aggregatedMembers.values()); + return new EnumShape(this, aggregatedMembers); } /** @@ -302,10 +314,10 @@ public EnumShape build() { * the enum trait, without having to manually add the trait or risk that it * gets serialized. */ - private void addSyntheticEnumTrait() { + private void addSyntheticEnumTrait(Collection memberShapes) { SyntheticEnumTrait.Builder builder = SyntheticEnumTrait.builder(); builder.sourceLocation(getSourceLocation()); - for (MemberShape member : members.get().values()) { + for (MemberShape member : memberShapes) { try { builder.addEnum(EnumShape.enumDefinitionFromMember(member)); } catch (IllegalStateException e) { diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/mixins/empty-enum-mixin.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/mixins/empty-enum-mixin.errors new file mode 100644 index 00000000000..66a2d03bb59 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/mixins/empty-enum-mixin.errors @@ -0,0 +1,2 @@ +[ERROR] smithy.example#EmptyEnumMixin: enum must have at least one entry | Model +[ERROR] smithy.example#EmptyEnum: Cannot apply mixin to smithy.example#EmptyEnum: smithy.example#EmptyEnumMixin not found | Model diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/mixins/empty-enum-mixin.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/mixins/empty-enum-mixin.smithy new file mode 100644 index 00000000000..3dccd5deb19 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/mixins/empty-enum-mixin.smithy @@ -0,0 +1,8 @@ +$version: "2.0" + +namespace smithy.example + +@mixin +enum EmptyEnumMixin {} + +enum EmptyEnum with [EmptyEnumMixin] {} diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/mixins/non-empty-enum-mixin.errors b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/mixins/non-empty-enum-mixin.errors new file mode 100644 index 00000000000..e69de29bb2d diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/mixins/non-empty-enum-mixin.smithy b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/mixins/non-empty-enum-mixin.smithy new file mode 100644 index 00000000000..12862960474 --- /dev/null +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/errorfiles/validators/mixins/non-empty-enum-mixin.smithy @@ -0,0 +1,10 @@ +$version: "2.0" + +namespace smithy.example + +@mixin +enum NonEmptyEnumMixin { + VALUE = "value" +} + +enum NonEmptyEnum with [NonEmptyEnumMixin] {}