|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.model.validation.validators; |
| 7 | + |
| 8 | +import static java.lang.String.format; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.HashSet; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.Set; |
| 16 | +import software.amazon.smithy.model.Model; |
| 17 | +import software.amazon.smithy.model.knowledge.OperationIndex; |
| 18 | +import software.amazon.smithy.model.knowledge.TopDownIndex; |
| 19 | +import software.amazon.smithy.model.shapes.MemberShape; |
| 20 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 21 | +import software.amazon.smithy.model.shapes.ResourceShape; |
| 22 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 23 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 24 | +import software.amazon.smithy.model.traits.RequiredTrait; |
| 25 | +import software.amazon.smithy.model.validation.AbstractValidator; |
| 26 | +import software.amazon.smithy.model.validation.ValidationEvent; |
| 27 | +import software.amazon.smithy.model.validation.ValidationUtils; |
| 28 | + |
| 29 | +/** |
| 30 | + * Validates if operations that are bound directly to a service may |
| 31 | + * be more accurately bound to a resource bound to the same service. |
| 32 | + */ |
| 33 | +public final class ServiceBoundResourceOperationValidator extends AbstractValidator { |
| 34 | + @Override |
| 35 | + public List<ValidationEvent> validate(Model model) { |
| 36 | + List<ValidationEvent> events = new ArrayList<>(); |
| 37 | + OperationIndex operationIndex = OperationIndex.of(model); |
| 38 | + TopDownIndex topDownIndex = TopDownIndex.of(model); |
| 39 | + |
| 40 | + // Check every service operation to see if it should be bound to a resource instead. |
| 41 | + for (ServiceShape service : model.getServiceShapes()) { |
| 42 | + // Store potential targets to emit one event per operation. |
| 43 | + Map<OperationShape, Set<ShapeId>> potentiallyBetterBindings = new HashMap<>(); |
| 44 | + for (ShapeId operationId : service.getOperations()) { |
| 45 | + OperationShape operation = model.expectShape(operationId, OperationShape.class); |
| 46 | + // Check the resources of the containing service to test for input/output attachment. |
| 47 | + for (ResourceShape resource : topDownIndex.getContainedResources(service)) { |
| 48 | + // Check the operation members to see if they are implicit matches for resource identifiers. |
| 49 | + for (MemberShape member : operationIndex.getInputMembers(operation).values()) { |
| 50 | + if (isImplicitIdentifierBinding(member, resource)) { |
| 51 | + potentiallyBetterBindings.computeIfAbsent(operation, k -> new HashSet<>()) |
| 52 | + .add(resource.getId()); |
| 53 | + } |
| 54 | + } |
| 55 | + for (MemberShape member : operationIndex.getOutputMembers(operation).values()) { |
| 56 | + if (isImplicitIdentifierBinding(member, resource)) { |
| 57 | + potentiallyBetterBindings.computeIfAbsent(operation, k -> new HashSet<>()) |
| 58 | + .add(resource.getId()); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Emit events per service that's present with a potentially bad binding. |
| 65 | + for (Map.Entry<OperationShape, Set<ShapeId>> entry : potentiallyBetterBindings.entrySet()) { |
| 66 | + events.add(warning(entry.getKey(), service, format( |
| 67 | + "The `%s` operation is bound to the `%s` service but has members that match identifiers " |
| 68 | + + "of the following resource shapes: [%s]. It may be more accurately bound to one " |
| 69 | + + "of them than directly to the service.", |
| 70 | + entry.getKey().getId(), service.getId(), ValidationUtils.tickedList(entry.getValue())), |
| 71 | + service.getId().toString(), entry.getKey().getId().getName())); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return events; |
| 76 | + } |
| 77 | + |
| 78 | + private boolean isImplicitIdentifierBinding(MemberShape member, ResourceShape resource) { |
| 79 | + return resource.getIdentifiers().containsKey(member.getMemberName()) |
| 80 | + && member.getTrait(RequiredTrait.class).isPresent() |
| 81 | + && member.getTarget().equals(resource.getIdentifiers().get(member.getMemberName())); |
| 82 | + } |
| 83 | +} |
0 commit comments