-
Notifications
You must be signed in to change notification settings - Fork 116
Generate list-of-interface getters on Java interfaces #953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,7 +94,9 @@ class TypeUtils( | |
| var canUseWildcardType = false | ||
| if (useWildcardType) { | ||
| if (typeName is ClassName) { | ||
| if (document.definitions | ||
| if (isFieldTypeAnInterface(node.type)) { | ||
| canUseWildcardType = true | ||
| } else if (document.definitions | ||
| .filterIsInstance<ObjectTypeDefinition>() | ||
| .any { e -> "I${e.name}" == typeName.simpleName() } || | ||
| ( | ||
|
|
@@ -107,6 +109,8 @@ class TypeUtils( | |
| ) { | ||
| canUseWildcardType = true | ||
| } | ||
| } else if (typeName is ParameterizedTypeName && typeName.rawType().canonicalName() == "java.util.List") { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also use wildcard for This ensure It will also unnecessarily do it for concrete types, which should be harmless. e.g., |
||
| canUseWildcardType = true | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -275,7 +279,7 @@ class TypeUtils( | |
| return NodeTraverser().postOrder(visitor, fieldType) as TypeName | ||
| } | ||
|
|
||
| private fun isFieldTypeAnInterface(fieldDefinitionType: TypeName): Boolean = | ||
| private fun isFieldTypeAnInterface(fieldDefinitionType: Type<*>): Boolean = | ||
| document | ||
| .getDefinitionsOfType(InterfaceTypeDefinition::class.java) | ||
| .any { node -> node.name == findInnerType(fieldDefinitionType).name } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -993,6 +993,12 @@ class CodeGenTest { | |
| | Pet getMother(); | ||
| | | ||
| | Pet getFather(); | ||
| | | ||
| | List<? extends Pet> getParents(); | ||
| | | ||
| | List<? extends List<? extends Pet>> getFriends(); | ||
| | | ||
| | List<? extends Pet> getSiblings(); | ||
| |} | ||
| | | ||
| """.trimMargin(), | ||
|
|
@@ -3393,6 +3399,7 @@ class CodeGenTest { | |
| |import com.netflix.graphql.dgs.codegen.tests.generated.Generated; | ||
| |import java.lang.Integer; | ||
| |import java.lang.String; | ||
| |import java.util.List; | ||
| | | ||
| |@Generated | ||
| |@JsonTypeInfo( | ||
|
|
@@ -3414,6 +3421,8 @@ class CodeGenTest { | |
| | void setAge(Integer age); | ||
| | | ||
| | Employee getBoss(); | ||
| | | ||
| | List<? extends Employee> getTeam(); | ||
| |} | ||
| | | ||
| """.trimMargin(), | ||
|
|
@@ -3480,9 +3489,9 @@ class CodeGenTest { | |
| | | ||
| | void setAge(Integer age); | ||
| | | ||
| | List<Person> getParents(); | ||
| | List<? extends Person> getParents(); | ||
| | | ||
| | void setParents(List<Person> parents); | ||
| | void setParents(List<? extends Person> parents); | ||
| | | ||
| | Person getFriend(); | ||
| | | ||
|
|
@@ -3520,9 +3529,9 @@ class CodeGenTest { | |
| | | ||
| | void setAge(Integer age); | ||
| | | ||
| | List<Person> getParents(); | ||
| | List<? extends Person> getParents(); | ||
| | | ||
| | void setParents(List<Person> parents); | ||
| | void setParents(List<? extends Person> parents); | ||
| | | ||
| | Person getFriend(); | ||
| | | ||
|
|
@@ -3993,14 +4002,27 @@ class CodeGenTest { | |
| | | ||
| | void setName(String name); | ||
| | | ||
| | List<Pet> getFriends(); | ||
| | List<? extends Pet> getFriends(); | ||
| | | ||
| | void setFriends(List<Pet> friends); | ||
| | void setFriends(List<? extends Pet> friends); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This demonstrates how interface-setters also have wildcards. This is more flexible for the Java compiler, but may theoretically lead to runtime errors. e.g., if a concrete type only supported a specific subtype of Note that this is consistent with the existing |
||
| |} | ||
| | | ||
| """.trimMargin(), | ||
| ) | ||
|
|
||
| assertThat(dataTypes[0].toString()).contains( | ||
| """ | ||
| | public List<? extends Pet> getFriends() { | ||
| | return friends; | ||
| | } | ||
| | | ||
| | public void setFriends(List<? extends Pet> friends) { | ||
| | this.friends = friends; | ||
| | } | ||
| | | ||
| """.trimMargin(), | ||
| ) | ||
|
|
||
| assertCompilesJava(codeGenResult) | ||
| } | ||
|
|
||
|
|
@@ -4059,7 +4081,7 @@ class CodeGenTest { | |
| | | ||
| | Integer getAge(); | ||
| | | ||
| | List<Person> getParents(); | ||
| | List<? extends Person> getParents(); | ||
| | | ||
| | Person getFriend(); | ||
| |} | ||
|
|
@@ -4091,7 +4113,7 @@ class CodeGenTest { | |
| | | ||
| | Integer getAge(); | ||
| | | ||
| | List<Person> getParents(); | ||
| | List<? extends Person> getParents(); | ||
| | | ||
| | Person getFriend(); | ||
| |} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use wildcards for
Listof interface.