From 44eb096ed5f470894fac6cefce163466edb1a951 Mon Sep 17 00:00:00 2001 From: Jackson Price <59149601+jprice8@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:42:57 -0600 Subject: [PATCH] fix(appsync): require definition and format when creating types (#8157) --- .../test/java/com/floci/test/AppSyncTest.java | 2 + .../aws/services/appsync/AppSyncService.java | 14 ++- .../AppSyncCreateTypeIntegrationTest.java | 86 +++++++++++++++++++ .../appsync/AppSyncIntegrationTest.java | 34 ++++++-- .../services/appsync/AppSyncServiceTest.java | 53 ++++++++++++ 5 files changed, 177 insertions(+), 12 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/appsync/AppSyncCreateTypeIntegrationTest.java diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AppSyncTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AppSyncTest.java index e3d4052a8f..38f6f69575 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AppSyncTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AppSyncTest.java @@ -552,6 +552,7 @@ class AppSyncTest { CreateTypeResponse resp = client.createType(CreateTypeRequest.builder() .apiId(apiId) .definition("type Item { id: ID!, name: String }") + .format(TypeDefinitionFormat.SDL) .build()); assertThat(resp.type()).isNotNull(); @@ -601,6 +602,7 @@ class AppSyncTest { client.createType(CreateTypeRequest.builder() .apiId(apiId) .definition("type TempType { id: ID }") + .format(TypeDefinitionFormat.SDL) .build()); DeleteTypeResponse resp = client.deleteType(DeleteTypeRequest.builder() diff --git a/src/main/java/io/floci/aws/services/appsync/AppSyncService.java b/src/main/java/io/floci/aws/services/appsync/AppSyncService.java index ba931b9206..cfb0bc9966 100644 --- a/src/main/java/io/floci/aws/services/appsync/AppSyncService.java +++ b/src/main/java/io/floci/aws/services/appsync/AppSyncService.java @@ -579,8 +579,11 @@ public class AppSyncService { public synchronized AppSyncType createType(String apiId, Map request) { assertSchemaNotBusy(apiId); getGraphqlApi(apiId); - String name = (String) request.get("name"); String definition = (String) request.get("definition"); + if (definition == null) { + throw new AwsException("BadRequestException", "A type definition is required", 400); + } + String name = (String) request.get("name"); if (name == null || name.isBlank()) { name = extractTypeNameFromDefinition(definition); } @@ -591,12 +594,17 @@ public class AppSyncService { if (typeStore.get(typeKey).isPresent()) { throw new AwsException("BadRequestException", "Type with name %s already exists.".formatted(name), 400); } + Object formatValue = request.get("format"); + if (formatValue == null) { + throw new AwsException("BadRequestException", "A type format is required", 400); + } + TypeFormat format = parseEnum(TypeFormat.class, formatValue); AppSyncType type = new AppSyncType(); type.setApiId(apiId); type.setName(name); - type.setDefinition((String) request.get("definition")); + type.setDefinition(definition); type.setDescription((String) request.get("description")); - type.setFormat(parseEnum(TypeFormat.class, request.getOrDefault("format", "SDL"))); + type.setFormat(format); typeStore.put(typeKey, type); return type; diff --git a/src/test/java/io/floci/aws/services/appsync/AppSyncCreateTypeIntegrationTest.java b/src/test/java/io/floci/aws/services/appsync/AppSyncCreateTypeIntegrationTest.java new file mode 100644 index 0000000000..1d75889672 --- /dev/null +++ b/src/test/java/io/floci/aws/services/appsync/AppSyncCreateTypeIntegrationTest.java @@ -0,0 +1,86 @@ +package io.floci.aws.services.appsync; + +import io.floci.aws.testing.AwsTestClients; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.appsync.AppSyncClient; +import software.amazon.awssdk.services.appsync.model.AuthenticationType; +import software.amazon.awssdk.services.appsync.model.BadRequestException; +import software.amazon.awssdk.services.appsync.model.NotFoundException; +import software.amazon.awssdk.services.appsync.model.Type; +import software.amazon.awssdk.services.appsync.model.TypeDefinitionFormat; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class AppSyncCreateTypeIntegrationTest { + + @ParameterizedTest + @EnumSource(value = TypeDefinitionFormat.class, names = {"SDL", "JSON"}) + void missingDefinitionDoesNotStoreTheTypeAndValidCreationStillSucceeds(TypeDefinitionFormat format) { + try (AppSyncClient client = AwsTestClients.create( + AppSyncClient.builder(), "741648700016", Region.US_EAST_1)) { + String apiId = client.createGraphqlApi(request -> request.name("type-create-" + UUID.randomUUID()) + .authenticationType(AuthenticationType.API_KEY)).graphqlApi().apiId(); + try { + BadRequestException refused = assertThrows(BadRequestException.class, + () -> client.createType(request -> request.apiId(apiId).format(format))); + + assertEquals(400, refused.statusCode()); + assertTrue(refused.getMessage().contains("definition")); + assertTrue(client.listTypes(request -> request.apiId(apiId).format(TypeDefinitionFormat.SDL)) + .types().isEmpty()); + assertThrows(NotFoundException.class, () -> client.getType(request -> request.apiId(apiId) + .typeName("Item").format(TypeDefinitionFormat.SDL))); + + Type created = client.createType(request -> request.apiId(apiId) + .definition("type Item { id: ID }").format(TypeDefinitionFormat.SDL)).type(); + assertEquals("Item", created.name()); + assertEquals("type Item { id: ID }", created.definition()); + } finally { + client.deleteGraphqlApi(request -> request.apiId(apiId)); + } + } + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = {"BOGUS", ""}) + void rejectedFormatDoesNotStoreTheTypeAndValidCreationStillSucceeds(String format) { + try (AppSyncClient client = AwsTestClients.create( + AppSyncClient.builder(), "741648700016", Region.US_EAST_1)) { + String apiId = client.createGraphqlApi(request -> request.name("type-create-" + UUID.randomUUID()) + .authenticationType(AuthenticationType.API_KEY)).graphqlApi().apiId(); + try { + String definition = "type Item { id: ID }"; + BadRequestException refused = assertThrows(BadRequestException.class, + () -> client.createType(request -> request.apiId(apiId) + .definition(definition).format(format))); + + assertEquals(400, refused.statusCode()); + assertTrue(client.listTypes(request -> request.apiId(apiId).format(TypeDefinitionFormat.SDL)) + .types().isEmpty()); + assertThrows(NotFoundException.class, () -> client.getType(request -> request.apiId(apiId) + .typeName("Item").format(TypeDefinitionFormat.SDL))); + + Type created = client.createType(request -> request.apiId(apiId) + .definition(definition).format(TypeDefinitionFormat.SDL)).type(); + assertEquals("Item", created.name()); + assertEquals(definition, created.definition()); + assertEquals(TypeDefinitionFormat.SDL, created.format()); + assertEquals(created, client.getType(request -> request.apiId(apiId).typeName("Item") + .format(TypeDefinitionFormat.SDL)).type()); + } finally { + client.deleteGraphqlApi(request -> request.apiId(apiId)); + } + } + } +} diff --git a/src/test/java/io/floci/aws/services/appsync/AppSyncIntegrationTest.java b/src/test/java/io/floci/aws/services/appsync/AppSyncIntegrationTest.java index 0d8cde72a4..3b2e6536b5 100644 --- a/src/test/java/io/floci/aws/services/appsync/AppSyncIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/appsync/AppSyncIntegrationTest.java @@ -388,7 +388,8 @@ class AppSyncIntegrationTest { .body(""" { "name": "Query", - "definition": "type Query { hello: String, getItem(id: ID!): Item }" + "definition": "type Query { hello: String, getItem(id: ID!): Item }", + "format": "SDL" } """) .when() @@ -448,7 +449,8 @@ class AppSyncIntegrationTest { .body(""" { "name": "TempType", - "definition": "type TempType { id: ID }" + "definition": "type TempType { id: ID }", + "format": "SDL" } """) .when() @@ -943,12 +945,15 @@ class AppSyncIntegrationTest { @Test @Order(108) - void createTypeMissingNameReturns400() { + void createTypeInfersNameAndRejectsMissingDefinition() { given() .header("Authorization", AUTH) .contentType("application/json") .body(""" - {"definition": "type Foo { id: ID }"} + { + "definition": "type Foo { id: ID }", + "format": "SDL" + } """) .when() .post("/v1/apis/" + apiId + "/types") @@ -959,14 +964,16 @@ class AppSyncIntegrationTest { .header("Authorization", AUTH) .contentType("application/json") .body(""" - {} + { + "format": "SDL" + } """) .when() .post("/v1/apis/" + apiId + "/types") .then() .statusCode(400) .body("__type", equalTo("BadRequestException")) - .body("message", containsString("A type name is required")); + .body("message", containsString("A type definition is required")); } @Test @@ -1139,7 +1146,8 @@ class AppSyncIntegrationTest { .body(""" { "name": "StandaloneDeleteType", - "definition": "type StandaloneDeleteType { id: ID }" + "definition": "type StandaloneDeleteType { id: ID }", + "format": "SDL" } """) .when() @@ -1627,7 +1635,11 @@ class AppSyncIntegrationTest { .header("Authorization", AUTH) .contentType("application/json") .body(""" - {"name": "%s", "definition": "type %s { id: ID }"} + { + "name": "%s", + "definition": "type %s { id: ID }", + "format": "SDL" + } """.formatted(typeName, typeName)) .when() .post("/v1/apis/" + apiId + "/types") @@ -1638,7 +1650,11 @@ class AppSyncIntegrationTest { .header("Authorization", AUTH) .contentType("application/json") .body(""" - {"name": "%s", "definition": "type %s { id: ID }"} + { + "name": "%s", + "definition": "type %s { id: ID }", + "format": "SDL" + } """.formatted(typeName, typeName)) .when() .post("/v1/apis/" + apiId + "/types") diff --git a/src/test/java/io/floci/aws/services/appsync/AppSyncServiceTest.java b/src/test/java/io/floci/aws/services/appsync/AppSyncServiceTest.java index 1dbfd12452..aceeaa29d6 100644 --- a/src/test/java/io/floci/aws/services/appsync/AppSyncServiceTest.java +++ b/src/test/java/io/floci/aws/services/appsync/AppSyncServiceTest.java @@ -707,4 +707,57 @@ class AppSyncServiceTest { assertEquals("original", service.getType(api.getApiId(), "Item").getDescription()); assertEquals("SDL", service.getType(api.getApiId(), "Item").getFormat().name()); } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void createTypeRejectsMissingDefinitionEvenWithAnExplicitName(boolean explicitNull) { + GraphqlApi api = service.createGraphqlApi( + Map.of("name", "types", "authenticationType", "API_KEY"), "us-east-1"); + Map request = new HashMap<>(); + request.put("name", "Item"); + request.put("format", "SDL"); + if (explicitNull) { + request.put("definition", null); + } + + AwsException refused = assertThrows(AwsException.class, + () -> service.createType(api.getApiId(), request)); + + assertEquals("BadRequestException", refused.getErrorCode()); + assertEquals(400, refused.getHttpStatus()); + assertTrue(refused.getMessage().contains("definition")); + assertTrue(service.listTypes(api.getApiId(), null, null).items().isEmpty()); + assertEquals("NotFoundException", assertThrows(AwsException.class, + () -> service.getType(api.getApiId(), "Item")).getErrorCode()); + + request.put("definition", "type Item { id: ID }"); + service.createType(api.getApiId(), request); + assertEquals("type Item { id: ID }", service.getType(api.getApiId(), "Item").getDefinition()); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void createTypeRejectsMissingFormatWithoutStoringTheType(boolean explicitNull) { + GraphqlApi api = service.createGraphqlApi( + Map.of("name", "types", "authenticationType", "API_KEY"), "us-east-1"); + Map request = new HashMap<>(); + request.put("definition", "type Item { id: ID }"); + if (explicitNull) { + request.put("format", null); + } + + AwsException refused = assertThrows(AwsException.class, + () -> service.createType(api.getApiId(), request)); + + assertEquals("BadRequestException", refused.getErrorCode()); + assertEquals(400, refused.getHttpStatus()); + assertTrue(refused.getMessage().contains("format")); + assertTrue(service.listTypes(api.getApiId(), null, null).items().isEmpty()); + assertEquals("NotFoundException", assertThrows(AwsException.class, + () -> service.getType(api.getApiId(), "Item")).getErrorCode()); + + request.put("format", "SDL"); + service.createType(api.getApiId(), request); + assertEquals("SDL", service.getType(api.getApiId(), "Item").getFormat().name()); + } }