Skip to content

Commit 1d23dbf

Browse files
mskacelikjmartisk
authored andcommitted
fix: Record implementing interface field
1 parent 1144d82 commit 1d23dbf

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

common/schema-builder/src/main/java/io/smallrye/graphql/schema/creator/type/InterfaceCreator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.smallrye.graphql.schema.creator.type;
22

3+
import static io.smallrye.graphql.schema.Annotations.NAME;
4+
import static io.smallrye.graphql.schema.Annotations.getAnnotationsForMethod;
5+
36
import java.util.List;
47

58
import org.jboss.jandex.ClassInfo;
@@ -38,7 +41,8 @@ protected void addFields(Type interfaceType, ClassInfo classInfo, Reference refe
3841

3942
// Add all fields from interface itself
4043
for (MethodInfo methodInfo : classInfo.methods()) {
41-
if (MethodHelper.isPropertyMethod(Direction.OUT, methodInfo)) {
44+
if (MethodHelper.isPropertyMethod(Direction.OUT, methodInfo)
45+
|| getAnnotationsForMethod(methodInfo).containsKeyAndValidValue(NAME)) {
4246
fieldCreator.createFieldForInterface(methodInfo, reference)
4347
.ifPresent(interfaceType::addField);
4448
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package io.smallrye.graphql.tests.records;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.net.URL;
6+
import java.util.List;
7+
8+
import org.eclipse.microprofile.graphql.GraphQLApi;
9+
import org.eclipse.microprofile.graphql.Name;
10+
import org.eclipse.microprofile.graphql.Query;
11+
import org.jboss.arquillian.container.test.api.Deployment;
12+
import org.jboss.arquillian.junit.Arquillian;
13+
import org.jboss.arquillian.test.api.ArquillianResource;
14+
import org.jboss.shrinkwrap.api.ShrinkWrap;
15+
import org.jboss.shrinkwrap.api.spec.WebArchive;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
19+
import io.smallrye.graphql.tests.GraphQLAssured;
20+
21+
@RunWith(Arquillian.class)
22+
public class RecordImplementingInterfaceTest {
23+
@Deployment
24+
public static WebArchive deployment() {
25+
return ShrinkWrap.create(WebArchive.class, "default.war")
26+
.addClasses(CustomerProductResource.class, InternetLine.class, MobileLine.class, CustomerProduct.class);
27+
}
28+
29+
@ArquillianResource
30+
URL testingURL;
31+
32+
@Test
33+
public void testRecordImplementingInterface() {
34+
GraphQLAssured graphQLAssured = new GraphQLAssured(testingURL);
35+
final String query = """
36+
query {
37+
products {
38+
name
39+
}
40+
}
41+
""";
42+
final String expected = """
43+
{
44+
"data": {
45+
"products": [
46+
{
47+
"name": "Fiber 100"
48+
},
49+
{
50+
"name": "Mobile 1"
51+
},
52+
{
53+
"name": "Fiber 200"
54+
},
55+
{
56+
"name": "Mobile 2"
57+
}
58+
]
59+
}
60+
}
61+
""";
62+
assertThat(graphQLAssured.post(query)).isEqualToIgnoringWhitespace(expected);
63+
64+
}
65+
66+
@GraphQLApi
67+
public static class CustomerProductResource {
68+
69+
@Query
70+
public List<CustomerProduct> getProducts() {
71+
return List.of(
72+
new InternetLine("Fiber 100", 100),
73+
new MobileLine("Mobile 1", "123456789"),
74+
new InternetLine("Fiber 200", 200),
75+
new MobileLine("Mobile 2", "987654321"));
76+
}
77+
}
78+
79+
public sealed interface CustomerProduct permits InternetLine, MobileLine {
80+
@Name("name") // @Name("") is also valid, since it automatically uses the field name
81+
String name();
82+
}
83+
84+
public record InternetLine(String name, int speed) implements CustomerProduct {
85+
}
86+
87+
public record MobileLine(String name, String phoneNumber) implements CustomerProduct {
88+
}
89+
}

0 commit comments

Comments
 (0)