|
5 | 5 | package software.amazon.smithy.aws.traits.protocols; |
6 | 6 |
|
7 | 7 | import java.util.ArrayList; |
| 8 | +import java.util.Collections; |
8 | 9 | import java.util.HashMap; |
| 10 | +import java.util.HashSet; |
9 | 11 | import java.util.List; |
10 | 12 | import java.util.Map; |
11 | | -import java.util.Objects; |
12 | 13 | import java.util.Set; |
13 | 14 | import java.util.TreeSet; |
14 | | -import java.util.stream.Collectors; |
15 | 15 | import software.amazon.smithy.model.Model; |
16 | 16 | import software.amazon.smithy.model.knowledge.TopDownIndex; |
| 17 | +import software.amazon.smithy.model.shapes.OperationShape; |
17 | 18 | import software.amazon.smithy.model.shapes.ServiceShape; |
18 | 19 | import software.amazon.smithy.model.shapes.Shape; |
19 | 20 | import software.amazon.smithy.model.shapes.ShapeId; |
|
26 | 27 | */ |
27 | 28 | @SmithyInternalApi |
28 | 29 | public class QueryErrorCodeValidator extends AbstractValidator { |
| 30 | + |
29 | 31 | @Override |
30 | 32 | public List<ValidationEvent> validate(Model model) { |
31 | | - List<ValidationEvent> events = new ArrayList<>(); |
| 33 | + List<ValidationEvent> events = Collections.emptyList(); |
32 | 34 | for (ServiceShape service : model.getServiceShapes()) { |
33 | 35 | if (service.hasTrait(AwsQueryCompatibleTrait.class) || service.hasTrait(AwsQueryTrait.class) |
34 | 36 | || service.hasTrait(Ec2QueryTrait.class)) { |
35 | | - events.addAll(validateService(model, service)); |
| 37 | + List<ValidationEvent> serviceEvents = validateService(model, service); |
| 38 | + if (!serviceEvents.isEmpty()) { |
| 39 | + if (events.isEmpty()) { |
| 40 | + events = new ArrayList<>(); |
| 41 | + } |
| 42 | + events.addAll(serviceEvents); |
| 43 | + } |
36 | 44 | } |
37 | 45 | } |
38 | 46 | return events; |
39 | 47 | } |
40 | 48 |
|
41 | 49 | private List<ValidationEvent> validateService(Model model, ServiceShape service) { |
42 | | - List<ValidationEvent> events = new ArrayList<>(); |
43 | 50 | TopDownIndex index = TopDownIndex.of(model); |
44 | | - List<ShapeId> errors = new ArrayList<>(service.getErrors()); |
45 | | - index.getContainedOperations(service).forEach(operation -> errors.addAll(operation.getErrors())); |
| 51 | + Set<ShapeId> errors = new HashSet<>(service.getErrors()); |
| 52 | + for (OperationShape operation : index.getContainedOperations(service)) { |
| 53 | + errors.addAll(operation.getErrors()); |
| 54 | + } |
46 | 55 |
|
47 | | - Map<String, Set<ShapeId>> errorCodeBindings = new HashMap<>(); |
| 56 | + Map<String, Set<String>> errorCodeBindings = new HashMap<>(); |
48 | 57 | for (ShapeId errorId : errors) { |
49 | 58 | String errorCode = errorId.getName(); |
50 | 59 | Shape errorShape = model.expectShape(errorId); |
51 | 60 | if (errorShape.hasTrait(AwsQueryErrorTrait.class)) { |
52 | 61 | AwsQueryErrorTrait trait = errorShape.expectTrait(AwsQueryErrorTrait.class); |
53 | 62 | errorCode = trait.getCode(); |
54 | 63 | } |
55 | | - errorCodeBindings.computeIfAbsent(errorCode, k -> new TreeSet<>()).add(errorId); |
| 64 | + errorCodeBindings.computeIfAbsent(errorCode, k -> new TreeSet<>()).add(errorId.toString()); |
56 | 65 | } |
57 | | - |
58 | | - for (Map.Entry<String, Set<ShapeId>> entry : errorCodeBindings.entrySet()) { |
| 66 | + List<ValidationEvent> events = Collections.emptyList(); |
| 67 | + for (Map.Entry<String, Set<String>> entry : errorCodeBindings.entrySet()) { |
59 | 68 | if (entry.getValue().size() == 1) { |
60 | 69 | continue; |
61 | 70 | } |
62 | 71 | String errorCode = entry.getKey(); |
63 | | - String shapes = entry.getValue().stream().map(Objects::toString).collect(Collectors.joining(", ")); |
64 | | - events.add(danger(service, |
| 72 | + String shapes = String.join(", ", entry.getValue()); |
| 73 | + if (events.isEmpty()) { |
| 74 | + events = new ArrayList<>(); |
| 75 | + } |
| 76 | + events.add(warning(service, |
65 | 77 | String.format("Multiple error shapes with the error code `%s` found: [%s]. This error code " |
66 | 78 | + "ambiguity will result in the wrong error shape being deserialized when running the " |
67 | 79 | + "query protocol. This pain will be carried forward when migrating to a new protocol " |
|
0 commit comments