Skip to content

Commit 4aa7872

Browse files
smyrickdariuszkuc
andauthored
[generator] Fix issue generating nested additional types (#615)
* Fix issue generating nested additional types We would encounter a bug if generating the additional types producded more additional types. So we now call the function recursively and clear it out before generating more. * Apply suggestions from code review Co-Authored-By: Dariusz Kuc <[email protected]> * Reverse addition order Co-authored-by: Dariusz Kuc <[email protected]>
1 parent 5b74830 commit 4aa7872

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/SchemaGenerator.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ open class SchemaGenerator(internal val config: SchemaGeneratorConfig) {
6060
builder.query(generateQueries(this, queries))
6161
builder.mutation(generateMutations(this, mutations))
6262
builder.subscription(generateSubscriptions(this, subscriptions))
63-
builder.additionalTypes(generateAdditionalTypes(additionalTypes))
63+
builder.additionalTypes(generateAdditionalTypes())
6464
builder.additionalDirectives(directives.values.toSet())
6565
builder.codeRegistry(codeRegistry.build())
6666

@@ -86,6 +86,18 @@ open class SchemaGenerator(internal val config: SchemaGeneratorConfig) {
8686
* Generate the GraphQL type for all the `additionalTypes`. They are generated as non-inputs and not as IDs.
8787
* If you need to provide more custom additional types that were not picked up from reflection of the schema objects,
8888
* you can modify the set of `additionalTypes` before you call this method.
89+
*
90+
* This function is recursive because while generating the additionalTypes it is possible to create additional types that need to be processed.
8991
*/
90-
protected open fun generateAdditionalTypes(additionalTypes: Set<KType>): Set<GraphQLType> = additionalTypes.map { generateGraphQLType(this, it) }.toSet()
92+
protected open fun generateAdditionalTypes(): Set<GraphQLType> {
93+
val currentlyProcessedTypes = this.additionalTypes.toSet()
94+
this.additionalTypes.clear()
95+
val graphqlTypes = currentlyProcessedTypes.map { generateGraphQLType(this, it) }.toSet()
96+
97+
return if (this.additionalTypes.isNotEmpty()) {
98+
graphqlTypes.plus(generateAdditionalTypes())
99+
} else {
100+
graphqlTypes
101+
}
102+
}
91103
}

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/SchemaGeneratorTest.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import com.expediagroup.graphql.SchemaGeneratorConfig
2020
import com.expediagroup.graphql.extensions.deepName
2121
import org.junit.jupiter.api.Test
2222
import kotlin.reflect.KClass
23-
import kotlin.reflect.KType
24-
import kotlin.reflect.full.createType
2523
import kotlin.test.assertEquals
2624

2725
class SchemaGeneratorTest {
@@ -45,9 +43,9 @@ class SchemaGeneratorTest {
4543
fun generateAdditionalTypes() {
4644
val config = SchemaGeneratorConfig(listOf("com.expediagroup.graphql.generator"))
4745
val generator = CustomSchemaGenerator(config)
48-
val types = setOf(SomeObjectWithAnnotaiton::class.createType())
46+
generator.addTypes(MyCustomAnnotation::class)
4947

50-
val result = generator.generateCustomAdditionalTypes(types)
48+
val result = generator.generateCustomAdditionalTypes()
5149

5250
assertEquals(1, result.size)
5351
assertEquals("SomeObjectWithAnnotaiton!", result.first().deepName)
@@ -56,7 +54,7 @@ class SchemaGeneratorTest {
5654
class CustomSchemaGenerator(config: SchemaGeneratorConfig) : SchemaGenerator(config) {
5755
internal fun addTypes(annotation: KClass<*>) = addAdditionalTypesWithAnnotation(annotation)
5856

59-
internal fun generateCustomAdditionalTypes(types: Set<KType>) = generateAdditionalTypes(types)
57+
internal fun generateCustomAdditionalTypes() = generateAdditionalTypes()
6058
}
6159

6260
annotation class MyCustomAnnotation
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2020 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.test.integration
18+
19+
import com.expediagroup.graphql.TopLevelObject
20+
import com.expediagroup.graphql.testSchemaConfig
21+
import com.expediagroup.graphql.toSchema
22+
import org.junit.jupiter.api.Test
23+
import kotlin.test.assertNotNull
24+
25+
class ConcurrentAdditionalTypesTest {
26+
27+
@Test
28+
fun `verify a concurrent exception is not thrown if there are additionalTypes added when generating the additionalTypes`() {
29+
val queries = listOf(TopLevelObject(SimpleQuery()))
30+
val schema = toSchema(testSchemaConfig, queries)
31+
assertNotNull(schema)
32+
assertNotNull(schema.getType("InterfaceOne"))
33+
assertNotNull(schema.getType("InterfaceTwo"))
34+
assertNotNull(schema.getType("ClassOneA"))
35+
assertNotNull(schema.getType("ClassOneB"))
36+
assertNotNull(schema.getType("ClassTwoA"))
37+
assertNotNull(schema.getType("ClassTwoB"))
38+
}
39+
40+
class SimpleQuery {
41+
fun getClassOne(): InterfaceOne = ClassOneB("foo")
42+
}
43+
44+
interface InterfaceOne {
45+
val value: String
46+
}
47+
48+
interface InterfaceTwo {
49+
val value: String
50+
}
51+
52+
class ClassOneA(
53+
override val value: String,
54+
val interfaceField: InterfaceTwo
55+
) : InterfaceOne
56+
57+
class ClassOneB(override val value: String) : InterfaceOne
58+
59+
class ClassTwoA(override val value: String) : InterfaceTwo
60+
61+
class ClassTwoB(override val value: String) : InterfaceTwo
62+
}

0 commit comments

Comments
 (0)