From 0d84c5d71ef17f6c761b2150b446c5b09810339c Mon Sep 17 00:00:00 2001 From: Ian Caffey Date: Fri, 25 Aug 2023 13:30:42 -0500 Subject: [PATCH] Support removing unused mixins --- .../transform/FlattenAndRemoveMixins.java | 2 +- .../model/transform/ModelTransformerTest.java | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/transform/FlattenAndRemoveMixins.java b/smithy-model/src/main/java/software/amazon/smithy/model/transform/FlattenAndRemoveMixins.java index 5aa3cfa5da4..ad790d78c3b 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/transform/FlattenAndRemoveMixins.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/transform/FlattenAndRemoveMixins.java @@ -38,7 +38,7 @@ Model transform(ModelTransformer transformer, Model model) { } } - if (!updatedShapes.isEmpty()) { + if (!updatedShapes.isEmpty() || !toRemove.isEmpty()) { Model.Builder builder = model.toBuilder(); updatedShapes.forEach(builder::addShape); // Don't use the removeShapes transform because that would further mutate shapes and remove the things diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/transform/ModelTransformerTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/transform/ModelTransformerTest.java index 3e54ff288ce..ee267e7b431 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/transform/ModelTransformerTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/transform/ModelTransformerTest.java @@ -145,6 +145,36 @@ public void canFilterAndRemoveMixinsWhenMixinsArePresent() { Matchers.equalTo(concrete.toBuilder().flattenMixins().build())); } + @Test + public void canFilterAndRemoveMixinsWhenUnusedMixinsArePresent() { + ModelTransformer transformer = ModelTransformer.create(); + Model.Builder builder = Model.builder(); + StringShape string = StringShape.builder().id("smithy.example#String").build(); + StructureShape mixin1 = StructureShape.builder() + .id("smithy.example#Mixin1") + .addTrait(MixinTrait.builder().build()) + .addMember("a", string.getId()) + .build(); + StructureShape mixin2 = StructureShape.builder() + .id("smithy.example#Mixin2") + .addMember("b", string.getId()) + .addTrait(MixinTrait.builder().build()) + .build(); + StructureShape mixin3 = StructureShape.builder() + .id("smithy.example#Mixin3") + .addMember("c", string.getId()) + .addTrait(MixinTrait.builder().build()) + .addMixin(mixin2) + .build(); + builder.addShapes(mixin1, mixin2, mixin3); + Model model = builder.build(); + Model result = transformer.flattenAndRemoveMixins(model); + + assertThat(result.toSet(), Matchers.not(Matchers.hasItem(mixin1))); + assertThat(result.toSet(), Matchers.not(Matchers.hasItem(mixin2))); + assertThat(result.toSet(), Matchers.not(Matchers.hasItem(mixin3))); + } + @ParameterizedTest @MethodSource("flattenShapesData") public void flattenShapes(String name) {