|
| 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.HashSet; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Set; |
| 15 | +import software.amazon.smithy.model.Model; |
| 16 | +import software.amazon.smithy.model.selector.PathFinder; |
| 17 | +import software.amazon.smithy.model.shapes.MemberShape; |
| 18 | +import software.amazon.smithy.model.shapes.ResourceShape; |
| 19 | +import software.amazon.smithy.model.shapes.Shape; |
| 20 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 21 | +import software.amazon.smithy.model.traits.ReferencesTrait; |
| 22 | +import software.amazon.smithy.model.validation.AbstractValidator; |
| 23 | +import software.amazon.smithy.model.validation.ValidationEvent; |
| 24 | +import software.amazon.smithy.model.validation.ValidationUtils; |
| 25 | +import software.amazon.smithy.utils.ListUtils; |
| 26 | + |
| 27 | +/** |
| 28 | + * Validates if a member matches a resource identifier without the |
| 29 | + * proper configuration of a `@references` trait. |
| 30 | + */ |
| 31 | +public final class MemberShouldReferenceResourceValidator extends AbstractValidator { |
| 32 | + @Override |
| 33 | + public List<ValidationEvent> validate(Model model) { |
| 34 | + // There are usually far fewer resources than members, precompute the identifiers |
| 35 | + // so various short circuits can be added. |
| 36 | + Set<String> identifierNames = getAllIdentifierNames(model); |
| 37 | + // Short circuit validating all the members if we don't have any resources to test. |
| 38 | + if (identifierNames.isEmpty()) { |
| 39 | + return ListUtils.of(); |
| 40 | + } |
| 41 | + |
| 42 | + // Check every member to see if it's a potential reference. |
| 43 | + List<ValidationEvent> events = new ArrayList<>(); |
| 44 | + for (MemberShape member : model.getMemberShapes()) { |
| 45 | + // Only the known identifier names can match for this, skip names that we don't know. |
| 46 | + if (!identifierNames.contains(member.getMemberName())) { |
| 47 | + continue; |
| 48 | + } |
| 49 | + // Only strings can be identifiers, so skip non-String targets. |
| 50 | + if (!model.expectShape(member.getTarget()).isStringShape()) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + Set<ShapeId> potentialReferences = computePotentialReferences(model, member); |
| 55 | + if (!potentialReferences.isEmpty()) { |
| 56 | + events.add(warning(member, format("This member appears to reference the following resources without " |
| 57 | + + "being included in a `@references` trait: [%s]", |
| 58 | + ValidationUtils.tickedList(potentialReferences)))); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return events; |
| 63 | + } |
| 64 | + |
| 65 | + private Set<String> getAllIdentifierNames(Model model) { |
| 66 | + Set<String> identifierNames = new HashSet<>(); |
| 67 | + for (ResourceShape resource : model.getResourceShapes()) { |
| 68 | + identifierNames.addAll(resource.getIdentifiers().keySet()); |
| 69 | + } |
| 70 | + return identifierNames; |
| 71 | + } |
| 72 | + |
| 73 | + private Set<ShapeId> computePotentialReferences(Model model, MemberShape member) { |
| 74 | + // Exclude any resources already in `@references` on the member or container structure. |
| 75 | + Set<ShapeId> resourcesToIgnore = new HashSet<>(); |
| 76 | + ignoreReferencedResources(member, resourcesToIgnore); |
| 77 | + ignoreReferencedResources(model.expectShape(member.getContainer()), resourcesToIgnore); |
| 78 | + |
| 79 | + // Check each resource in the model for something missed. |
| 80 | + Set<ShapeId> potentialResources = new HashSet<>(); |
| 81 | + for (ResourceShape resource : model.getResourceShapes()) { |
| 82 | + // We'll want to ignore some resources based on the member -> resource path. |
| 83 | + computeResourcesToIgnore(model, member, resource, resourcesToIgnore); |
| 84 | + |
| 85 | + // Exclude members bound to resource hierarchies from generating events, |
| 86 | + // including for resources that are within the same hierarchy. |
| 87 | + if (resourcesToIgnore.contains(resource.getId())) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + // This member matches the identifier for the resource we're checking, add it to a list. |
| 92 | + if (isIdentifierMatch(resource, member)) { |
| 93 | + potentialResources.add(resource.getId()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Clean up any resources added through other paths that should be ignored. |
| 98 | + potentialResources.removeAll(resourcesToIgnore); |
| 99 | + return potentialResources; |
| 100 | + } |
| 101 | + |
| 102 | + private void computeResourcesToIgnore(Model model, MemberShape member, ResourceShape resource, |
| 103 | + Set<ShapeId> resourcesToIgnore) { |
| 104 | + // Exclude actually bound members via searching with a PathFinder. |
| 105 | + List<PathFinder.Path> resourceMemberPaths = PathFinder.create(model) |
| 106 | + .search(resource, ListUtils.of(member)); |
| 107 | + if (!resourceMemberPaths.isEmpty()) { |
| 108 | + // This member is already bound to a resource, so we don't need a references trait for it. |
| 109 | + // In addition, we should not tell users to add a references trait for other resources that |
| 110 | + // are children in that hierarchy - any parent resources or other children of those parents. |
| 111 | + for (PathFinder.Path path : resourceMemberPaths) { |
| 112 | + for (Shape pathShape : path.getShapes()) { |
| 113 | + if (pathShape.isResourceShape()) { |
| 114 | + ResourceShape resourceShape = (ResourceShape) pathShape; |
| 115 | + resourcesToIgnore.add(resourceShape.getId()); |
| 116 | + resourcesToIgnore.addAll(resourceShape.getResources()); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private void ignoreReferencedResources(Shape shape, Set<ShapeId> resourcesToIgnore) { |
| 124 | + if (shape.hasTrait(ReferencesTrait.class)) { |
| 125 | + for (ReferencesTrait.Reference reference : shape.expectTrait(ReferencesTrait.class) |
| 126 | + .getReferences()) { |
| 127 | + resourcesToIgnore.add(reference.getResource()); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private boolean isIdentifierMatch(ResourceShape resource, MemberShape member) { |
| 133 | + Map<String, ShapeId> identifiers = resource.getIdentifiers(); |
| 134 | + return identifiers.containsKey(member.getMemberName()) |
| 135 | + && identifiers.get(member.getMemberName()).equals(member.getTarget()); |
| 136 | + } |
| 137 | +} |
0 commit comments