Skip to content

Commit 9409298

Browse files
ind1gobbakerman
authored andcommitted
Fix ArrayIndexOutOfBoundsException in empty ChainedInstrumentation (graphql-java#781)
* Failing test for empty ChainedInstrumentation * Fix exception in empty ChainedInstrumentation (graphql-java#780)
1 parent 2704394 commit 9409298

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/main/java/graphql/execution/instrumentation/ChainedInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionRes
137137
ExecutionResult lastResult = prevResults.size() > 0 ? prevResults.get(prevResults.size() - 1) : executionResult;
138138
return instrumentation.instrumentExecutionResult(lastResult, parameters.withNewState(state));
139139
});
140-
return resultsFuture.thenApply((results) -> results.get(results.size() - 1));
140+
return resultsFuture.thenApply((results) -> results.isEmpty() ? executionResult : results.get(results.size() - 1));
141141
}
142142

143143
private static class ChainedInstrumentationState implements InstrumentationState {

src/test/groovy/graphql/execution/instrumentation/ChainedInstrumentationStateTest.groovy

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,32 @@ class ChainedInstrumentationStateTest extends Specification {
175175

176176
}
177177

178+
def "empty chain"() {
179+
def chainedInstrumentation = new ChainedInstrumentation(Arrays.asList())
180+
181+
def query = """
182+
query HeroNameAndFriendsQuery {
183+
hero {
184+
id
185+
}
186+
}
187+
"""
188+
189+
when:
190+
def strategy = new AsyncExecutionStrategy()
191+
def graphQL = GraphQL
192+
.newGraphQL(StarWarsSchema.starWarsSchema)
193+
.queryExecutionStrategy(strategy)
194+
.instrumentation(chainedInstrumentation)
195+
.build()
196+
197+
graphQL.execute(query)
198+
199+
then:
200+
noExceptionThrown()
201+
202+
}
203+
178204
private void assertCalls(NamedInstrumentation instrumentation) {
179205
assert instrumentation.dfInvocations[0].getFieldDefinition().name == 'hero'
180206
assert instrumentation.dfInvocations[0].getFieldTypeInfo().getPath().toList() == ['hero']

0 commit comments

Comments
 (0)