-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validator for service bound resoruce operations
This commit introduces a new validator that emits warnings when it detects operations that are bound directly to a service when a resource bound to that service may be a better match.
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...are/amazon/smithy/model/validation/validators/ServiceBoundResourceOperationValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.model.validation.validators; | ||
|
||
import static java.lang.String.format; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.knowledge.OperationIndex; | ||
import software.amazon.smithy.model.knowledge.TopDownIndex; | ||
import software.amazon.smithy.model.shapes.MemberShape; | ||
import software.amazon.smithy.model.shapes.OperationShape; | ||
import software.amazon.smithy.model.shapes.ResourceShape; | ||
import software.amazon.smithy.model.shapes.ServiceShape; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.model.traits.RequiredTrait; | ||
import software.amazon.smithy.model.validation.AbstractValidator; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
import software.amazon.smithy.model.validation.ValidationUtils; | ||
|
||
/** | ||
* Validates if operations that are bound directly to a service may | ||
* be more accurately bound to a resource bound to the same service. | ||
*/ | ||
public final class ServiceBoundResourceOperationValidator extends AbstractValidator { | ||
@Override | ||
public List<ValidationEvent> validate(Model model) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
OperationIndex operationIndex = OperationIndex.of(model); | ||
TopDownIndex topDownIndex = TopDownIndex.of(model); | ||
|
||
// Check every service operation to see if it should be bound to a resource instead. | ||
for (ServiceShape service : model.getServiceShapes()) { | ||
// Store potential targets to emit one event per operation. | ||
Map<OperationShape, Set<ShapeId>> potentiallyBetterBindings = new HashMap<>(); | ||
for (ShapeId operationId : service.getOperations()) { | ||
OperationShape operation = model.expectShape(operationId, OperationShape.class); | ||
// Check the resources of the containing service to test for input/output attachment. | ||
for (ResourceShape resource : topDownIndex.getContainedResources(service)) { | ||
// Check the operation members to see if they are implicit matches for resource identifiers. | ||
for (MemberShape member : operationIndex.getInputMembers(operation).values()) { | ||
if (isImplicitIdentifierBinding(member, resource)) { | ||
potentiallyBetterBindings.computeIfAbsent(operation, k -> new HashSet<>()) | ||
.add(resource.getId()); | ||
} | ||
} | ||
for (MemberShape member : operationIndex.getOutputMembers(operation).values()) { | ||
if (isImplicitIdentifierBinding(member, resource)) { | ||
potentiallyBetterBindings.computeIfAbsent(operation, k -> new HashSet<>()) | ||
.add(resource.getId()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Emit events per service that's present with a potentially bad binding. | ||
for (Map.Entry<OperationShape, Set<ShapeId>> entry : potentiallyBetterBindings.entrySet()) { | ||
events.add(warning(entry.getKey(), service, format( | ||
"The `%s` operation is bound to the `%s` service but has members that match identifiers " | ||
+ "of the following resource shapes: [%s]. It may be more accurately bound to one " | ||
+ "of them than directly to the service.", | ||
entry.getKey().getId(), service.getId(), ValidationUtils.tickedList(entry.getValue())), | ||
service.getId().toString(), entry.getKey().getId().getName())); | ||
} | ||
} | ||
|
||
return events; | ||
} | ||
|
||
private boolean isImplicitIdentifierBinding(MemberShape member, ResourceShape resource) { | ||
return resource.getIdentifiers().containsKey(member.getMemberName()) | ||
&& member.getTrait(RequiredTrait.class).isPresent() | ||
&& member.getTarget().equals(resource.getIdentifiers().get(member.getMemberName())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...mithy/model/errorfiles/validators/resource-identifiers/detects-misbound-operations.errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[WARNING] smithy.example#GetFoo: The `smithy.example#GetFoo` operation is bound to the `smithy.example#FooService` service but has members that match identifiers of the following resource shapes: [`smithy.example#Foo`]. It may be more accurately bound to one of them than directly to the service. | ServiceBoundResourceOperation.smithy.example#FooService.GetFoo |
53 changes: 53 additions & 0 deletions
53
...mithy/model/errorfiles/validators/resource-identifiers/detects-misbound-operations.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
$version: "2.0" | ||
|
||
namespace smithy.example | ||
|
||
service FooService { | ||
version: "2020-07-02" | ||
operations: [GetFoo] | ||
resources: [Foo, BoundNoMatch] | ||
} | ||
|
||
resource Foo { | ||
identifiers: { | ||
fooId: String | ||
} | ||
create: CreateFoo | ||
} | ||
|
||
operation CreateFoo { | ||
input := { | ||
@required | ||
something: String | ||
} | ||
output := { | ||
@required | ||
fooId: String | ||
|
||
@required | ||
something: String | ||
} | ||
} | ||
|
||
operation GetFoo { | ||
input := { | ||
@required | ||
fooId: String | ||
} | ||
output := { | ||
@required | ||
something: String | ||
} | ||
} | ||
|
||
resource Unbound { | ||
identifiers: { | ||
id: String | ||
} | ||
} | ||
|
||
resource BoundNoMatch { | ||
identifiers: { | ||
id: String | ||
} | ||
} |