Skip to content

Warn when box trait found on union member #1420

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

Merged
merged 1 commit into from
Sep 22, 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 @@ -22,6 +22,7 @@
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.BoxTrait;
import software.amazon.smithy.model.traits.DefaultTrait;
import software.amazon.smithy.model.validation.AbstractValidator;
import software.amazon.smithy.model.validation.ValidationEvent;
Expand All @@ -38,16 +39,30 @@ public List<ValidationEvent> validate(Model model) {
} else {
for (MemberShape member : union.getAllMembers().values()) {
Shape target = model.expectShape(member.getTarget());
if (target.hasTrait(DefaultTrait.class)) {
events.add(note(member, String.format(
"This union member targets `%s`, a shape with a default value of `%s`. Note that "
+ "default values are only applicable to structures and ignored in tagged unions. It "
+ "is a best practice for union members to target shapes with no default value.",
target.getId(), Node.printJson(target.expectTrait(DefaultTrait.class).toNode()))));
}
validateUnionMemberTarget(member, target, events);
validateUnionMember(member, events);
}
}
}
return events;
}

private void validateUnionMemberTarget(MemberShape member, Shape target, List<ValidationEvent> events) {
if (target.hasTrait(DefaultTrait.class)) {
events.add(note(member, String.format(
"This union member targets `%s`, a shape with a default value of `%s`. Note that "
+ "default values are only applicable to structures and ignored in tagged unions. It "
+ "is a best practice for union members to target shapes with no default value (for example, "
+ "instead of targeting PrimitiveInteger, target Integer).",
target.getId(), Node.printJson(target.expectTrait(DefaultTrait.class).toNode()))));
}
}

private void validateUnionMember(MemberShape member, List<ValidationEvent> events) {
if (member.hasTrait(BoxTrait.class)) {
events.add(warning(member, member.expectTrait(BoxTrait.class),
"Invalid box trait found on a union member. The box trait on union members "
+ "has no effect because union members are implicitly nullable."));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[NOTE] smithy.example#Example$foo: This union member targets `smithy.api#PrimitiveInteger`, a shape with a default value of `0`. Note that default values are only applicable to structures and ignored in tagged unions. It is a best practice for union members to target shapes with no default value. | Union
[NOTE] smithy.example#Example$bar: This union member targets `smithy.api#PrimitiveBoolean`, a shape with a default value of `false`. Note that default values are only applicable to structures and ignored in tagged unions. It is a best practice for union members to target shapes with no default value. | Union
[NOTE] smithy.example#Example$foo: This union member targets `smithy.api#PrimitiveInteger`, a shape with a default value of `0`. Note that default values are only applicable to structures and ignored in tagged unions. It is a best practice for union members to target shapes with no default value (for example, instead of targeting PrimitiveInteger, target Integer). | Union
[NOTE] smithy.example#Example$bar: This union member targets `smithy.api#PrimitiveBoolean`, a shape with a default value of `false`. Note that default values are only applicable to structures and ignored in tagged unions. It is a best practice for union members to target shapes with no default value (for example, instead of targeting PrimitiveInteger, target Integer). | Union
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[WARNING] smithy.example#Example$a: Invalid box trait found on a union member. The box trait on union members has no effect because union members are implicitly nullable. | Union
[NOTE] smithy.example#Example$a: This union member targets `smithy.api#PrimitiveInteger`, a shape with a default value of `0`. Note that default values are only applicable to structures and ignored in tagged unions. It is a best practice for union members to target shapes with no default value (for example, instead of targeting PrimitiveInteger, target Integer). | Union
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$version: "1.0"

namespace smithy.example

union Example {
@box
a: PrimitiveInteger,

b: Integer
}