Skip to content

Commit

Permalink
Fix issue where we emitted events twice
Browse files Browse the repository at this point in the history
ModelAssembler event listeners were receiving events twice in some cases,
causing events to appear twice when running the CLI/Gradle/etc.
  • Loading branch information
mtdowling authored and Michael Dowling committed Aug 18, 2022
1 parent ecbb83d commit edfd6cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.SourceException;
import software.amazon.smithy.model.SourceLocation;
Expand Down Expand Up @@ -194,16 +193,20 @@ public Validator createValidator() {
return coreEvents;
}

Stream<ValidationEvent> eventStream = Stream.concat(
includeEvents.stream(),
modelValidators.parallelStream().flatMap(validator -> validator.validate(model).stream()));
List<ValidationEvent> result = eventStream
List<ValidationEvent> result = modelValidators.parallelStream()
.flatMap(validator -> validator.validate(model).stream())
.filter(ModelValidator::filterPrelude)
.map(event -> suppressEvent(model, event, modelSuppressions))
// Emit events as they occur during validation.
.peek(eventListener)
.collect(Collectors.toList());

for (ValidationEvent event : includeEvents) {
if (ModelValidator.filterPrelude(event)) {
result.add(suppressEvent(model, event, modelSuppressions));
}
}

// Add in events encountered while building up validators and suppressions.
result.addAll(coreEvents);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,4 +932,20 @@ public void forwardReferencesAreOrdered() {
ShapeId.from("smithy.example#Error5"),
ShapeId.from("smithy.example#Error6")));
}

@Test
public void doesNotEmitWarningsTwice() {
List<ValidationEvent> events = new ArrayList<>();
Model.assembler()
.addUnparsedModel("foo.smithy", "$version: \"1.0\"\n")
.validationEventListener(e -> {
if (e.getId().equals(Validator.MODEL_DEPRECATION)) {
events.add(e);
}
})
.assemble()
.unwrap();

assertThat(events, hasSize(1));
}
}

0 comments on commit edfd6cc

Please sign in to comment.