Skip to content

Commit 6a6a869

Browse files
committed
Test unions that list each other or themselves
Both tests fail with a StackOverflowError.
1 parent a24067d commit 6a6a869

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

‎src/test/kotlin/graphql/kickstart/tools/SchemaParserTest.kt‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,52 @@ class SchemaParserTest {
277277
assertEquals(error.message, "Type 'Nested' used by input field 'TestInput.nested' at line 6, column 14 is not defined in the schema.")
278278
}
279279

280+
@Test
281+
fun `parser should throw descriptive exception when unions list each other`() {
282+
val error = assertThrows(SchemaError::class.java) {
283+
unionParser(
284+
"""
285+
|union Result = Book | Media
286+
|union Media = Movie | Result
287+
""".trimMargin())
288+
}
289+
290+
assertEquals(error.message, "Union 'Result' contains itself through Result -> Media -> Result at line 6, column 23.")
291+
}
292+
293+
@Test
294+
fun `parser should throw descriptive exception when union lists itself`() {
295+
val error = assertThrows(SchemaError::class.java) {
296+
unionParser("union Result = Book | Result")
297+
}
298+
299+
assertEquals(error.message, "Union 'Result' contains itself through Result -> Result at line 5, column 23.")
300+
}
301+
302+
private fun unionParser(unions: String) = SchemaParser.newParser()
303+
.schemaString(
304+
"""
305+
|type Query {
306+
| results: [Result!]!
307+
|}
308+
|
309+
|$unions
310+
|
311+
|type Book {
312+
| title: String!
313+
|}
314+
|
315+
|type Movie {
316+
| title: String!
317+
|}
318+
""".trimMargin())
319+
.resolvers(object : GraphQLQueryResolver {
320+
fun results(): List<Any> = listOf()
321+
})
322+
.dictionary(Book::class, Movie::class)
323+
.build()
324+
.makeExecutableSchema()
325+
280326
@Test
281327
fun `parser should throw descriptive exception when type was never reached by the scanner`() {
282328
val error = assertThrows(SchemaError::class.java) {
@@ -1062,6 +1108,10 @@ class SchemaParserTest {
10621108

10631109
class RootObj
10641110

1111+
data class Book(val title: String)
1112+
1113+
data class Movie(val title: String)
1114+
10651115
class ProxiedResolver : GraphQLQueryResolver {
10661116
fun test(): List<String> = listOf()
10671117
}

0 commit comments

Comments
 (0)