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

Flag enum insertions as errors #873

Merged
merged 2 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -29,8 +29,9 @@
import software.amazon.smithy.utils.Pair;

/**
* Emits a NOTE when a new enum value is added, emits an ERROR when an
* enum value is removed, and emits an ERROR when an enum name changes.
* Emits a NOTE when a new enum value is appended, emits an ERROR when an
* enum value is removed, emits an ERROR when an enum name changes, and
* emits an ERROR when a new enum value is inserted.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throughout this PR, can we be more specific about what inserted means? We mean that an enum value was added but not at the end of the list of enum values. I think clarifying with this little tweak will save countless hours of teams not understanding the error.

*/
public final class ChangedEnumTrait extends AbstractDiffEvaluator {
@Override
Expand All @@ -46,6 +47,7 @@ private List<ValidationEvent> validateEnum(ChangedShape<Shape> change, Pair<Enum
EnumTrait oldTrait = trait.getLeft();
EnumTrait newTrait = trait.getRight();
List<ValidationEvent> events = new ArrayList<>();
int oldEndPosition = oldTrait.getValues().size() - 1;

for (EnumDefinition definition : oldTrait.getValues()) {
Optional<EnumDefinition> maybeNewValue = newTrait.getValues().stream()
Expand All @@ -55,6 +57,7 @@ private List<ValidationEvent> validateEnum(ChangedShape<Shape> change, Pair<Enum
if (!maybeNewValue.isPresent()) {
events.add(error(change.getNewShape(), String.format(
"Enum value `%s` was removed", definition.getValue())));
oldEndPosition--;
} else {
EnumDefinition newValue = maybeNewValue.get();
if (!newValue.getName().equals(definition.getName())) {
Expand All @@ -67,11 +70,20 @@ private List<ValidationEvent> validateEnum(ChangedShape<Shape> change, Pair<Enum
}
}

int newPosition = 0;
for (EnumDefinition definition : newTrait.getValues()) {
if (!oldTrait.getEnumDefinitionValues().contains(definition.getValue())) {
events.add(note(change.getNewShape(), String.format(
"Enum value `%s` was added", definition.getValue())));
if (newPosition <= oldEndPosition) {
events.add(error(change.getNewShape(), String.format(
"Enum value `%s` was inserted. This can cause compatibility issues when ordinal values are "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the extra bit of context here about why this matters! Can you say something like "Enum value %s was inserted, but not at the end of the list of existing enum values. [...]" or something to that effect?

+ "used for iteration, serialization, etc.", definition.getValue())));
} else {
events.add(note(change.getNewShape(), String.format(
"Enum value `%s` was appended", definition.getValue())));
}
}

newPosition++;
}

return events;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

import java.util.List;
import org.junit.jupiter.api.Test;
Expand All @@ -30,7 +31,7 @@

public class ChangedEnumTraitTest {
@Test
public void detectsAddedEnums() {
public void detectsAppendedEnums() {
StringShape s1 = StringShape.builder()
.id("foo.baz#Baz")
.addTrait(EnumTrait.builder()
Expand Down Expand Up @@ -96,4 +97,61 @@ public void detectsRenamedEnums() {
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").size(), equalTo(1));
assertThat(TestHelper.findEvents(events, Severity.ERROR).size(), equalTo(1));
}

@Test
public void detectsInsertedEnums() {
StringShape s1 = StringShape.builder()
.id("foo.baz#Baz")
.addTrait(EnumTrait.builder()
.addEnum(EnumDefinition.builder().value("foo").build())
.build())
.build();
StringShape s2 = StringShape.builder()
.id("foo.baz#Baz")
.addTrait(EnumTrait.builder()
.addEnum(EnumDefinition.builder().value("baz").build())
.addEnum(EnumDefinition.builder().value("foo").build())
.build())
.build();
Model modelA = Model.assembler().addShape(s1).assemble().unwrap();
Model modelB = Model.assembler().addShape(s2).assemble().unwrap();
List<ValidationEvent> events = ModelDiff.compare(modelA, modelB);

assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").size(), equalTo(1));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").get(0).getSeverity(), equalTo(Severity.ERROR));
}

@Test
public void detectsAppendedEnumsAfterRemovedEnums() {
StringShape s1 = StringShape.builder()
.id("foo.baz#Baz")
.addTrait(EnumTrait.builder()
.addEnum(EnumDefinition.builder().value("old1").build())
.addEnum(EnumDefinition.builder().value("old2").build())
.addEnum(EnumDefinition.builder().value("old3").build())
.build())
.build();
StringShape s2 = StringShape.builder()
.id("foo.baz#Baz")
.addTrait(EnumTrait.builder()
.addEnum(EnumDefinition.builder().value("old1").build())
.addEnum(EnumDefinition.builder().value("old3").build())
.addEnum(EnumDefinition.builder().value("new1").build())
.build())
.build();
Model modelA = Model.assembler().addShape(s1).assemble().unwrap();
Model modelB = Model.assembler().addShape(s2).assemble().unwrap();
List<ValidationEvent> allEvents = ModelDiff.compare(modelA, modelB);

List<ValidationEvent> changeEvents = TestHelper.findEvents(allEvents, "ChangedEnumTrait");
assertThat(changeEvents.size(), equalTo(2));

ValidationEvent removedEvent = changeEvents.get(0);
assertThat(removedEvent.getSeverity(), equalTo(Severity.ERROR));
assertThat(removedEvent.getMessage(), stringContainsInOrder("Enum value `old2` was removed"));

ValidationEvent appendedEvent = changeEvents.get(1);
assertThat(appendedEvent.getSeverity(), equalTo(Severity.NOTE));
assertThat(appendedEvent.getMessage(), stringContainsInOrder("Enum value `new1` was appended"));
}
}