Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore valid set->list type change in diff #1383

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

import java.util.List;
import java.util.stream.Collectors;
import software.amazon.smithy.diff.ChangedShape;
import software.amazon.smithy.diff.Differences;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeType;
import software.amazon.smithy.model.traits.UniqueItemsTrait;
import software.amazon.smithy.model.validation.ValidationEvent;

/**
Expand All @@ -28,9 +32,23 @@ public final class ChangedShapeType extends AbstractDiffEvaluator {
public List<ValidationEvent> evaluate(Differences differences) {
return differences.changedShapes()
.filter(diff -> diff.getOldShape().getType() != diff.getNewShape().getType())
.filter(diff -> !expectedSetToListChange(diff))
.map(diff -> error(diff.getNewShape(), String.format(
"Shape `%s` type was changed from `%s` to `%s`.",
diff.getShapeId(), diff.getOldShape().getType(), diff.getNewShape().getType())))
.collect(Collectors.toList());
}

private boolean expectedSetToListChange(ChangedShape<Shape> diff) {
ShapeType oldType = diff.getOldShape().getType();
ShapeType newType = diff.getNewShape().getType();

// Smithy diff doesn't raise an issue if a set is changed to a list and the list
// has the uniqueItems trait. Set is deprecated and this is a recommended change.
if (oldType == ShapeType.SET && newType == ShapeType.LIST) {
return diff.getNewShape().hasTrait(UniqueItemsTrait.class);
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
package software.amazon.smithy.diff.evaluators;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;

import java.util.List;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.diff.ModelDiff;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.shapes.ModelSerializer;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.StringShape;
import software.amazon.smithy.model.shapes.TimestampShape;
Expand All @@ -40,4 +43,20 @@ public void detectsTypeChanges() {
assertThat(TestHelper.findEvents(events, "ChangedShapeType").size(), equalTo(1));
assertThat(TestHelper.findEvents(events, shapeA1.getId()).size(), equalTo(1));
}

@Test
public void ignoresExpectedSetToListMigration() {
String rawModel = "$version: \"1.0\"\nnamespace smithy.example\nset Foo { member: String }\n";
Model oldModel = Model.assembler().addUnparsedModel("example.smithy", rawModel)
.assemble().unwrap();
Node serialized = ModelSerializer.builder().build().serialize(oldModel);
Model newModel = Model.assembler()
.addDocumentNode(serialized)
.assemble()
.unwrap();

List<ValidationEvent> events = ModelDiff.compare(oldModel, newModel);

assertThat(TestHelper.findEvents(events, "ChangedShapeType"), empty());
}
}