Skip to content
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

Add validation for payloadFormatVersion with HTTP APIs #688

Merged
merged 3 commits into from
Jan 12, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private static ObjectNode getIntegrationAsObject(
if (integration instanceof MockIntegrationTrait) {
integrationNode = integration.toNode().expectObjectNode().withMember("type", Node.from("mock"));
} else if (integration instanceof IntegrationTrait) {
validateTraitConfiguration((IntegrationTrait) integration, context, shape);
integrationNode = ((IntegrationTrait) integration).toExpandedNode(context.getService(), shape);
} else {
throw new OpenApiException("Unexpected integration trait: " + integration);
Expand All @@ -121,6 +122,21 @@ private static ObjectNode getIntegrationAsObject(
return integrationNode;
}

private static void validateTraitConfiguration(IntegrationTrait trait,
Context<? extends Trait> context,
OperationShape operation) {
// For HTTP APIs, API Gateway requires that the payloadFormatVersion is set on integrations.
// https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-integration.html
// If the payloadFormatVersion has not been set on an integration and the apiGatewayType has been set to "HTTP",
// the conversion fails.
ApiGatewayConfig.ApiType apiType = context.getConfig().getExtensions(ApiGatewayConfig.class)
.getApiGatewayType();
if (!trait.getPayloadFormatVersion().isPresent() && apiType.equals(ApiGatewayConfig.ApiType.HTTP)) {
throw new OpenApiException("When the 'apiGatewayType' OpenAPI conversion setting is 'HTTP', a "
+ "'payloadFormatVersion' must be set on the aws.apigateway#integration trait.");
}
}

private ObjectNode updateIntegrationWithCors(
Context<? extends Trait> context,
OperationObject operationObject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@

package software.amazon.smithy.aws.apigateway.openapi;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.openapi.OpenApiConfig;
import software.amazon.smithy.openapi.OpenApiException;
import software.amazon.smithy.openapi.fromsmithy.OpenApiConverter;
import software.amazon.smithy.openapi.model.OpenApi;
import software.amazon.smithy.utils.IoUtils;
Expand Down Expand Up @@ -56,4 +61,24 @@ public void addsIntegrationsWithoutCredentials() {

Node.assertEquals(result, expectedNode);
}

@Test
public void throwsOnInvalidIntegrationTraitForHttpApi() {
OpenApiException thrown = assertThrows(OpenApiException.class, () -> {
Model model = Model.assembler(getClass().getClassLoader())
.discoverModels(getClass().getClassLoader())
.addImport(getClass().getResource("invalid-integration-for-http-api.json"))
.assemble()
.unwrap();
OpenApiConfig config = new OpenApiConfig();
config.setService(ShapeId.from("smithy.example#Service"));
ApiGatewayConfig apiGatewayConfig = new ApiGatewayConfig();
apiGatewayConfig.setApiGatewayType(ApiGatewayConfig.ApiType.HTTP);
config.putExtensions(apiGatewayConfig);
OpenApiConverter.create().config(config).convertToNode(model);
});

assertThat(thrown.getMessage(), containsString("When the 'apiGatewayType' OpenAPI conversion setting is"
+ " 'HTTP', a 'payloadFormatVersion' must be set on the aws.apigateway#integration trait."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"type": "aws_proxy",
"credentials": "arn:aws:iam::123456789012:role/{serviceName}{operationName}LambdaRole",
"uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:{serviceName}{operationName}/invocations",
"httpMethod": "POST"
"httpMethod": "POST",
"payloadFormatVersion": "1.0"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"type": "aws_proxy",
"uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:MyServiceListPayloads/invocations",
"httpMethod": "POST",
"payloadFormatVersion": "1.0",
"credentials": "arn:aws:iam::123456789012:role/MyServiceListPayloadsLambdaRole",
"responses": {
"default": {
Expand Down Expand Up @@ -137,6 +138,7 @@
"type": "aws_proxy",
"uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:MyServiceDeletePayload/invocations",
"httpMethod": "POST",
"payloadFormatVersion": "1.0",
"credentials": "arn:aws:iam::123456789012:role/MyServiceDeletePayloadLambdaRole",
"responses": {
"default": {
Expand Down Expand Up @@ -194,6 +196,7 @@
"type": "aws_proxy",
"uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:MyServiceGetPayload/invocations",
"httpMethod": "POST",
"payloadFormatVersion": "1.0",
"credentials": "arn:aws:iam::123456789012:role/MyServiceGetPayloadLambdaRole",
"responses": {
"default": {
Expand Down Expand Up @@ -336,6 +339,7 @@
"type": "aws_proxy",
"uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:MyServicePutPayload/invocations",
"httpMethod": "POST",
"payloadFormatVersion": "1.0",
"credentials": "arn:aws:iam::123456789012:role/MyServicePutPayloadLambdaRole",
"responses": {
"default": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"type": "aws_proxy",
"uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:MyServiceListPayloads/invocations",
"httpMethod": "POST",
"payloadFormatVersion": "1.0",
"credentials": "arn:aws:iam::123456789012:role/MyServiceListPayloadsLambdaRole",
"responses": {
"default": {
Expand Down Expand Up @@ -137,6 +138,7 @@
"type": "aws_proxy",
"uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:MyServiceDeletePayload/invocations",
"httpMethod": "POST",
"payloadFormatVersion": "1.0",
"credentials": "arn:aws:iam::123456789012:role/MyServiceDeletePayloadLambdaRole",
"responses": {
"default": {
Expand Down Expand Up @@ -194,6 +196,7 @@
"type": "aws_proxy",
"uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:MyServiceGetPayload/invocations",
"httpMethod": "POST",
"payloadFormatVersion": "1.0",
"credentials": "arn:aws:iam::123456789012:role/MyServiceGetPayloadLambdaRole",
"responses": {
"default": {
Expand Down Expand Up @@ -336,6 +339,7 @@
"type": "aws_proxy",
"uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123456789012:function:MyServicePutPayload/invocations",
"httpMethod": "POST",
"payloadFormatVersion": "1.0",
"credentials": "arn:aws:iam::123456789012:role/MyServicePutPayloadLambdaRole",
"responses": {
"default": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{
"smithy": "1.0",
"shapes": {
"smithy.example#Service": {
"type": "service",
"version": "2018-03-17",
"operations": [
{
"target": "smithy.example#Operation1"
}
],
"traits": {
"aws.protocols#restJson1": {},
"aws.api#service": {
"sdkId": "Some Value"
},
"aws.apigateway#integration": {
"type": "aws",
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:012345678901:function:HelloWorld/invocations",
"httpMethod": "POST",
"credentials": "arn:aws:iam::012345678901:role/apigateway-invoke-lambda-exec-role",
"requestTemplates": {
"application/json": "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }",
"application/xml": "#set ($root=$input.path('$')) <stage>$root.name</stage> "
},
"requestParameters": {
"integration.request.path.stage": "method.request.querystring.version",
"integration.request.querystring.provider": "method.request.querystring.vendor"
},
"cacheNamespace": "cache namespace",
"cacheKeyParameters": [],
"passThroughBehavior": "never",
"responses": {
"2\\d{2}": {
"statusCode": "200",
"responseParameters": {
"method.response.header.requestId": "integration.response.header.cid"
},
"responseTemplates": {
"application/json": "#set ($root=$input.path('$')) { \"stage\": \"$root.name\", \"user-id\": \"$root.key\" }",
"application/xml": "#set ($root=$input.path('$')) <stage>$root.name</stage> "
}
},
"302": {
"statusCode": "302",
"responseParameters": {
"method.response.header.Location": "integration.response.body.redirect.url"
}
},
"default": {
"statusCode": "400",
"responseParameters": {
"method.response.header.test-method-response-header": "'static value'"
}
}
}
}
}
},
"smithy.example#Operation1": {
"type": "operation",
"input": {
"target": "smithy.example#OperationInput"
},
"output": {
"target": "smithy.example#OperationOutput"
},
"errors": [
{
"target": "smithy.example#Default"
},
{
"target": "smithy.example#Redirect"
}
],
"traits": {
"smithy.api#http": {
"method": "POST",
"uri": "/1"
}
}
},
"smithy.example#Default": {
"type": "structure",
"members": {
"testMethodResponseHeader": {
"target": "smithy.api#String",
"traits": {
"smithy.api#httpHeader": "test-method-response-header"
}
}
},
"traits": {
"smithy.api#error": "client",
"smithy.api#httpError": 400
}
},
"smithy.example#Redirect": {
"type": "structure",
"members": {
"location": {
"target": "smithy.api#String",
"traits": {
"smithy.api#httpHeader": "Location"
}
}
},
"traits": {
"smithy.api#error": "client",
"smithy.api#httpError": 302,
"smithy.api#suppress": ["HttpResponseCodeSemantics"]
}
},
"smithy.example#OperationInput": {
"type": "structure",
"members": {
"vendor": {
"target": "smithy.api#Integer",
"traits": {
"smithy.api#httpQuery": "vendor",
"smithy.api#required": {}
}
},
"version": {
"target": "smithy.api#Integer",
"traits": {
"smithy.api#httpQuery": "version",
"smithy.api#required": {}
}
}
}
},
"smithy.example#OperationOutput": {
"type": "structure",
"members": {
"requestId": {
"target": "smithy.api#String",
"traits": {
"smithy.api#httpHeader": "requestId"
}
}
}
}
}
}