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

Add validator to warn on ignored idempotency token trait #2358

Conversation

sugmanue
Copy link
Contributor

Background

  • What do these changes do?

Added a validator to warn on ignored use of @idempotencyToken trait. As per spec,

Only a single member of the input of an operation can be targeted by the idempotencyToken trait; only top-level structure members of the input of an operation are considered.

This validator warns when we find a member using the trait in a context where it will be ignored. We cannot ERROR here as the structures might be reused in valid and invalid context, thus we only warning about the context where it will be ignored.

  • Why are they important?

Testing

  • How did you test these changes?

Links

  • Links to additional context, if necessary
  • Issue #, if applicable (see here for a list of keywords to use for linking issues)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@sugmanue sugmanue requested a review from a team as a code owner July 26, 2024 00:17
@sugmanue sugmanue requested a review from kstich July 26, 2024 00:17
}
List<ValidationEvent> events = new ArrayList<>();
NeighborProvider reverse = NeighborProviderIndex.of(model).getReverseProvider();
for (MemberShape memberShape : model.getMemberShapes()) {
Copy link
Member

Choose a reason for hiding this comment

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

You can use getMemberShapesWithTrait(IdempotencyToken.class)


// Store relationships so we can emit one event per ignored binding.
Map<RelationshipType, List<ShapeId>> ignoredRelationships = new TreeMap<>();
List<ValidationEvent> events = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

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

minor but we could just pass in the mutable list of events from the validate method rather than needing to create intermediate lists

continue;
}
if (relationship.getRelationshipType() != RelationshipType.INPUT) {
ignoredRelationships.merge(relationship.getRelationshipType(),
Copy link
Member

Choose a reason for hiding this comment

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

Can this be simplified to just use ignoredRelationships.computeIfAbsent? Might not need mergeShapeIdsList either at that point.

Map<RelationshipType, List<ShapeId>> ignoredRelationships
) {
String mixedIn = memberShape.getMixins().isEmpty() ? "" : " mixed in";
String message = "The `%s` trait applied to this%s member is ";
Copy link
Member

Choose a reason for hiding this comment

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

Can we say something more clear like "The idempotencyToken trait only has an effect when applied to a top-level operation input member, but it was applied and ignored in the following contexts: " ... etc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will change as described.

) {
String mixedIn = memberShape.getMixins().isEmpty() ? "" : " mixed in";
String message = "The `%s` trait applied to this%s member is ";
if (checkedRelationshipCount == ignoredRelationships.size()) {
Copy link
Member

Choose a reason for hiding this comment

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

Why omit the context here? It looks like we emit the event on the member that has the trait, but don't say how the containing shape was referenced in a way that caused the trait to be ignored.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will remove.

sugmanue and others added 2 commits July 29, 2024 11:01
…tion/validators/IdempotencyTokenIgnoredValidator.java

Co-authored-by: Michael Dowling <michael@mtdowling.com>
// Store relationships so we can emit one event per ignored binding.
Map<RelationshipType, List<ShapeId>> ignoredRelationships = new TreeMap<>();
List<Relationship> relationships = reverse.getNeighbors(containerShape);
int checkedRelationshipCount = relationships.size();
Copy link
Member

Choose a reason for hiding this comment

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

looks like this doesn't do anything now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦 yeah, let me remove that.

Copy link
Member

@mtdowling mtdowling left a comment

Choose a reason for hiding this comment

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

Ship it but remove what I think is some dead code

@sugmanue sugmanue merged commit 3d76f98 into smithy-lang:main Jul 29, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants