mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(appsync): require definition and format when creating types (#8157)
This commit is contained in:
1 parent
d05710e1d9
commit
44eb096ed5
5 files changed
+177
-12
No files matched your search
@@ -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()
|
||||
|
||||
@@ -579,8 +579,11 @@ public class AppSyncService {
|
||||
public synchronized AppSyncType createType(String apiId, Map<String, Object> 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;
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
@@ -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<String, Object> 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<String, Object> 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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user