fix(apigateway): store and return a REST API's minimumCompressionSize (#9515)

This commit is contained in:
David authored and GitHub committed 2026-11-27 07:33:35 -04:00
1 parent ed6ec25b12
commit 7a54f4ac0f
4 files changed
+224 -1

No files matched your search

@@ -2375,6 +2375,10 @@ public class ApiGatewayController {
}
node.put("apiKeySource", api.getApiKeySource());
// Absent while compression is off, as on AWS.
if (api.getMinimumCompressionSize() != null) {
node.put("minimumCompressionSize", api.getMinimumCompressionSize());
}
// Not settable on a REST API here, so the emulated value is always the AWS default.
// Report it rather than omitting it, because a client that reads an absent member back
// as null sees it as a difference from the configuration it just sent and never converges.
@@ -175,6 +175,9 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
private static final String EPC_VPC_IDS_KEY = "vpcEndpointIds";
private static final String EPC_TYPES_PATH = "/endpointConfiguration/types/";
private static final String EPC_VPC_IDS_PATH = "/endpointConfiguration/vpcEndpointIds";
private static final String MIN_COMPRESSION_PATH = "/minimumCompressionSize";
/** The largest {@code minimumCompressionSize} AWS accepts: 10 MiB. */
private static final int MAX_MINIMUM_COMPRESSION_SIZE = 10_485_760;
private static final String TLS_1_2 = "TLS_1_2";
/** The API type a mapping to a REST API is stored with, beside a v2 API's protocol type. */
private static final String REST_API_TYPE = "REST";
@@ -356,6 +359,9 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
if (request.get("apiKeySource") != null) {
api.setApiKeySource(apiKeySource(request.get("apiKeySource")));
}
if (request.get("minimumCompressionSize") != null) {
api.setMinimumCompressionSize(minimumCompressionSize(request.get("minimumCompressionSize")));
}
EndpointConfiguration endpointConfiguration = new EndpointConfiguration();
if (request.get(EPC_KEY) instanceof Map<?, ?> epMap) {
@@ -415,6 +421,28 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
throw new AwsException("BadRequestException", "API key source must be HEADER or AUTHORIZER.", 400);
}
/**
* A valid {@code minimumCompressionSize}: a whole number of bytes from 0 to 10485760. The value
* is a JSON number on create and a string in a patch operation, so both are read.
*/
private static Integer minimumCompressionSize(Object value) {
Long size = null;
if (value instanceof Integer || value instanceof Long || value instanceof Short) {
size = ((Number) value).longValue();
} else if (value instanceof String text) {
try {
size = Long.parseLong(text.trim());
} catch (NumberFormatException ignored) {
// Not a whole number of bytes: refused below.
}
}
if (size == null || size < 0 || size > MAX_MINIMUM_COMPRESSION_SIZE) {
throw new AwsException("BadRequestException",
"Invalid minimum compression size, must be between 0 and 10485760", 400);
}
return size.intValue();
}
/** The endpoint type {@code value} names, in any case. */
private static EndpointType endpointType(String value) {
try {
@@ -3668,8 +3696,13 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
// endpoint change or API key source must leave it as it was.
EndpointConfiguration endpointConfiguration = patchEndpointConfiguration(api, patchOperations, region);
for (Map<String, String> op : patchOperations) {
if ("replace".equals(op.get("op")) && "/apiKeySource".equals(op.get("path"))) {
if (!"replace".equals(op.get("op"))) {
continue;
}
if ("/apiKeySource".equals(op.get("path"))) {
apiKeySource(op.get("value"));
} else if (MIN_COMPRESSION_PATH.equals(op.get("path")) && !clearsCompression(op.get("value"))) {
minimumCompressionSize(op.get("value"));
}
}
for (Map<String, String> op : patchOperations) {
@@ -3689,6 +3722,10 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
} else if ("/apiKeySource" .equals(path)) {
// aws_api_gateway_rest_api sends this for a changed api_key_source.
api.setApiKeySource(apiKeySource(value));
} else if (MIN_COMPRESSION_PATH.equals(path)) {
// A patch without a value turns compression off, which is how
// aws_api_gateway_rest_api clears minimum_compression_size.
api.setMinimumCompressionSize(clearsCompression(value) ? null : minimumCompressionSize(value));
}
}
if (endpointConfiguration != null) {
@@ -3699,6 +3736,11 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
return api;
}
/** True when a {@code /minimumCompressionSize} patch value turns compression off. */
private static boolean clearsCompression(String value) {
return value == null || value.isBlank();
}
/**
* The endpoint configuration {@code patchOperations} leave {@code api} with, or null when none of
* them touches it. {@code replace /endpointConfiguration/types/<type>} changes the type: the path
@@ -31,8 +31,21 @@ public class RestApi {
* default) or {@code AUTHORIZER} (the {@code usageIdentifierKey} a Lambda authorizer returns).
*/
private String apiKeySource = API_KEY_SOURCE_HEADER;
/**
* The smallest payload, in bytes, that compression applies to: 0 to 10485760, or null when
* compression is off, which is AWS's default.
*/
private Integer minimumCompressionSize;
private List<String> warnings = new ArrayList<>();
public Integer getMinimumCompressionSize() {
return minimumCompressionSize;
}
public void setMinimumCompressionSize(Integer minimumCompressionSize) {
this.minimumCompressionSize = minimumCompressionSize;
}
public String getApiKeySource() {
return apiKeySource;
}
@@ -0,0 +1,164 @@
package io.floci.aws.services.apigateway;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import io.restassured.response.ValidatableResponse;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
/**
* A REST API's {@code minimumCompressionSize}: a nullable integer between 0 and 10485760 that is
* kept on create and on a {@code /minimumCompressionSize} patch, reported by GetRestApi, and
* cleared by a patch with no value. Terraform's {@code aws_api_gateway_rest_api} reads it back as
* {@code minimum_compression_size}, so a dropped value shows a change on every plan.
*
* @see <a href="https://docs.aws.amazon.com/apigateway/latest/api/API_RestApi.html">RestApi</a>
*/
@QuarkusTest
class ApiGatewayMinimumCompressionSizeIntegrationTest {
private static final String MEMBER = "minimumCompressionSize";
private static final String OUT_OF_RANGE = "Invalid minimum compression size, must be between 0 and 10485760";
private final List<String> apiIds = new ArrayList<>();
@AfterEach
void deleteApis() {
apiIds.forEach(id -> given().delete("/restapis/" + id).then().statusCode(202));
apiIds.clear();
}
private ValidatableResponse create(Map<String, Object> body) {
return given().contentType(ContentType.JSON).body(body).post("/restapis").then();
}
private String createApi(String name, Object minimumCompressionSize) {
Map<String, Object> body = new HashMap<>();
body.put("name", name);
body.put(MEMBER, minimumCompressionSize);
return createApi(body);
}
private String createApi(Map<String, Object> body) {
String id = create(body).statusCode(201).extract().path("id");
apiIds.add(id);
return id;
}
@SafeVarargs
private ValidatableResponse patch(String apiId, Map<String, String>... operations) {
return given().contentType(ContentType.JSON)
.body(Map.of("patchOperations", List.of(operations)))
.patch("/restapis/" + apiId).then();
}
private static Map<String, String> replace(String path, String value) {
Map<String, String> op = new HashMap<>();
op.put("op", "replace");
op.put("path", path);
op.put("value", value);
return op;
}
/** A replace that carries no {@code value} key at all, as Terraform sends to turn compression off. */
private static Map<String, String> replaceWithoutValue(String path) {
return Map.of("op", "replace", "path", path);
}
private ValidatableResponse get(String apiId) {
return given().get("/restapis/" + apiId).then().statusCode(200);
}
@Test
void createRestApiKeepsTheSizeAndGetRestApiReportsIt() {
Map<String, Object> body = Map.of("name", "compression-create", "minimumCompressionSize", 1024);
String apiId = create(body).statusCode(201)
.body("minimumCompressionSize", equalTo(1024))
.extract().path("id");
apiIds.add(apiId);
get(apiId).body("minimumCompressionSize", equalTo(1024));
}
@Test
void zeroIsKeptBecauseItEnablesCompressionForEveryPayload() {
String apiId = createApi("compression-zero", 0);
get(apiId).body("minimumCompressionSize", equalTo(0));
}
@Test
void anApiCreatedWithoutASizeOmitsTheMember() {
String omitted = createApi(Map.of("name", "compression-omitted"));
String explicitNull = createApi("compression-null", null);
get(omitted).body("$", not(hasKey(MEMBER)));
get(explicitNull).body("$", not(hasKey(MEMBER)));
}
@Test
void theBoundsOfTheRangeAreAccepted() {
String apiId = createApi("compression-max", 10485760);
get(apiId).body("minimumCompressionSize", equalTo(10485760));
}
@Test
void aPatchReplacesTheSizeAndAPatchWithoutAValueClearsIt() {
String apiId = createApi("compression-patch", null);
patch(apiId, replace("/minimumCompressionSize", "2048")).statusCode(200)
.body("minimumCompressionSize", equalTo(2048));
get(apiId).body("minimumCompressionSize", equalTo(2048));
// Terraform clears minimum_compression_size by leaving the value out of the operation.
patch(apiId, replaceWithoutValue("/minimumCompressionSize")).statusCode(200)
.body("$", not(hasKey(MEMBER)));
get(apiId).body("$", not(hasKey(MEMBER)));
// An explicit null or an empty value clears it the same way.
patch(apiId, replace("/minimumCompressionSize", "512")).statusCode(200);
patch(apiId, replace("/minimumCompressionSize", null)).statusCode(200)
.body("$", not(hasKey(MEMBER)));
patch(apiId, replace("/minimumCompressionSize", "512")).statusCode(200);
patch(apiId, replace("/minimumCompressionSize", "")).statusCode(200)
.body("$", not(hasKey(MEMBER)));
get(apiId).body("$", not(hasKey(MEMBER)));
}
@Test
void aSizeOutsideTheRangeIsRefusedOnCreate() {
create(Map.of("name", "compression-negative", MEMBER, -1)).statusCode(400)
.body("message", equalTo(OUT_OF_RANGE));
create(Map.of("name", "compression-too-big", MEMBER, 10485761)).statusCode(400)
.body("message", equalTo(OUT_OF_RANGE));
create(Map.of("name", "compression-text", MEMBER, "big")).statusCode(400)
.body("message", equalTo(OUT_OF_RANGE));
}
@Test
void aRefusedPatchLeavesTheApiAsItWas() {
String apiId = createApi("compression-refused", 1024);
patch(apiId, replace("/minimumCompressionSize", "-1")).statusCode(400)
.body("message", equalTo(OUT_OF_RANGE));
patch(apiId, replace("/minimumCompressionSize", "10485761")).statusCode(400)
.body("message", equalTo(OUT_OF_RANGE));
patch(apiId, replace("/minimumCompressionSize", "1k")).statusCode(400)
.body("message", equalTo(OUT_OF_RANGE));
// The operations before the refused one in the same request are not applied either.
patch(apiId, replace("/name", "renamed"), replace("/minimumCompressionSize", "1k")).statusCode(400);
get(apiId).body("name", equalTo("compression-refused"), "minimumCompressionSize", equalTo(1024));
}
}