|
38 | 38 | import software.amazon.smithy.utils.SetUtils;
|
39 | 39 |
|
40 | 40 | public class ClientEndpointDiscoveryValidator extends AbstractValidator {
|
| 41 | + private static final Set<String> VALID_INPUT_MEMBERS = SetUtils.of("Operation", "Identifiers"); |
| 42 | + |
41 | 43 | @Override
|
42 | 44 | public List<ValidationEvent> validate(Model model) {
|
43 | 45 | ClientEndpointDiscoveryIndex discoveryIndex = model.getKnowledge(ClientEndpointDiscoveryIndex.class);
|
@@ -133,37 +135,9 @@ private List<ValidationEvent> validateEndpointOperation(
|
133 | 135 | Model model, OperationIndex opIndex, OperationShape operation
|
134 | 136 | ) {
|
135 | 137 | List<ValidationEvent> events = new ArrayList<>();
|
136 |
| - opIndex.getInput(operation).ifPresent(input -> { |
137 |
| - Set<String> memberNames = SetUtils.copyOf(input.getMemberNames()); |
138 |
| - if (!memberNames.equals(SetUtils.of("Operation", "Identifiers"))) { |
139 |
| - events.add(error(input, String.format( |
140 |
| - "Input for endpoint discovery operation `%s` may only have the members Operation and " |
141 |
| - + "Identifiers but found: %s", |
142 |
| - operation.getId().toString(), |
143 |
| - String.join(", ", memberNames) |
144 |
| - ))); |
145 |
| - } |
146 |
| - |
147 |
| - input.getMember("Operation") |
148 |
| - .flatMap(member -> model.getShape(member.getTarget())) |
149 |
| - .filter(shape -> !shape.isStringShape()) |
150 |
| - .ifPresent(shape -> events.add(error( |
151 |
| - shape, "The Operation member of an endpoint discovery operation must be a string"))); |
152 |
| - |
153 |
| - input.getMember("Identifiers") |
154 |
| - .map(member -> Pair.of(member, model.getShape(member.getTarget()))) |
155 |
| - .ifPresent(pair -> { |
156 |
| - Optional<MapShape> map = pair.getRight().flatMap(Shape::asMapShape); |
157 |
| - if (map.isPresent()) { |
158 |
| - Optional<Shape> value = model.getShape(map.get().getValue().getTarget()); |
159 |
| - if (value.isPresent() && value.get().isStringShape()) { |
160 |
| - return; |
161 |
| - } |
162 |
| - } |
163 |
| - events.add(error(pair.getLeft(), "The Identifiers member of an endpoint discovery " |
164 |
| - + "operation must be a map whose keys and values are strings.")); |
165 |
| - }); |
166 |
| - }); |
| 138 | + opIndex.getInput(operation) |
| 139 | + .map(input -> validateEndpointOperationInput(model, input, operation)) |
| 140 | + .ifPresent(events::addAll); |
167 | 141 |
|
168 | 142 | Optional<StructureShape> output = opIndex.getOutput(operation);
|
169 | 143 | if (!output.isPresent()) {
|
@@ -224,4 +198,40 @@ private List<ValidationEvent> validateEndpointOperation(
|
224 | 198 |
|
225 | 199 | return events;
|
226 | 200 | }
|
| 201 | + |
| 202 | + private List<ValidationEvent> validateEndpointOperationInput( |
| 203 | + Model model, StructureShape input, OperationShape operation |
| 204 | + ) { |
| 205 | + List<ValidationEvent> events = new ArrayList<>(); |
| 206 | + Set<String> memberNames = SetUtils.copyOf(input.getMemberNames()); |
| 207 | + if (!VALID_INPUT_MEMBERS.containsAll(memberNames)) { |
| 208 | + events.add(error(input, String.format( |
| 209 | + "Input for endpoint discovery operation `%s` may only have the members Operation and " |
| 210 | + + "Identifiers but found: %s", |
| 211 | + operation.getId().toString(), |
| 212 | + String.join(", ", memberNames) |
| 213 | + ))); |
| 214 | + } |
| 215 | + |
| 216 | + input.getMember("Operation") |
| 217 | + .flatMap(member -> model.getShape(member.getTarget())) |
| 218 | + .filter(shape -> !shape.isStringShape()) |
| 219 | + .ifPresent(shape -> events.add(error( |
| 220 | + shape, "The Operation member of an endpoint discovery operation must be a string"))); |
| 221 | + |
| 222 | + input.getMember("Identifiers") |
| 223 | + .map(member -> Pair.of(member, model.getShape(member.getTarget()))) |
| 224 | + .ifPresent(pair -> { |
| 225 | + Optional<MapShape> map = pair.getRight().flatMap(Shape::asMapShape); |
| 226 | + if (map.isPresent()) { |
| 227 | + Optional<Shape> value = model.getShape(map.get().getValue().getTarget()); |
| 228 | + if (value.isPresent() && value.get().isStringShape()) { |
| 229 | + return; |
| 230 | + } |
| 231 | + } |
| 232 | + events.add(error(pair.getLeft(), "The Identifiers member of an endpoint discovery " |
| 233 | + + "operation must be a map whose keys and values are strings.")); |
| 234 | + }); |
| 235 | + return events; |
| 236 | + } |
227 | 237 | }
|
0 commit comments