Skip to content

Add validation for missing request URI #6182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-7cf1e5c.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Add code generation validation for missing request URI on an operation."
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -35,6 +37,7 @@
import software.amazon.awssdk.codegen.C2jModels;
import software.amazon.awssdk.codegen.CodeGenerator;
import software.amazon.awssdk.codegen.IntermediateModelBuilder;
import software.amazon.awssdk.codegen.internal.Jackson;
import software.amazon.awssdk.codegen.internal.Utils;
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
Expand All @@ -44,6 +47,8 @@
import software.amazon.awssdk.codegen.model.service.ServiceModel;
import software.amazon.awssdk.codegen.model.service.Waiters;
import software.amazon.awssdk.codegen.utils.ModelLoaderUtils;
import software.amazon.awssdk.codegen.validation.ModelInvalidException;
import software.amazon.awssdk.codegen.validation.ModelValidationReport;
import software.amazon.awssdk.utils.StringUtils;

/**
Expand Down Expand Up @@ -84,7 +89,18 @@ public void execute() throws MojoExecutionException {
this.resourcesDirectory = Paths.get(outputDirectory).resolve("generated-resources").resolve("sdk-resources");
this.testsDirectory = Paths.get(outputDirectory).resolve("generated-test-sources").resolve("sdk-tests");

List<GenerationParams> generationParams = initGenerationParams();
List<GenerationParams> generationParams;

try {
generationParams = initGenerationParams();
} catch (ModelInvalidException e) {
if (writeValidationReport) {
ModelValidationReport report = new ModelValidationReport();
report.setValidationEntries(e.validationEntries());
emitValidationReport(report);
}
throw e;
}

Map<String, IntermediateModel> serviceNameToModelMap = new HashMap<>();

Expand Down Expand Up @@ -137,6 +153,8 @@ private List<GenerationParams> initGenerationParams() throws MojoExecutionExcept
}).collect(Collectors.toList());
}



private Stream<ModelRoot> findModelRoots() throws MojoExecutionException {
try {
return Files.find(codeGenResources.toPath(), 10, this::isModelFile)
Expand Down Expand Up @@ -216,6 +234,17 @@ private <T> Optional<T> loadOptionalModel(Class<T> clzz, Path location) {
return ModelLoaderUtils.loadOptionalModel(clzz, location.toFile());
}

private void emitValidationReport(ModelValidationReport report) {
Path modelsDir = sourcesDirectory.resolve("models");
try {
Writer writer = Files.newBufferedWriter(modelsDir.resolve("validation-report.json"),
StandardCharsets.UTF_8);
Jackson.writeWithObjectMapper(report, writer);
} catch (IOException e) {
getLog().warn("Failed to write validation report to " + modelsDir, e);
}
}

private static class ModelRoot {
private final Path modelRoot;
private final CustomizationConfig customizationConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static software.amazon.awssdk.codegen.internal.Utils.isMapShape;
import static software.amazon.awssdk.codegen.internal.Utils.isScalar;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -37,9 +38,14 @@
import software.amazon.awssdk.codegen.model.intermediate.VariableModel;
import software.amazon.awssdk.codegen.model.service.Location;
import software.amazon.awssdk.codegen.model.service.Member;
import software.amazon.awssdk.codegen.model.service.Operation;
import software.amazon.awssdk.codegen.model.service.ServiceModel;
import software.amazon.awssdk.codegen.model.service.Shape;
import software.amazon.awssdk.codegen.naming.NamingStrategy;
import software.amazon.awssdk.codegen.validation.ModelInvalidException;
import software.amazon.awssdk.codegen.validation.ValidationEntry;
import software.amazon.awssdk.codegen.validation.ValidationErrorId;
import software.amazon.awssdk.codegen.validation.ValidationErrorSeverity;
import software.amazon.awssdk.utils.StringUtils;
import software.amazon.awssdk.utils.Validate;

Expand Down Expand Up @@ -345,11 +351,20 @@ private boolean isGreedy(Shape parentShape, Map<String, Shape> allC2jShapes, Par
* @throws RuntimeException If operation can't be found.
*/
private String findRequestUri(Shape parentShape, Map<String, Shape> allC2jShapes) {
return builder.getService().getOperations().values().stream()
.filter(o -> o.getInput() != null)
.filter(o -> allC2jShapes.get(o.getInput().getShape()).equals(parentShape))
.map(o -> o.getHttp().getRequestUri())
.findFirst().orElseThrow(() -> new RuntimeException("Could not find request URI for input shape"));
Optional<Operation> operation = builder.getService().getOperations().values().stream()
.filter(o -> o.getInput() != null)
.filter(o -> allC2jShapes.get(o.getInput().getShape()).equals(parentShape))
.findFirst();

return operation.map(o -> o.getHttp().getRequestUri())
.orElseThrow(() -> {
String detailMsg = "Could not find request URI for input shape";
ValidationEntry entry =
new ValidationEntry().withErrorId(ValidationErrorId.REQUEST_URI_NOT_FOUND)
.withDetailMessage(detailMsg)
.withSeverity(ValidationErrorSeverity.DANGER);
return ModelInvalidException.builder().validationEntries(Collections.singletonList(entry)).build();
});
}

private String deriveUnmarshallerLocationName(Shape memberShape, String memberName, Member member) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public enum ValidationErrorId {
+ " files generated by the code generator."
),
UNKNOWN_SHAPE_MEMBER("The model references an unknown shape member."),
REQUEST_URI_NOT_FOUND("The request URI does not exist."),
;

private final String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.codegen.internal.Jackson;
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.rules.endpoints.EndpointTestSuiteModel;
import software.amazon.awssdk.codegen.model.service.ServiceModel;
import software.amazon.awssdk.codegen.poet.ClientTestModels;
import software.amazon.awssdk.codegen.validation.ModelInvalidException;
import software.amazon.awssdk.codegen.validation.ModelValidator;
Expand Down Expand Up @@ -155,6 +157,19 @@ void execute_endpointsTestReferencesUnknownOperationMember_throwsValidationError
});
}

@Test
void execute_operationHasNoRequestUri_throwsValidationError() throws IOException {
C2jModels models = C2jModels.builder()
.customizationConfig(CustomizationConfig.create())
.serviceModel(getMissingRequestUriServiceModel())
.build();

assertThatThrownBy(() -> generateCodeFromC2jModels(models, outputDir, true, Collections.emptyList()))
.isInstanceOf(ModelInvalidException.class)
.matches(e -> ((ModelInvalidException) e).validationEntries().get(0).getErrorId()
== ValidationErrorId.REQUEST_URI_NOT_FOUND);
}

private void generateCodeFromC2jModels(C2jModels c2jModels, Path outputDir) {
generateCodeFromC2jModels(c2jModels, outputDir, false, null);
}
Expand Down Expand Up @@ -201,17 +216,28 @@ private static Path validationReportPath(Path root) {
}

private EndpointTestSuiteModel getBrokenEndpointTestSuiteModel() throws IOException {
InputStream resourceAsStream = getClass().getResourceAsStream("incorrect-endpoint-tests.json");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read;
while ((read = resourceAsStream.read(buffer)) != -1) {
baos.write(buffer, 0, read);
}
String json = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(baos.toByteArray())).toString();
String json = resourceAsString("incorrect-endpoint-tests.json");
return Jackson.load(EndpointTestSuiteModel.class, json);
}

private ServiceModel getMissingRequestUriServiceModel() throws IOException {
String json = resourceAsString("no-request-uri-operation-service.json");
return Jackson.load(ServiceModel.class, json);
}

private String resourceAsString(String name) throws IOException {
ByteArrayOutputStream baos;
try (InputStream resourceAsStream = getClass().getResourceAsStream(name)) {
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read;
while ((read = resourceAsStream.read(buffer)) != -1) {
baos.write(buffer, 0, read);
}
}
return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(baos.toByteArray())).toString();
}

private static void deleteDirectory(Path dir) throws IOException {
Files.walkFileTree(dir, new FileVisitor<Path>() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"version": "2.0",
"metadata": {
"apiVersion": "2010-05-08",
"endpointPrefix": "json-service-endpoint",
"globalEndpoint": "json-service.amazonaws.com",
"protocol": "rest-json",
"serviceAbbreviation": "Rest Json Service",
"serviceFullName": "Some Service That Uses Rest-Json Protocol",
"serviceId": "Rest Json Service",
"signingName": "json-service",
"signatureVersion": "v4",
"uid": "json-service-2010-05-08",
"xmlNamespace": "https://json-service.amazonaws.com/doc/2010-05-08/"
},
"operations": {
"OperationWithUriMappedParam": {
"name": "OperationWithUriMappedParam",
"http": {
"method": "GET"
},
"input": {
"shape": "OperationWithUriMappedParamRequest"
}
}
},
"shapes": {
"OperationWithUriMappedParamRequest": {
"type": "structure",
"members": {
"StringMember": {
"shape": "String",
"location": "uri",
"locationName": "stringMember"
}
}
},
"String": {
"type": "string"
}
},
"documentation": "A service that is implemented using the rest-json protocol"
}
Loading