Skip to content

Commit

Permalink
Added a fix which enables having Generics in response type (fields), …
Browse files Browse the repository at this point in the history
…added test
  • Loading branch information
mskacelik authored and jmartisk committed Oct 9, 2023
1 parent 406d55c commit 6f3e56d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ private List<MethodInfo> getAllMethodsIncludingFromSuperClasses(ClassInfo classI
IndexView index = ScanningContext.getIndex();
List<MethodInfo> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MethodInfo> classMethods = c.methods();
List<MethodInfo> classMethods = filterOutBridgeMethod(c.methods());
allMethods.addAll(classMethods);
allMethods.addAll(getAllInterfaceMethods(c, classMethods
.stream()
Expand Down Expand Up @@ -97,8 +97,9 @@ private List<MethodInfo> getAllInterfaceMethods(ClassInfo classInfo, Set<String>
.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()))
Expand All @@ -112,6 +113,10 @@ private boolean isNotGenericType(MethodInfo method) {
type.kind() != org.jboss.jandex.Type.Kind.PARAMETERIZED_TYPE);
}

private List<MethodInfo> filterOutBridgeMethod(List<MethodInfo> methods) {
return methods.stream().filter(methodInfo -> !methodInfo.isSynthetic()).collect(Collectors.toList());
}

@Override
public String getDirectiveLocation() {
return "OBJECT";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,30 @@ public void testGenericSchemaBuilding() {
assertNotNull(schema);
Set<Operation> queries = schema.getQueries();
Set<Operation> mutations = schema.getMutations();
Map<String, Type> 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()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.smallrye.graphql.index.generic;

public class Greet implements ResponseAttribute<String> {
String value;

public Greet(String name) {
this.value = value;
}

@Override
public String getValue() {
return "hello".concat(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.smallrye.graphql.index.generic;

public interface ResponseAttribute<T> {
public T getValue();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 6f3e56d

Please sign in to comment.