Skip to content

Commit

Permalink
Fix an issue with double forward reference
Browse files Browse the repository at this point in the history
This commit fixes an issue where resolving a forward reference could
throw a ConcurrentModificationException when the resolution would
need to create another forward reference. For example, when applying
a trait to a shape defined in another file without importing the trait
or using its fully qualified shape ID, as done when applying a prelude
trait.
  • Loading branch information
kstich committed Jun 3, 2020
1 parent e4c7651 commit f1d6487
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import software.amazon.smithy.model.validation.ValidatedResult;
import software.amazon.smithy.model.validation.ValidationEvent;
import software.amazon.smithy.model.validation.Validator;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.SmithyBuilder;

/**
Expand Down Expand Up @@ -436,7 +437,13 @@ public ValidatedResult<Model> onEnd() {

private void finalizeShapeTargets() {
// Run any finalizers used for things like forward reference resolution.
for (ForwardReferenceResolver resolver : forwardReferenceResolvers) {
// Do this through a copy to handle forward references that generate more
// forward references, like when a Prelude trait is applied to a shape that
// is defined in another file.
List<ForwardReferenceResolver> resolvers = ListUtils.copyOf(forwardReferenceResolvers);
forwardReferenceResolvers.clear();

for (ForwardReferenceResolver resolver : resolvers) {
// First, resolve to a shape in the current namespace if one exists.
if (!hasDefinedShape(resolver.expectedId)) {
// Next resolve to a prelude shape if one exists and is public.
Expand All @@ -450,7 +457,10 @@ private void finalizeShapeTargets() {
resolver.consumer.accept(resolver.expectedId);
}

forwardReferenceResolvers.clear();
// Resolve any new forward references created.
if (!forwardReferenceResolvers.isEmpty()) {
finalizeShapeTargets();
}
}

private void finalizePendingTraits() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ apply Foo @bar("hi")

// This applies smithy.example.b#baz to smithy.example#Foo
apply Foo @baz("hi")

// This applies smithy.api#deprecated to smithy.example#Foo
apply Foo @deprecated

// This applies smithy.api#sensitive to smithy.example#Foo
apply Foo @smithy.api#sensitive

0 comments on commit f1d6487

Please sign in to comment.