Skip to content

Commit 834d7c3

Browse files
authored
Merge pull request #894 from Netflix/fix/interface-gen-setters
Fix regression bug in interface setter generation
2 parents dddd6a8 + 8a0c3e2 commit 834d7c3

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/InterfaceGenerator.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class InterfaceGenerator(
7373
val mergedFieldDefinitions = definition.fieldDefinitions + extensions.flatMap { it.fieldDefinitions }
7474

7575
mergedFieldDefinitions.filterSkipped().forEach {
76-
// Generate getters/setters for fields that are not interfaces, and only getters for fields that are interfaces.
76+
// Generate getters/setters for fields that are not interfaces, and only getters for fields that are interfaces
77+
// unless generateInterfaceMethodsForInterfaceFields && generateInterfaceSetters.
7778
// Skip generating interface methods with list types where the inner type is an interface as Java does not
7879
// support overriding them with more specific types (i.e. List<Dog> does not override List<Pet>).
7980
//
@@ -164,9 +165,7 @@ class InterfaceGenerator(
164165
}
165166
javaType.addMethod(getterBuilder.build())
166167

167-
// Only generate setters for non-interface-typed fields unless generateInterfaceMethodsForInterfaceFields is true
168-
if (config.generateInterfaceMethodsForInterfaceFields ||
169-
(config.generateInterfaceSetters && !isFieldAnInterface(fieldDefinition))
168+
if (config.generateInterfaceSetters && (config.generateInterfaceMethodsForInterfaceFields || !isFieldAnInterface(fieldDefinition))
170169
) {
171170
val setterBuilder =
172171
MethodSpec

graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/CodeGenTest.kt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3810,6 +3810,99 @@ class CodeGenTest {
38103810
assertCompilesJava(dataTypes + interfaces)
38113811
}
38123812

3813+
@Test
3814+
fun generateInterfaceMethodsForInterfaceFieldsAndNoInterfaceSetters() {
3815+
val schema =
3816+
"""
3817+
interface Person {
3818+
name: String
3819+
age: Int
3820+
parents: [Person]!
3821+
friend: Person
3822+
}
3823+
3824+
interface Employee implements Person {
3825+
name: String
3826+
age: Int
3827+
parents: [Person]!
3828+
friend: Person
3829+
}
3830+
3831+
type Manager implements Employee {
3832+
name: String
3833+
age: Int
3834+
parents: [Person]!
3835+
friend: Person
3836+
}
3837+
""".trimIndent()
3838+
3839+
val (dataTypes, interfaces) =
3840+
CodeGen(
3841+
CodeGenConfig(
3842+
schemas = setOf(schema),
3843+
packageName = BASE_PACKAGE_NAME,
3844+
generateInterfaceMethodsForInterfaceFields = true,
3845+
generateInterfaceSetters = false,
3846+
),
3847+
).generate()
3848+
3849+
assertThat(interfaces).hasSize(2)
3850+
3851+
val person = interfaces.find { it.typeSpec.name == "Person" }!!
3852+
assertThat(person.toString()).isEqualTo(
3853+
"""
3854+
|package com.netflix.graphql.dgs.codegen.tests.generated.types;
3855+
|
3856+
|import java.lang.Integer;
3857+
|import java.lang.String;
3858+
|import java.util.List;
3859+
|
3860+
|public interface Person {
3861+
| String getName();
3862+
|
3863+
| Integer getAge();
3864+
|
3865+
| List<Person> getParents();
3866+
|
3867+
| Person getFriend();
3868+
|}
3869+
|
3870+
""".trimMargin(),
3871+
)
3872+
3873+
val employee = interfaces.find { it.typeSpec.name == "Employee" }!!
3874+
assertThat(employee.toString()).isEqualTo(
3875+
"""
3876+
|package com.netflix.graphql.dgs.codegen.tests.generated.types;
3877+
|
3878+
|import com.fasterxml.jackson.annotation.JsonSubTypes;
3879+
|import com.fasterxml.jackson.annotation.JsonTypeInfo;
3880+
|import java.lang.Integer;
3881+
|import java.lang.String;
3882+
|import java.util.List;
3883+
|
3884+
|@JsonTypeInfo(
3885+
| use = JsonTypeInfo.Id.NAME,
3886+
| include = JsonTypeInfo.As.PROPERTY,
3887+
| property = "__typename"
3888+
|)
3889+
|@JsonSubTypes(@JsonSubTypes.Type(value = Manager.class, name = "Manager"))
3890+
|public interface Employee extends Person {
3891+
| String getName();
3892+
|
3893+
| Integer getAge();
3894+
|
3895+
| List<Person> getParents();
3896+
|
3897+
| Person getFriend();
3898+
|}
3899+
|
3900+
""".trimMargin(),
3901+
)
3902+
3903+
assertCompilesJava(dataTypes + interfaces)
3904+
}
3905+
38133906
@Test
38143907
fun generateObjectTypeInterfaceWithInterfaceInheritance() {
38153908
val schema =

0 commit comments

Comments
 (0)