Skip to content

Commit

Permalink
Fix generation of Interface when return type of the method has a list…
Browse files Browse the repository at this point in the history
… of Unions #1018 (#1030)
  • Loading branch information
kobylynskyi authored Feb 20, 2023
1 parent ec6d30d commit 76e0471
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public NamedDefinition getLanguageType(MappingContext mappingContext, String gra
serializeUsingObjectMapper = true;
}

return new NamedDefinition(langTypeName, graphQLType, mappingContext.getInterfacesName().contains(graphQLType),
return new NamedDefinition(langTypeName, graphQLType, isInterfaceOrUnion(mappingContext, graphQLType),
mandatory, primitiveCanBeUsed, serializeUsingObjectMapper);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ public NamedDefinition getLanguageType(MappingContext mappingContext, Type<?> gr
} else if (graphqlType instanceof ListType) {
NamedDefinition mappedCollectionType = getLanguageType(mappingContext, ((ListType) graphqlType).getType(),
name, parentTypeName, false, true);
if (mappedCollectionType.isInterface() && mappingContext.getInterfacesName().contains(parentTypeName)) {
if (mappedCollectionType.isInterfaceOrUnion() &&
isInterfaceOrUnion(mappingContext, parentTypeName)) {
mappedCollectionType.setJavaName(
wrapSuperTypeIntoList(mappingContext, mappedCollectionType.getJavaName(), mandatory));
} else {
Expand Down Expand Up @@ -223,7 +224,7 @@ public NamedDefinition getLanguageType(MappingContext mappingContext, String gra
serializeFieldsUsingObjectMapper.contains(graphQLType) ||
serializeFieldsUsingObjectMapper.contains(parentTypeName + "." + name);

return new NamedDefinition(langTypeName, graphQLType, mappingContext.getInterfacesName().contains(graphQLType),
return new NamedDefinition(langTypeName, graphQLType, isInterfaceOrUnion(mappingContext, graphQLType),
mandatory, primitiveCanBeUsed, serializeUsingObjectMapper);
}

Expand All @@ -247,4 +248,9 @@ public String getResponseReturnType(MappingContext mappingContext,
return computedTypeName;
}

protected boolean isInterfaceOrUnion(MappingContext mappingContext, String graphQLType) {
return mappingContext.getInterfacesName().contains(graphQLType) ||
mappingContext.getUnionsNames().contains(graphQLType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class MappingContext implements GraphQLCodegenConfiguration {
private final ExtendedDocument document;
private final Set<String> typesUnionsInterfacesNames;
private final Set<String> interfacesName;
private final Set<String> unionsName;
private final Set<String> operationsName;
private final Map<String, Set<String>> interfaceChildren;
private final GeneratedInformation generatedInformation;
Expand All @@ -47,6 +48,7 @@ private MappingContext(File outputDirectory,
this.document = document;
this.typesUnionsInterfacesNames = document.getTypesUnionsInterfacesNames();
this.interfacesName = document.getInterfacesNames();
this.unionsName = document.getUnionsNames();
this.interfaceChildren = document.getInterfaceChildren();
this.generatedInformation = generatedInformation;
this.operationsName = document.getOperationsNames();
Expand Down Expand Up @@ -346,6 +348,10 @@ public Set<String> getInterfacesName() {
return interfacesName;
}

public Set<String> getUnionsNames() {
return unionsName;
}

public Set<String> getOperationsName() {
return operationsName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ public class NamedDefinition {

private String javaName;
private String graphqlTypeName;
private boolean isInterface;
private boolean isInterfaceOrUnion;
private boolean mandatory;
private boolean primitiveCanBeUsed;
private boolean serializeUsingObjectMapper;

public NamedDefinition(String javaName, String graphqlTypeName,
boolean isInterface, boolean mandatory,
boolean isInterfaceOrUnion, boolean mandatory,
boolean primitiveCanBeUsed, boolean serializeUsingObjectMapper) {
this.javaName = javaName;
this.graphqlTypeName = graphqlTypeName;
this.isInterface = isInterface;
this.isInterfaceOrUnion = isInterfaceOrUnion;
this.mandatory = mandatory;
this.primitiveCanBeUsed = primitiveCanBeUsed;
this.serializeUsingObjectMapper = serializeUsingObjectMapper;
Expand All @@ -39,12 +39,12 @@ public void setGraphqlTypeName(String graphqlTypeName) {
this.graphqlTypeName = graphqlTypeName;
}

public boolean isInterface() {
return isInterface;
public boolean isInterfaceOrUnion() {
return isInterfaceOrUnion;
}

public void setInterface(boolean anInterface) {
isInterface = anInterface;
public void setInterfaceOrUnion(boolean interfaceOrUnion) {
isInterfaceOrUnion = interfaceOrUnion;
}

public boolean isMandatory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public Set<String> getInterfacesNames() {
.collect(Collectors.toSet());
}

public Set<String> getUnionsNames() {
return unionDefinitions.stream()
.map(ExtendedDefinition::getName)
.collect(Collectors.toSet());
}

public Set<String> getOperationsNames() {
return operationDefinitions.stream()
.map(ExtendedObjectTypeDefinition::getFieldDefinitions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ void init() {
mappingConfig.setGenerateJacksonTypeIdResolver(true);
}

private List<File> generate(String s) throws IOException {
return new JavaGraphQLCodegen(singletonList(s), outputBuildDir, mappingConfig,
TestUtils.getStaticGeneratedInfo()).generate();
}

@AfterEach
void cleanup() {
Utils.deleteDir(outputBuildDir);
Expand All @@ -48,8 +43,8 @@ void generate_CheckFiles_with_model_package() throws Exception {
File outputJavaClassesDir = new File("build/generated/com/kobylynskyi/graphql/unionresolver");
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
List<String> generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList());
List<String> expectedClasses = Arrays.asList("GraphqlJacksonTypeIdResolver.java", "UnionMemberA.java",
"UnionMemberB.java", "UnionToResolve.java");
List<String> expectedClasses = Arrays.asList("GraphqlJacksonTypeIdResolver.java", "ResultObject.java",
"UnionMemberA.java", "UnionMemberB.java", "UnionToResolve.java");
assertEquals(expectedClasses, generatedFileNames);

for (File file : files) {
Expand All @@ -70,8 +65,8 @@ void generate_CheckFiles_without_model_package_and_with_prefix_and_suffix() thro
File outputJavaClassesDir = new File("build/generated");
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
List<String> generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList());
List<String> expectedClasses = Arrays.asList("GraphqlJacksonTypeIdResolver.java", "MyUnionMemberASuffix.java",
"MyUnionMemberBSuffix.java", "MyUnionToResolveSuffix.java");
List<String> expectedClasses = Arrays.asList("GraphqlJacksonTypeIdResolver.java", "MyResultObjectSuffix.java",
"MyUnionMemberASuffix.java", "MyUnionMemberBSuffix.java", "MyUnionToResolveSuffix.java");
assertEquals(expectedClasses, generatedFileNames);

for (File file : files) {
Expand All @@ -82,4 +77,10 @@ void generate_CheckFiles_without_model_package_and_with_prefix_and_suffix() thro
file);
}
}

private void generate(String path) throws IOException {
new JavaGraphQLCodegen(singletonList(path), outputBuildDir, mappingConfig,
TestUtils.getStaticGeneratedInfo()).generate();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ void init() {
mappingConfig.setGeneratedLanguage(GeneratedLanguage.KOTLIN);
}

private List<File> generate(String s) throws IOException {
return new KotlinGraphQLCodegen(singletonList(s), outputBuildDir, mappingConfig,
TestUtils.getStaticGeneratedInfo()).generate();
}

@AfterEach
void cleanup() {
Utils.deleteDir(outputBuildDir);
Expand All @@ -50,8 +45,8 @@ void generate_CheckFiles_with_model_package() throws Exception {
File outputJavaClassesDir = new File("build/generated/com/kobylynskyi/graphql/unionresolver");
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
List<String> generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList());
List<String> expectedClasses = Arrays.asList("GraphqlJacksonTypeIdResolver.kt", "UnionMemberA.kt",
"UnionMemberB.kt", "UnionToResolve.kt");
List<String> expectedClasses = Arrays.asList("GraphqlJacksonTypeIdResolver.kt", "ResultObject.kt",
"UnionMemberA.kt", "UnionMemberB.kt", "UnionToResolve.kt");
assertEquals(expectedClasses, generatedFileNames);

for (File file : files) {
Expand All @@ -72,8 +67,8 @@ void generate_CheckFiles_without_model_package_and_with_prefix_and_suffix() thro
File outputJavaClassesDir = new File("build/generated");
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
List<String> generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList());
List<String> expectedClasses = Arrays.asList("GraphqlJacksonTypeIdResolver.kt", "MyUnionMemberASuffix.kt",
"MyUnionMemberBSuffix.kt", "MyUnionToResolveSuffix.kt");
List<String> expectedClasses = Arrays.asList("GraphqlJacksonTypeIdResolver.kt", "MyResultObjectSuffix.kt",
"MyUnionMemberASuffix.kt", "MyUnionMemberBSuffix.kt", "MyUnionToResolveSuffix.kt");
assertEquals(expectedClasses, generatedFileNames);

for (File file : files) {
Expand All @@ -85,4 +80,10 @@ void generate_CheckFiles_without_model_package_and_with_prefix_and_suffix() thro
file);
}
}

private void generate(String path) throws IOException {
new KotlinGraphQLCodegen(singletonList(path), outputBuildDir, mappingConfig,
TestUtils.getStaticGeneratedInfo()).generate();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ void init() {
mappingConfig.setGeneratedLanguage(GeneratedLanguage.SCALA);
}

private List<File> generate(String s) throws IOException {
return new ScalaGraphQLCodegen(singletonList(s), outputBuildDir, mappingConfig,
TestUtils.getStaticGeneratedInfo()).generate();
}

@AfterEach
void cleanup() {
Utils.deleteDir(outputBuildDir);
Expand All @@ -50,8 +45,8 @@ void generate_CheckFiles_with_model_package() throws Exception {
File outputJavaClassesDir = new File("build/generated/com/kobylynskyi/graphql/unionresolver");
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
List<String> generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList());
List<String> expectedClasses = Arrays.asList("GraphqlJacksonTypeIdResolver.scala", "UnionMemberA.scala",
"UnionMemberB.scala", "UnionToResolve.scala");
List<String> expectedClasses = Arrays.asList("GraphqlJacksonTypeIdResolver.scala", "ResultObject.scala",
"UnionMemberA.scala", "UnionMemberB.scala", "UnionToResolve.scala");
assertEquals(expectedClasses, generatedFileNames);

for (File file : files) {
Expand All @@ -72,8 +67,8 @@ void generate_CheckFiles_without_model_package_and_with_prefix_and_suffix() thro
File outputJavaClassesDir = new File("build/generated");
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
List<String> generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList());
List<String> expectedClasses = Arrays.asList("GraphqlJacksonTypeIdResolver.scala", "MyUnionMemberASuffix.scala",
"MyUnionMemberBSuffix.scala", "MyUnionToResolveSuffix.scala");
List<String> expectedClasses = Arrays.asList("GraphqlJacksonTypeIdResolver.scala", "MyResultObjectSuffix.scala",
"MyUnionMemberASuffix.scala", "MyUnionMemberBSuffix.scala", "MyUnionToResolveSuffix.scala");
assertEquals(expectedClasses, generatedFileNames);

for (File file : files) {
Expand All @@ -85,4 +80,10 @@ void generate_CheckFiles_without_model_package_and_with_prefix_and_suffix() thro
file);
}
}

private void generate(String path) throws IOException {
new ScalaGraphQLCodegen(singletonList(path), outputBuildDir, mappingConfig,
TestUtils.getStaticGeneratedInfo()).generate();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.kobylynskyi.graphql.unionresolver;


@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface ResultObject {

java.util.List<? extends UnionToResolve> getList();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface MyResultObjectSuffix {

java.util.List<? extends MyUnionToResolveSuffix> getList();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.kobylynskyi.graphql.unionresolver


@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface ResultObject {

val list: List<out UnionToResolve>?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface MyResultObjectSuffix {

val list: List<out MyUnionToResolveSuffix>?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.kobylynskyi.graphql.unionresolver


@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait ResultObject {

val list: scala.Seq[_ <: UnionToResolve]

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait MyResultObjectSuffix {

val list: scala.Seq[_ <: MyUnionToResolveSuffix]

}
4 changes: 4 additions & 0 deletions src/test/resources/schemas/union-resolver.graphqls
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
interface ResultObject {
list: [UnionToResolve!]
}

type UnionMemberA {
someField: Int
}
Expand Down

0 comments on commit 76e0471

Please sign in to comment.