From 7beefa454476368c24335ef7510392df19c8afa0 Mon Sep 17 00:00:00 2001 From: Botond Botos Date: Sat, 30 May 2026 15:43:27 +0200 Subject: [PATCH] fix(cloudformation): forward ApiGatewayV2 Api properties to apigatewayv2 service (#1086) provisionApiGatewayV2Api only forwarded Name and ProtocolType, so a template with ProtocolType: WEBSOCKET failed with "RouteSelectionExpression is required for WEBSOCKET protocol" even when the field was present, and Description, ApiKeySelectionExpression, CorsConfiguration and Tags were silently dropped. Forward RouteSelectionExpression, Description and ApiKeySelectionExpression through resolveOptional, and add parseApiGatewayV2Tags / parseApiGatewayV2Cors helpers for the JSON-object Tags shape and the PascalCase CorsConfiguration sub-object that this resource type uses. Closes #1072 --- .../CloudFormationResourceProvisioner.java | 56 +++++++++++++ .../CloudFormationIntegrationTest.java | 81 +++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java index a6a1645150..0a10f321a3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java @@ -1530,12 +1530,68 @@ public class CloudFormationResourceProvisioner { Map req = new HashMap<>(); req.put("name", name); req.put("protocolType", resolveOrDefault(props, "ProtocolType", engine, "HTTP")); + req.put("routeSelectionExpression", resolveOptional(props, "RouteSelectionExpression", engine)); + req.put("description", resolveOptional(props, "Description", engine)); + req.put("apiKeySelectionExpression", resolveOptional(props, "ApiKeySelectionExpression", engine)); + + Map tags = parseApiGatewayV2Tags(props != null ? props.get("Tags") : null, engine); + if (!tags.isEmpty()) { + req.put("tags", tags); + } + + Map cors = parseApiGatewayV2Cors(props != null ? props.get("CorsConfiguration") : null, engine); + if (cors != null) { + req.put("corsConfiguration", cors); + } Api api = apiGatewayV2Service.createApi(region, req); r.setPhysicalId(api.getApiId()); r.getAttributes().put("ApiEndpoint", api.getApiEndpoint()); } + private Map parseApiGatewayV2Tags(JsonNode tagsNode, CloudFormationTemplateEngine engine) { + Map out = new HashMap<>(); + if (tagsNode == null || tagsNode.isNull()) { + return out; + } + JsonNode resolved = engine.resolveNode(tagsNode); + if (!resolved.isObject()) { + return out; + } + resolved.properties().forEach(e -> out.put(e.getKey(), e.getValue().asText(""))); + return out; + } + + private Map parseApiGatewayV2Cors(JsonNode corsNode, CloudFormationTemplateEngine engine) { + if (corsNode == null || corsNode.isNull()) { + return null; + } + JsonNode resolved = engine.resolveNode(corsNode); + if (!resolved.isObject()) { + return null; + } + Map out = new HashMap<>(); + resolved.properties().forEach(e -> { + String key = e.getKey(); + String camel = key.isEmpty() || !Character.isUpperCase(key.charAt(0)) + ? key + : Character.toLowerCase(key.charAt(0)) + key.substring(1); + JsonNode v = e.getValue(); + if (v.isArray()) { + List list = new ArrayList<>(); + v.forEach(item -> list.add(item.asText())); + out.put(camel, list); + } else if (v.isBoolean()) { + out.put(camel, v.booleanValue()); + } else if (v.isNumber()) { + out.put(camel, v.numberValue()); + } else if (!v.isNull()) { + out.put(camel, v.asText()); + } + }); + return out; + } + private void provisionApiGatewayV2Route(StackResource r, JsonNode props, CloudFormationTemplateEngine engine, String region) { String apiId = resolveOptional(props, "ApiId", engine); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java index f800c437ea..ac5443cfa3 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java @@ -4691,4 +4691,85 @@ class CloudFormationIntegrationTest { .body(containsString("nested-stack-child-queue")); } + // ── Issue #1072: AWS::ApiGatewayV2::Api WEBSOCKET drops RouteSelectionExpression ─── + + @Test + void createStack_apiGatewayV2WebSocketApi_forwardsRouteSelectionExpression() { + String template = """ + { + "Resources": { + "MyWsApi": { + "Type": "AWS::ApiGatewayV2::Api", + "Properties": { + "Name": "cfn-ws-api", + "ProtocolType": "WEBSOCKET", + "RouteSelectionExpression": "$request.body.action", + "Description": "ws api created via cfn", + "ApiKeySelectionExpression": "$request.header.x-custom-key", + "CorsConfiguration": { + "AllowOrigins": ["https://example.com"], + "AllowMethods": ["GET", "POST"], + "AllowHeaders": ["content-type"], + "ExposeHeaders": ["x-request-id"], + "MaxAge": 600, + "AllowCredentials": true + }, + "Tags": { + "Environment": "test", + "Owner": "floci" + } + } + } + } + } + """; + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "CreateStack") + .formParam("StackName", "ws-api-stack") + .formParam("TemplateBody", template) + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("")); + + given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DescribeStackResources") + .formParam("StackName", "ws-api-stack") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("MyWsApi")) + .body(containsString("CREATE_COMPLETE")) + .body(not(containsString("CREATE_FAILED"))); + + String apisJson = given() + .header("X-Amz-Target", "AmazonApiGatewayV2.GetApis") + .contentType("application/x-amz-json-1.1") + .body("{}") + .when() + .post("/") + .then() + .statusCode(200) + .extract().asString(); + + assertThat(apisJson, containsString("\"Name\":\"cfn-ws-api\"")); + assertThat(apisJson, containsString("\"ProtocolType\":\"WEBSOCKET\"")); + assertThat(apisJson, containsString("\"RouteSelectionExpression\":\"$request.body.action\"")); + assertThat(apisJson, containsString("\"Description\":\"ws api created via cfn\"")); + assertThat(apisJson, containsString("\"ApiKeySelectionExpression\":\"$request.header.x-custom-key\"")); + assertThat(apisJson, containsString("\"Environment\":\"test\"")); + assertThat(apisJson, containsString("\"Owner\":\"floci\"")); + assertThat(apisJson, containsString("\"AllowOrigins\":[\"https://example.com\"]")); + assertThat(apisJson, containsString("\"AllowMethods\":[\"GET\",\"POST\"]")); + assertThat(apisJson, containsString("\"AllowHeaders\":[\"content-type\"]")); + assertThat(apisJson, containsString("\"ExposeHeaders\":[\"x-request-id\"]")); + assertThat(apisJson, containsString("\"MaxAge\":600")); + assertThat(apisJson, containsString("\"AllowCredentials\":true")); + } + }