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

Forbid fully recursive unions #1253

Merged
merged 1 commit into from
Jun 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
2 changes: 2 additions & 0 deletions docs/source/1.0/spec/core/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,8 @@ Smithy allows recursive shape definitions with the following limitations:
a recursive list in Java ``List<List<List....``).
2. A structure cannot contain a cyclical set of members marked with the
:ref:`required-trait` that refers back to itself.
3. A union MUST contain at least one member that does not refer back to
itself.

The following recursive shape definition is **valid**:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import software.amazon.smithy.model.selector.PathFinder;
import software.amazon.smithy.model.shapes.ListShape;
import software.amazon.smithy.model.shapes.MapShape;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.SetShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.RequiredTrait;
import software.amazon.smithy.model.validation.AbstractValidator;
import software.amazon.smithy.model.validation.ValidationEvent;
Expand All @@ -50,6 +52,7 @@ public List<ValidationEvent> validate(Model model) {
List<ValidationEvent> events = new ArrayList<>();
validateListMapSetShapes(finder, model, events);
validateStructurePaths(finder, model, events);
validateUnions(model, events);
return events;
}

Expand Down Expand Up @@ -108,4 +111,18 @@ private String formatPath(PathFinder.Path path) {
}
return joiner.toString();
}

private void validateUnions(Model model, List<ValidationEvent> events) {
nextUnion: for (UnionShape union : model.getUnionShapes()) {
// Don't claim the union is recursive if it's empty (which is also invalid).
if (!union.getAllMembers().isEmpty()) {
for (MemberShape member : union.getAllMembers().values()) {
if (!member.getTarget().equals(member.getContainer())) {
continue nextUnion;
}
}
events.add(error(union, "Unions must contain at least one member that is not directly recursive"));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ERROR] smithy.example#RecursiveUnion: Unions must contain at least one member that is not directly recursive | ShapeRecursion
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$version: "1.0"

namespace smithy.example

// It's impossible to provide a value for this union.
union RecursiveUnion {
a: RecursiveUnion,
b: RecursiveUnion,
}

union NotFullyRecursiveUnion {
a: RecursiveUnion,
b: RecursiveUnion,
c: String
}