|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.codegen.validation; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel; |
| 22 | +import software.amazon.awssdk.codegen.model.intermediate.MemberModel; |
| 23 | +import software.amazon.awssdk.codegen.model.intermediate.OperationModel; |
| 24 | +import software.amazon.awssdk.codegen.model.intermediate.ShapeModel; |
| 25 | +import software.amazon.awssdk.codegen.model.service.PaginatorDefinition; |
| 26 | + |
| 27 | +/** |
| 28 | + * Validates paginator definitions against the service model. Checks that: |
| 29 | + * <ul> |
| 30 | + * <li>Each paginator references an operation that exists in the model</li> |
| 31 | + * <li>Input tokens reference valid members in the request shape</li> |
| 32 | + * <li>Output tokens reference valid members in the response shape</li> |
| 33 | + * <li>Result keys reference valid members in the response shape</li> |
| 34 | + * <li>The more_results field, if specified, references a valid member in the response shape</li> |
| 35 | + * <li>The limit_key field, if specified, references a valid member in the request shape</li> |
| 36 | + * </ul> |
| 37 | + */ |
| 38 | +public final class PaginatorDefinitionValidator implements ModelValidator { |
| 39 | + |
| 40 | + @Override |
| 41 | + public List<ValidationEntry> validateModels(ModelValidationContext context) { |
| 42 | + List<ValidationEntry> entries = new ArrayList<>(); |
| 43 | + IntermediateModel model = context.intermediateModel(); |
| 44 | + Map<String, PaginatorDefinition> paginators = model.getPaginators(); |
| 45 | + |
| 46 | + if (paginators == null || paginators.isEmpty()) { |
| 47 | + return entries; |
| 48 | + } |
| 49 | + |
| 50 | + for (Map.Entry<String, PaginatorDefinition> entry : paginators.entrySet()) { |
| 51 | + String operationName = entry.getKey(); |
| 52 | + PaginatorDefinition definition = entry.getValue(); |
| 53 | + |
| 54 | + // Skip paginators that don't have the minimum required fields (inputToken, outputToken). |
| 55 | + // This is legacy behavior: codegen has historically silently skipped these incomplete definitions |
| 56 | + // rather than generating code for them. We mirror that filter here so we only validate paginators |
| 57 | + // that will actually be used during code generation. |
| 58 | + if (!definition.hasAllRequiredFields()) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + OperationModel operationModel = model.getOperation(operationName); |
| 63 | + if (operationModel == null) { |
| 64 | + entries.add(ValidationEntry.create( |
| 65 | + ValidationErrorId.UNKNOWN_OPERATION, |
| 66 | + ValidationErrorSeverity.DANGER, |
| 67 | + String.format("Invalid paginator definition - The service model does not contain the " |
| 68 | + + "referenced operation '%s'", operationName) |
| 69 | + )); |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + ShapeModel inputShape = operationModel.getInputShape(); |
| 74 | + ShapeModel outputShape = operationModel.getOutputShape(); |
| 75 | + |
| 76 | + validateInputTokens(entries, operationName, definition, inputShape); |
| 77 | + validateOutputTokens(entries, operationName, definition, outputShape); |
| 78 | + validateResultKeys(entries, operationName, definition, outputShape); |
| 79 | + validateMoreResults(entries, operationName, definition, outputShape); |
| 80 | + validateLimitKey(entries, operationName, definition, inputShape); |
| 81 | + } |
| 82 | + |
| 83 | + return entries; |
| 84 | + } |
| 85 | + |
| 86 | + private void validateInputTokens(List<ValidationEntry> entries, String operationName, |
| 87 | + PaginatorDefinition definition, ShapeModel inputShape) { |
| 88 | + if (definition.getInputToken() == null || inputShape == null) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + for (String inputToken : definition.getInputToken()) { |
| 93 | + if (!isValidMemberPath(inputShape, inputToken)) { |
| 94 | + entries.add(ValidationEntry.create( |
| 95 | + ValidationErrorId.INVALID_PAGINATOR_DEFINITION, |
| 96 | + ValidationErrorSeverity.DANGER, |
| 97 | + String.format("Paginator for operation '%s' references input_token '%s' which does not " |
| 98 | + + "exist in the request shape '%s'", operationName, inputToken, |
| 99 | + inputShape.getShapeName()) |
| 100 | + )); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private void validateOutputTokens(List<ValidationEntry> entries, String operationName, |
| 106 | + PaginatorDefinition definition, ShapeModel outputShape) { |
| 107 | + if (definition.getOutputToken() == null || outputShape == null) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + for (String outputToken : definition.getOutputToken()) { |
| 112 | + if (!isValidMemberPath(outputShape, outputToken)) { |
| 113 | + entries.add(ValidationEntry.create( |
| 114 | + ValidationErrorId.INVALID_PAGINATOR_DEFINITION, |
| 115 | + ValidationErrorSeverity.DANGER, |
| 116 | + String.format("Paginator for operation '%s' references output_token '%s' which does not " |
| 117 | + + "exist in the response shape '%s'", operationName, outputToken, |
| 118 | + outputShape.getShapeName()) |
| 119 | + )); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private void validateResultKeys(List<ValidationEntry> entries, String operationName, |
| 125 | + PaginatorDefinition definition, ShapeModel outputShape) { |
| 126 | + if (definition.getResultKey() == null || outputShape == null) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + for (String resultKey : definition.getResultKey()) { |
| 131 | + if (!isValidMemberPath(outputShape, resultKey)) { |
| 132 | + entries.add(ValidationEntry.create( |
| 133 | + ValidationErrorId.INVALID_PAGINATOR_DEFINITION, |
| 134 | + ValidationErrorSeverity.DANGER, |
| 135 | + String.format("Paginator for operation '%s' references result_key '%s' which does not " |
| 136 | + + "exist in the response shape '%s'", operationName, resultKey, |
| 137 | + outputShape.getShapeName()) |
| 138 | + )); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private void validateMoreResults(List<ValidationEntry> entries, String operationName, |
| 144 | + PaginatorDefinition definition, ShapeModel outputShape) { |
| 145 | + if (definition.getMoreResults() == null || outputShape == null) { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + if (!isValidMemberPath(outputShape, definition.getMoreResults())) { |
| 150 | + entries.add(ValidationEntry.create( |
| 151 | + ValidationErrorId.INVALID_PAGINATOR_DEFINITION, |
| 152 | + ValidationErrorSeverity.DANGER, |
| 153 | + String.format("Paginator for operation '%s' references more_results '%s' which does not " |
| 154 | + + "exist in the response shape '%s'", operationName, definition.getMoreResults(), |
| 155 | + outputShape.getShapeName()) |
| 156 | + )); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private void validateLimitKey(List<ValidationEntry> entries, String operationName, |
| 161 | + PaginatorDefinition definition, ShapeModel inputShape) { |
| 162 | + if (definition.getLimitKey() == null || inputShape == null) { |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + if (!isValidMemberPath(inputShape, definition.getLimitKey())) { |
| 167 | + entries.add(ValidationEntry.create( |
| 168 | + ValidationErrorId.INVALID_PAGINATOR_DEFINITION, |
| 169 | + ValidationErrorSeverity.DANGER, |
| 170 | + String.format("Paginator for operation '%s' references limit_key '%s' which does not " |
| 171 | + + "exist in the request shape '%s'", operationName, definition.getLimitKey(), |
| 172 | + inputShape.getShapeName()) |
| 173 | + )); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Validates that a dotted member path (e.g. "StreamDescription.Shards") resolves to a valid member |
| 179 | + * in the given shape hierarchy. |
| 180 | + */ |
| 181 | + private boolean isValidMemberPath(ShapeModel shape, String memberPath) { |
| 182 | + if (shape == null || memberPath == null || memberPath.isEmpty()) { |
| 183 | + return false; |
| 184 | + } |
| 185 | + |
| 186 | + String[] segments = memberPath.split("\\."); |
| 187 | + ShapeModel currentShape = shape; |
| 188 | + |
| 189 | + for (int i = 0; i < segments.length; i++) { |
| 190 | + MemberModel member = currentShape.getMemberByC2jName(segments[i]); |
| 191 | + if (member == null) { |
| 192 | + return false; |
| 193 | + } |
| 194 | + if (i < segments.length - 1) { |
| 195 | + currentShape = member.getShape(); |
| 196 | + if (currentShape == null) { |
| 197 | + return false; |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return true; |
| 203 | + } |
| 204 | +} |
0 commit comments