mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 09:22:49 -05:00
fix(appsync): avoid duplicate AWS scalar definitions (#8612)
* fix(appsync): avoid duplicate AWS scalar definitions * test(appsync): cover customer AWS scalar declarations * fix(appsync): scan top-level scalar declarations * test(appsync): cover scalar token edge cases * fix(test): use newline separators in SDL cases * fix(appsync): align SDL name tokenization * refactor(appsync): simplify scalar declaration scan
This commit is contained in:
1 parent
07f6dea386
commit
580781847a
2 files changed
+204
-6
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user