fix(apigatewayv2): include corsConfiguration on Api management responses (#933)

`Api` does not currently model `corsConfiguration`, so neither
`ApiGatewayV2Service.createApi` / `updateApi` accept the field from the
inbound request nor do the two response builders
(`ApiGatewayController.toV2ApiNode` and `ApiGatewayV2JsonHandler.toApiNode`)
emit it. Terraform's `aws_apigatewayv2_api.cors_configuration` is therefore
always perceived as drift on every plan against a correctly-configured
emulator.

Introduces a nested `Api.Cors` record mirroring the `Authorizer.JwtConfiguration`
pattern (six AWS-documented sub-fields: allowOrigins, allowMethods, allowHeaders,
exposeHeaders, maxAge, allowCredentials). Adds request extraction via a shared
`toCors(Map)` helper (boolean uses the same defensive
`Boolean.parseBoolean(String.valueOf(...))` style as `Stage.autoDeploy`) and
emits the value from both REST and JSON 1.1 response paths.

Covered by a focused regression test (`ApiCorsConfigurationResponseTest`)
exercising create/get/list, PATCH update, omitted-when-null, the
containsKey-guard against future regression on PATCH-other-field, and a
JSON 1.1 round-trip via `X-Amz-Target` dispatch.

Same Terraform-drift class as #887 (`Route.authorizerId`) and #931
(`Integration.connectionType`, `Authorizer.enableSimpleResponses`).
This commit is contained in:
Yoshiyuki Ohta authored and GitHub committed 2026-05-19 21:45:41 -05:00
1 parent c6928128ff
commit 4c156df10b
5 files changed
+296

No files matched your search

@@ -1649,6 +1649,32 @@ public class ApiGatewayController {
api.getTags().forEach(tagsNode::put);
node.set("tags", tagsNode);
}
if (api.getCorsConfiguration() != null) {
node.set("corsConfiguration", toV2CorsNode(api.getCorsConfiguration()));
}
return node;
}
private ObjectNode toV2CorsNode(Api.Cors cors) {
ObjectNode node = objectMapper.createObjectNode();
if (cors.allowOrigins() != null) {
ArrayNode arr = node.putArray("allowOrigins");
cors.allowOrigins().forEach(arr::add);
}
if (cors.allowMethods() != null) {
ArrayNode arr = node.putArray("allowMethods");
cors.allowMethods().forEach(arr::add);
}
if (cors.allowHeaders() != null) {
ArrayNode arr = node.putArray("allowHeaders");
cors.allowHeaders().forEach(arr::add);
}
if (cors.exposeHeaders() != null) {
ArrayNode arr = node.putArray("exposeHeaders");
cors.exposeHeaders().forEach(arr::add);
}
if (cors.maxAge() != null) node.put("maxAge", cors.maxAge());
if (cors.allowCredentials() != null) node.put("allowCredentials", cors.allowCredentials());
return node;
}
@@ -508,6 +508,32 @@ public class ApiGatewayV2JsonHandler {
ObjectNode tagsNode = node.putObject("Tags");
api.getTags().forEach(tagsNode::put);
}
if (api.getCorsConfiguration() != null) {
node.set("CorsConfiguration", toCorsNode(api.getCorsConfiguration()));
}
return node;
}
private ObjectNode toCorsNode(Api.Cors cors) {
ObjectNode node = objectMapper.createObjectNode();
if (cors.allowOrigins() != null) {
ArrayNode arr = node.putArray("AllowOrigins");
cors.allowOrigins().forEach(arr::add);
}
if (cors.allowMethods() != null) {
ArrayNode arr = node.putArray("AllowMethods");
cors.allowMethods().forEach(arr::add);
}
if (cors.allowHeaders() != null) {
ArrayNode arr = node.putArray("AllowHeaders");
cors.allowHeaders().forEach(arr::add);
}
if (cors.exposeHeaders() != null) {
ArrayNode arr = node.putArray("ExposeHeaders");
cors.exposeHeaders().forEach(arr::add);
}
if (cors.maxAge() != null) node.put("MaxAge", cors.maxAge());
if (cors.allowCredentials() != null) node.put("AllowCredentials", cors.allowCredentials());
return node;
}
@@ -98,6 +98,12 @@ public class ApiGatewayV2Service {
api.setTags(tags);
}
@SuppressWarnings("unchecked")
Map<String, Object> corsConfig = (Map<String, Object>) request.get("corsConfiguration");
if (corsConfig != null) {
api.setCorsConfiguration(toCors(corsConfig));
}
apiStore.put(apiKey(region, api.getApiId()), api);
LOG.infov("Created {0} API: {1} ({2}) in {3}", protocolType, api.getName(), api.getApiId(), region);
return api;
@@ -138,11 +144,32 @@ public class ApiGatewayV2Service {
Map<String, String> tags = (Map<String, String>) request.get("tags");
api.setTags(tags);
}
if (request.containsKey("corsConfiguration")) {
@SuppressWarnings("unchecked")
Map<String, Object> corsConfig = (Map<String, Object>) request.get("corsConfiguration");
api.setCorsConfiguration(corsConfig == null ? null : toCors(corsConfig));
}
apiStore.put(apiKey(region, apiId), api);
return api;
}
private static Api.Cors toCors(Map<String, Object> m) {
@SuppressWarnings("unchecked")
List<String> allowOrigins = (List<String>) m.get("allowOrigins");
@SuppressWarnings("unchecked")
List<String> allowMethods = (List<String>) m.get("allowMethods");
@SuppressWarnings("unchecked")
List<String> allowHeaders = (List<String>) m.get("allowHeaders");
@SuppressWarnings("unchecked")
List<String> exposeHeaders = (List<String>) m.get("exposeHeaders");
Integer maxAge = m.get("maxAge") == null ? null : ((Number) m.get("maxAge")).intValue();
Boolean allowCredentials = m.get("allowCredentials") == null
? null
: Boolean.parseBoolean(String.valueOf(m.get("allowCredentials")));
return new Api.Cors(allowOrigins, allowMethods, allowHeaders, exposeHeaders, maxAge, allowCredentials);
}
// ──────────────────────────── Authorizer CRUD ────────────────────────────
public Authorizer createAuthorizer(String region, String apiId, Map<String, Object> request) {
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RegisterForReflection
@@ -18,6 +19,7 @@ public class Api {
private String routeSelectionExpression;
private String description;
private String apiKeySelectionExpression;
private Cors corsConfiguration;
public Api() {}
@@ -47,4 +49,16 @@ public class Api {
public String getApiKeySelectionExpression() { return apiKeySelectionExpression; }
public void setApiKeySelectionExpression(String apiKeySelectionExpression) { this.apiKeySelectionExpression = apiKeySelectionExpression; }
public Cors getCorsConfiguration() { return corsConfiguration; }
public void setCorsConfiguration(Cors corsConfiguration) { this.corsConfiguration = corsConfiguration; }
@RegisterForReflection
public record Cors(
List<String> allowOrigins,
List<String> allowMethods,
List<String> allowHeaders,
List<String> exposeHeaders,
Integer maxAge,
Boolean allowCredentials) {}
}