diff --git a/src/main/java/io/floci/aws/services/appsync/graphql/SidecarSchemaCompiler.java b/src/main/java/io/floci/aws/services/appsync/graphql/SidecarSchemaCompiler.java index ea2ec63b5a..e9df428c8a 100644 --- a/src/main/java/io/floci/aws/services/appsync/graphql/SidecarSchemaCompiler.java +++ b/src/main/java/io/floci/aws/services/appsync/graphql/SidecarSchemaCompiler.java @@ -18,7 +18,7 @@ import java.util.regex.Pattern; * Prepares a customer's SDL for the (AWS-agnostic) GraphQL sidecar and validates it against it * (issue #2917): what {@code AppSyncSchemaParser} used to do in-process. AppSync provides its * directive declarations ({@code @aws_auth} etc.) and the 17 custom scalar type declarations - * implicitly: customers never write {@code scalar AWSDateTime} themselves. That injection is + * implicitly, though customers may also declare these scalar types in their own SDL. That injection is * pure AWS-specific knowledge (which directives exist, their exact argument shapes) with no * business living in the sidecar, so it happens here, before the SDL is ever sent anywhere. */ @@ -83,23 +83,23 @@ public class SidecarSchemaCompiler { /** The SDL to actually send the sidecar for plan/execute: same injection as at validation time. */ public String withDirectivesAndScalars(String sdl) { + List declaredScalars = declaredScalars(sdl); StringBuilder sb = new StringBuilder(); for (AppSyncDirective directive : AppSyncDirective.values()) { sb.append(directive.sdl()).append('\n'); } sb.append('\n'); for (String scalarName : SCALAR_NAMES) { - sb.append("scalar ").append(scalarName).append('\n'); + if (!declaredScalars.contains(scalarName)) { + sb.append("scalar ").append(scalarName).append('\n'); + } } sb.append('\n').append(sdl); return sb.toString(); } private void validateNoUnknownDirectives(String sdl) { - String clean = sdl - .replaceAll("\"\"\"[\\s\\S]*?\"\"\"", "") - .replaceAll("\"(?:[^\"\\\\]|\\\\.)*\"", "") - .replaceAll("#[^\n]*", ""); + String clean = stripIgnoredTokens(sdl); Matcher matcher = DIRECTIVE_USE.matcher(clean); while (matcher.find()) { String name = matcher.group(1); @@ -111,6 +111,127 @@ public class SidecarSchemaCompiler { } } + private List declaredScalars(String sdl) { + String clean = stripIgnoredTokens(sdl); + List declaredScalars = new ArrayList<>(); + int braces = 0; + int parentheses = 0; + int brackets = 0; + String previousTopLevelToken = ""; + boolean expectingScalarName = false; + int index = 0; + while (index < clean.length()) { + char current = clean.charAt(index); + if (current == '{') { + expectingScalarName = false; + braces++; + index++; + } else if (current == '}') { + expectingScalarName = false; + braces--; + index++; + } else if (current == '(') { + expectingScalarName = false; + parentheses++; + index++; + } else if (current == ')') { + expectingScalarName = false; + parentheses--; + index++; + } else if (current == '[') { + expectingScalarName = false; + brackets++; + index++; + } else if (current == ']') { + expectingScalarName = false; + brackets--; + index++; + } else if (isGraphqlNameStart(current)) { + int tokenStart = index++; + while (index < clean.length() && isGraphqlNamePart(clean.charAt(index))) { + index++; + } + String token = clean.substring(tokenStart, index); + if (braces == 0 && parentheses == 0 && brackets == 0) { + if (expectingScalarName) { + declaredScalars.add(token); + } + expectingScalarName = token.equals("scalar") && !previousTopLevelToken.equals("extend"); + previousTopLevelToken = token; + } + } else { + if (braces == 0 && parentheses == 0 && brackets == 0 + && !Character.isWhitespace(current) && current != ',') { + expectingScalarName = false; + previousTopLevelToken = String.valueOf(current); + } + index++; + } + } + return declaredScalars; + } + + private boolean isGraphqlNameStart(char value) { + return value == '_' || value >= 'A' && value <= 'Z' || value >= 'a' && value <= 'z'; + } + + private boolean isGraphqlNamePart(char value) { + return isGraphqlNameStart(value) || value >= '0' && value <= '9'; + } + + /** Replaces GraphQL comments and string tokens while preserving whitespace and line breaks. */ + private String stripIgnoredTokens(String sdl) { + StringBuilder clean = new StringBuilder(sdl.length()); + int index = 0; + while (index < sdl.length()) { + char current = sdl.charAt(index); + if (current == '#') { + while (index < sdl.length() && sdl.charAt(index) != '\n' && sdl.charAt(index) != '\r') { + clean.append(' '); + index++; + } + } else if (sdl.startsWith("\"\"\"", index)) { + clean.append(" "); + index += 3; + while (index < sdl.length() && !sdl.startsWith("\"\"\"", index)) { + char blockChar = sdl.charAt(index); + if (blockChar == '\\' && sdl.startsWith("\\\"\"\"", index)) { + clean.append(" "); + index += 4; + } else { + clean.append(blockChar == '\n' || blockChar == '\r' ? blockChar : ' '); + index++; + } + } + if (index < sdl.length()) { + clean.append(" "); + index += 3; + } + } else if (current == '"') { + clean.append(' '); + index++; + while (index < sdl.length() && sdl.charAt(index) != '"') { + if (sdl.charAt(index) == '\\' && index + 1 < sdl.length()) { + clean.append(" "); + index += 2; + } else { + char stringChar = sdl.charAt(index); + clean.append(stringChar == '\n' || stringChar == '\r' ? stringChar : ' '); + index++; + } + } + if (index < sdl.length()) { + clean.append(' '); + index++; + } + } else { + clean.append(current); + index++; + } + } + return clean.toString(); + } + private AwsException toAwsException(SchemaValidationException e) { List> codeErrors = new ArrayList<>(); for (SchemaIssue issue : e.issues()) { diff --git a/src/test/java/io/floci/aws/services/appsync/graphql/SidecarSchemaCompilerTest.java b/src/test/java/io/floci/aws/services/appsync/graphql/SidecarSchemaCompilerTest.java new file mode 100644 index 0000000000..6d8d2fe1c2 --- /dev/null +++ b/src/test/java/io/floci/aws/services/appsync/graphql/SidecarSchemaCompilerTest.java @@ -0,0 +1,77 @@ +package io.floci.aws.services.appsync.graphql; + +import io.floci.aws.services.appsync.GraphqlSidecarClient; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; + +class SidecarSchemaCompilerTest { + + private final SidecarSchemaCompiler compiler = new SidecarSchemaCompiler(mock(GraphqlSidecarClient.class)); + + @Test + void customerAwsScalarDeclarationsAreNotDuplicated() { + String sdl = "\"\"\"customer date scalar\"\"\"\n" + + "scalar AWSDateTime\n" + + "type Query { time: AWSDateTime }"; + + String prepared = compiler.withDirectivesAndScalars(sdl); + + assertEquals(0, count(prepared.substring(0, prepared.lastIndexOf(sdl)), "scalar AWSDateTime")); + assertTrue(prepared.endsWith(sdl)); + assertEquals(1, count(prepared, "scalar AWSDateTime")); + assertEquals(1, count(prepared, "scalar AWSDate\n")); + } + + @Test + void scalarMentionsInCommentsAndDescriptionsDoNotSuppressInjection() { + String sdl = "# scalar AWSDateTime\n" + + "\"\"\"scalar AWSDateTime\"\"\"\n" + + "type Query { description: String }"; + + String prepared = compiler.withDirectivesAndScalars(sdl); + + assertEquals(1, count(prepared.substring(0, prepared.lastIndexOf(sdl)), "scalar AWSDateTime")); + assertTrue(prepared.endsWith(sdl)); + } + + @Test + void enumValuesNamedScalarAndAwsScalarDoNotSuppressInjection() { + for (String scalarName : List.of("AWSDateTime", "AWSDate")) { + String sdl = "enum Choice { scalar " + scalarName + " }\n" + + "type Query { value: " + scalarName + " }"; + + String prepared = compiler.withDirectivesAndScalars(sdl); + String injected = prepared.substring(0, prepared.lastIndexOf(sdl)); + + assertEquals(1, count(injected, "scalar " + scalarName + "\n")); + assertTrue(prepared.endsWith(sdl)); + } + } + + @Test + void commaBetweenScalarKeywordAndNameIsIgnored() { + String sdl = "scalar, AWSDateTime\ntype Query { time: AWSDateTime }"; + + String prepared = compiler.withDirectivesAndScalars(sdl); + String injected = prepared.substring(0, prepared.lastIndexOf(sdl)); + + assertEquals(0, count(injected, "scalar AWSDateTime")); + assertTrue(prepared.endsWith(sdl)); + } + + private static int count(String value, String literal) { + Matcher matcher = Pattern.compile(Pattern.quote(literal)).matcher(value); + int count = 0; + while (matcher.find()) { + count++; + } + return count; + } +}