Skip to content

Sqs AwsJson migration #3329

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

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "AWS Codegen, AWS core",
"contributor": "ziyanli-amazon",
"description": "Add awsQueryCompatible trait support to service.\nWhen awsQueryCompatible trait is found, it's made available as an API property. When this property is found,\nthe error code is returned by looking up the mapping. This is a pre-requisite for migrating services from\nAWSQuery wire protocol to AWSJson."
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public IntermediateModel build() {
IntermediateModel fullModel = new IntermediateModel(
constructMetadata(service, customConfig), operations, shapes,
customConfig, endpointOperation, paginators.getPagination(), namingStrategy,
waiters.getWaiters());
waiters.getWaiters(), service.getAwsQueryCompatible());

customization.postprocess(fullModel);

Expand All @@ -149,7 +149,8 @@ public IntermediateModel build() {
endpointOperation,
fullModel.getPaginators(),
namingStrategy,
fullModel.getWaiters());
fullModel.getWaiters(),
fullModel.getAwsQueryCompatible());

linkMembersToShapes(trimmedModel);
linkOperationsToInputOutputShapes(trimmedModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import software.amazon.awssdk.awscore.AwsResponse;
import software.amazon.awssdk.awscore.AwsResponseMetadata;
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
import software.amazon.awssdk.codegen.model.service.AwsQueryCompatible;
import software.amazon.awssdk.codegen.model.service.PaginatorDefinition;
import software.amazon.awssdk.codegen.model.service.WaiterDefinition;
import software.amazon.awssdk.codegen.naming.NamingStrategy;
Expand All @@ -53,6 +54,9 @@ public final class IntermediateModel {
@JsonIgnore
private NamingStrategy namingStrategy;

@JsonIgnore
private Map<String, AwsQueryCompatible> awsQueryCompatible;

static {
FILE_HEADER = loadDefaultFileHeader();
}
Expand All @@ -71,7 +75,7 @@ public IntermediateModel(Metadata metadata,
Map<String, ShapeModel> shapes,
CustomizationConfig customizationConfig) {
this(metadata, operations, shapes, customizationConfig, null,
Collections.emptyMap(), null, Collections.emptyMap());
Collections.emptyMap(), null, Collections.emptyMap(), Collections.emptyMap());
}

public IntermediateModel(
Expand All @@ -82,7 +86,8 @@ public IntermediateModel(
OperationModel endpointOperation,
Map<String, PaginatorDefinition> paginators,
NamingStrategy namingStrategy,
Map<String, WaiterDefinition> waiters) {
Map<String, WaiterDefinition> waiters,
Map<String, AwsQueryCompatible> awsQueryCompatible) {
this.metadata = metadata;
this.operations = operations;
this.shapes = shapes;
Expand All @@ -91,6 +96,7 @@ public IntermediateModel(
this.paginators = paginators;
this.namingStrategy = namingStrategy;
this.waiters = waiters;
this.awsQueryCompatible = awsQueryCompatible;
}

public Metadata getMetadata() {
Expand Down Expand Up @@ -154,6 +160,9 @@ public Map<String, WaiterDefinition> getWaiters() {
return waiters;
}

public Map<String, AwsQueryCompatible> getAwsQueryCompatible() {
return awsQueryCompatible;
}
public void setPaginators(Map<String, PaginatorDefinition> paginators) {
this.paginators = paginators;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.codegen.model.service;

/**
* Models the errorCode to use for errors to make the error compatible with an AWS Query protocol.
*/
public class AwsQueryCompatible {
private String errorCode;

public AwsQueryCompatible() {
}
public AwsQueryCompatible(String errorCode) {
this.errorCode = errorCode;
}

public String getErrorCode() {
return errorCode;
}

public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ServiceModel {
private Map<String, Shape> shapes;
private Map<String, Authorizer> authorizers;

private Map<String, AwsQueryCompatible> awsQueryCompatible;
private String documentation;

public ServiceModel() {
Expand All @@ -33,11 +34,13 @@ public ServiceModel() {
public ServiceModel(ServiceMetadata metadata,
Map<String, Operation> operations,
Map<String, Shape> shapes,
Map<String, Authorizer> authorizers) {
Map<String, Authorizer> authorizers,
Map<String, AwsQueryCompatible> awsQueryCompatible) {
this.metadata = metadata;
this.operations = operations;
this.shapes = shapes;
this.authorizers = authorizers;
this.awsQueryCompatible = awsQueryCompatible;
}

public ServiceMetadata getMetadata() {
Expand Down Expand Up @@ -99,4 +102,18 @@ public Map<String, Authorizer> getAuthorizers() {
public void setAuthorizers(Map<String, Authorizer> authorizers) {
this.authorizers = authorizers;
}

/**
* Provides a map of error code to compatibility model, used during error deserialization to ensure
* that errorCode is compatible with an AWS Query protocol
*
* @return the compatibility map, or null if none is specified in the model
*/
public Map<String, AwsQueryCompatible> getAwsQueryCompatible() {
return awsQueryCompatible;
}

public void setAwsQueryCompatible(Map<String, AwsQueryCompatible> awsQueryCompatible) {
this.awsQueryCompatible = awsQueryCompatible;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public MethodSpec initProtocolFactory(IntermediateModel model) {
if (contentType != null) {
methodSpec.addCode(".contentType($S)", contentType);
}
if (model.getAwsQueryCompatible() != null) {
methodSpec.addCode(errorCodeMapping(model));
}

registerModeledExceptions(model, poetExtensions).forEach(methodSpec::addCode);
methodSpec.addCode(";");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import software.amazon.awssdk.codegen.model.intermediate.ShapeModel;
import software.amazon.awssdk.codegen.model.intermediate.ShapeType;
import software.amazon.awssdk.codegen.model.service.AuthType;
import software.amazon.awssdk.codegen.model.service.AwsQueryCompatible;
import software.amazon.awssdk.codegen.poet.PoetExtension;
import software.amazon.awssdk.core.client.handler.SyncClientHandler;
import software.amazon.awssdk.core.runtime.transform.AsyncStreamingRequestMarshaller;
Expand Down Expand Up @@ -68,6 +69,17 @@ default List<MethodSpec> additionalMethods() {
return new ArrayList<>();
}

default CodeBlock errorCodeMapping(IntermediateModel model) {
CodeBlock.Builder builder = CodeBlock.builder()
.add(".errorCodeMapping(software.amazon.awssdk.utils.ImmutableMap.<String,String>builder()\n");
model.getAwsQueryCompatible().keySet().stream().sorted().forEach(errorCode -> {
AwsQueryCompatible errorModel = model.getAwsQueryCompatible().get(errorCode);
builder.add(".put($S,$S)\n", errorCode, errorModel.getErrorCode());
});
builder.add(".build()");
return builder.build();
}

default List<CodeBlock> registerModeledExceptions(IntermediateModel model, PoetExtension poetExtensions) {
return model.getShapes().values().stream()
.filter(s -> s.getShapeType() == ShapeType.Exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.ShapeModel;
import software.amazon.awssdk.codegen.model.service.AwsQueryCompatible;
import software.amazon.awssdk.codegen.model.service.ServiceModel;
import software.amazon.awssdk.codegen.utils.ModelLoaderUtils;

Expand Down Expand Up @@ -89,4 +90,20 @@ public void defaultEndpointDiscovery_false() {
assertFalse(testModel.getEndpointOperation().get().isEndpointCacheRequired());
}

@Test
public void testAwsQueryCompatibleDeserialization() {
final File modelFile = new File(IntermediateModelBuilderTest.class
.getResource("poet/client/c2j/awsQueryCompatible/service-2.json").getFile());
IntermediateModel testModel = new IntermediateModelBuilder(
C2jModels.builder()
.serviceModel(ModelLoaderUtils.loadModel(ServiceModel.class, modelFile))
.customizationConfig(CustomizationConfig.create())
.build())
.build();

assertThat(testModel.getAwsQueryCompatible().values())
.extracting(AwsQueryCompatible::getErrorCode)
.contains("fooErrorCode");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,34 @@
package software.amazon.awssdk.codegen.model.intermediate;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.codegen.C2jModels;
import software.amazon.awssdk.codegen.IntermediateModelBuilder;
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
import software.amazon.awssdk.codegen.model.service.AwsQueryCompatible;
import software.amazon.awssdk.codegen.model.service.ServiceMetadata;
import software.amazon.awssdk.codegen.model.service.ServiceModel;
import software.amazon.awssdk.codegen.utils.ModelLoaderUtils;

public class IntermediateModelTest {

private static final String ERROR_CODE = "fooErrorCode";
private static final String QUERY_ERROR_CODE = "queryErrorCode";
private final AwsQueryCompatible model = new AwsQueryCompatible(ERROR_CODE);
private final Map<String, AwsQueryCompatible> awsQueryCompatible = new HashMap() {
{
put(QUERY_ERROR_CODE, model);
}
};

@Test
public void cannotFindShapeWhenNoShapesExist() {

Expand All @@ -40,6 +55,7 @@ public void cannotFindShapeWhenNoShapesExist() {
IntermediateModel testModel = new IntermediateModelBuilder(
C2jModels.builder()
.serviceModel(new ServiceModel(metadata,
Collections.emptyMap(),
Collections.emptyMap(),
Collections.emptyMap(),
Collections.emptyMap()))
Expand Down Expand Up @@ -69,4 +85,29 @@ public void getShapeByNameAndC2jNameVerifiesC2JName() {
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("C2J shape AnyShape with shape name PingResponse does not exist in the intermediate model.");
}

@Test
public void validateAwsQueryCompatible() {

ServiceMetadata metadata = new ServiceMetadata();
metadata.setProtocol(Protocol.REST_JSON.getValue());
metadata.setServiceId("empty-service");
metadata.setSignatureVersion("V4");

IntermediateModel testModel = new IntermediateModelBuilder(
C2jModels.builder()
.serviceModel(new ServiceModel(metadata,
Collections.emptyMap(),
Collections.emptyMap(),
Collections.emptyMap(),
awsQueryCompatible))
.customizationConfig(CustomizationConfig.create())
.build())
.build();

assertEquals(awsQueryCompatible, testModel.getAwsQueryCompatible());
assertNotNull(testModel.getAwsQueryCompatible().get(QUERY_ERROR_CODE));
AwsQueryCompatible awsQueryCompatible = testModel.getAwsQueryCompatible().get(QUERY_ERROR_CODE);
Assert.assertEquals(ERROR_CODE, awsQueryCompatible.getErrorCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.codegen.model.service;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class AwsQueryCompatibleTest {

private static final String ERROR_CODE = "fooErrorCode";
private AwsQueryCompatible model = new AwsQueryCompatible(ERROR_CODE);

@Test
public void validateAwsQueryCompatibleAccessors() {
assertEquals(ERROR_CODE, model.getErrorCode());
}

@Test
public void validateAwsQueryCompatibleMutators() {
model.setErrorCode("newErrorCode");
assertEquals("newErrorCode", model.getErrorCode());
}

@public void somethe() {
model.setErrorCode("");
assertEquals("", "");
}
}
Loading