From 25156b74e82d23aa70304a58a9fb44630df22dbe Mon Sep 17 00:00:00 2001 From: Marek Skacelik Date: Thu, 5 Oct 2023 13:24:19 +0200 Subject: [PATCH] Added a fix which enables having Generics in response type (fields), added test --- .../graphql/schema/SchemaBuilder.java | 3 +-- .../schema/creator/type/TypeCreator.java | 11 ++++++++--- .../graphql/index/SchemaBuilderTest.java | 19 ++++++++++++++++--- .../smallrye/graphql/index/generic/Greet.java | 14 ++++++++++++++ .../graphql/index/generic/HeroResource.java | 9 +++++++++ .../index/generic/ResponseAttribute.java | 5 +++++ .../index/generic/ResponseComposite.java | 17 +++++++++++++++++ 7 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/Greet.java create mode 100644 common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/ResponseAttribute.java create mode 100644 common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/ResponseComposite.java diff --git a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/SchemaBuilder.java b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/SchemaBuilder.java index 7bea27847..0fe4b3ac2 100644 --- a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/SchemaBuilder.java +++ b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/SchemaBuilder.java @@ -157,8 +157,7 @@ private List getAllMethodsIncludingFromSuperClasses(ClassInfo classI IndexView index = ScanningContext.getIndex(); List methods = new ArrayList<>(); while (current != null) { - methods.addAll( - current.methods().stream().filter(methodInfo -> !methodInfo.isSynthetic()).collect(Collectors.toList())); + current.methods().stream().filter(methodInfo -> !methodInfo.isSynthetic()).forEach(methods::add); DotName superName = classInfo.superName(); if (superName != null) { current = index.getClassByName(current.superName()); diff --git a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/type/TypeCreator.java b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/type/TypeCreator.java index b9de3fee2..46f36b86f 100644 --- a/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/type/TypeCreator.java +++ b/common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/type/TypeCreator.java @@ -51,7 +51,7 @@ protected void addFields(Type type, ClassInfo classInfo, Reference reference) { // Find all methods and properties up the tree for (ClassInfo c = classInfo; c != null; c = ScanningContext.getIndex().getClassByName(c.superName())) { if (InterfaceCreator.canAddInterfaceIntoScheme(c.toString())) { // Not java objects - List classMethods = c.methods(); + List classMethods = filterOutBridgeMethod(c.methods()); allMethods.addAll(classMethods); allMethods.addAll(getAllInterfaceMethods(c, classMethods .stream() @@ -97,8 +97,9 @@ private List getAllInterfaceMethods(ClassInfo classInfo, Set .map(ScanningContext.getIndex()::getClassByName) .filter(Objects::nonNull) .flatMap(parentInterfaceInfo -> Stream.concat( - parentInterfaceInfo - .methods() + filterOutBridgeMethod( + parentInterfaceInfo + .methods()) .stream() .filter(method -> isNotGenericType(method) && methodMemory.add(method.toString())), getAllInterfaceMethods(parentInterfaceInfo, methodMemory).stream())) @@ -112,6 +113,10 @@ private boolean isNotGenericType(MethodInfo method) { type.kind() != org.jboss.jandex.Type.Kind.PARAMETERIZED_TYPE); } + private List filterOutBridgeMethod(List methods) { + return methods.stream().filter(methodInfo -> !methodInfo.isSynthetic()).collect(Collectors.toList()); + } + @Override public String getDirectiveLocation() { return "OBJECT"; diff --git a/common/schema-builder/src/test/java/io/smallrye/graphql/index/SchemaBuilderTest.java b/common/schema-builder/src/test/java/io/smallrye/graphql/index/SchemaBuilderTest.java index 0552ca470..deeed879a 100644 --- a/common/schema-builder/src/test/java/io/smallrye/graphql/index/SchemaBuilderTest.java +++ b/common/schema-builder/src/test/java/io/smallrye/graphql/index/SchemaBuilderTest.java @@ -197,17 +197,30 @@ public void testGenericSchemaBuilding() { assertNotNull(schema); Set queries = schema.getQueries(); Set mutations = schema.getMutations(); + Map outputTypes = schema.getTypes(); - assertEquals(queries.size(), 1); + assertEquals(queries.size(), 2); assertEquals(mutations.size(), 4); - Operation query = queries.stream() + Operation firstQuery = queries.stream() .filter(q -> q.getName().equals("heroes")) .findFirst() .orElseThrow(AssertionError::new); // return type - assertEquals(query.getReference().getName(), "Hero"); + assertEquals(firstQuery.getReference().getName(), "Hero"); + + Operation secondQuery = queries.stream() + .filter(q -> q.getName().equals("sayHello")).findFirst().orElseThrow(AssertionError::new); + assertEquals(secondQuery.getReference().getName(), "ResponseComposite"); + assertEquals(secondQuery.getDescription(), "Say hello"); + + Type responseCompositeType = outputTypes.get("ResponseComposite"); + assertNotNull(responseCompositeType); + + Type greetingType = outputTypes.get("Greet"); + assertNotNull(greetingType); + // ------------------------------------------------------------------ // MUTATIONS Operation firstMutation = mutations.stream() diff --git a/common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/Greet.java b/common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/Greet.java new file mode 100644 index 000000000..e36b05990 --- /dev/null +++ b/common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/Greet.java @@ -0,0 +1,14 @@ +package io.smallrye.graphql.index.generic; + +public class Greet implements ResponseAttribute { + String value; + + public Greet(String name) { + this.value = value; + } + + @Override + public String getValue() { + return "hello".concat(value); + } +} diff --git a/common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/HeroResource.java b/common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/HeroResource.java index 6c0f27669..9981c1c7e 100644 --- a/common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/HeroResource.java +++ b/common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/HeroResource.java @@ -2,6 +2,7 @@ import java.util.List; +import org.eclipse.microprofile.graphql.Description; import org.eclipse.microprofile.graphql.GraphQLApi; import org.eclipse.microprofile.graphql.Mutation; import org.eclipse.microprofile.graphql.Name; @@ -39,4 +40,12 @@ public Hero update(@Name("hero") Hero character) { public Hero doSomething() { return null; } + + @Query + @Description("Say hello") + public ResponseComposite sayHello(String name) { + ResponseComposite response = new ResponseComposite(new Greet(name)); + return null; + } + } diff --git a/common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/ResponseAttribute.java b/common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/ResponseAttribute.java new file mode 100644 index 000000000..fa755da02 --- /dev/null +++ b/common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/ResponseAttribute.java @@ -0,0 +1,5 @@ +package io.smallrye.graphql.index.generic; + +public interface ResponseAttribute { + public T getValue(); +} diff --git a/common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/ResponseComposite.java b/common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/ResponseComposite.java new file mode 100644 index 000000000..e7d08a7ec --- /dev/null +++ b/common/schema-builder/src/test/java/io/smallrye/graphql/index/generic/ResponseComposite.java @@ -0,0 +1,17 @@ +package io.smallrye.graphql.index.generic; + +public class ResponseComposite { + Greet greet; + + public ResponseComposite(Greet greet) { + this.greet = greet; + } + + public Greet getGreet() { + return this.greet; + } + + public void setGreet(Greet greet) { + this.greet = greet; + } +}