Skip to content

Commit

Permalink
fix: Record implementing interface field
Browse files Browse the repository at this point in the history
  • Loading branch information
mskacelik committed Dec 11, 2024
1 parent 7b32640 commit 433c776
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.smallrye.graphql.schema.creator.type;

import static io.smallrye.graphql.schema.Annotations.NAME;
import static io.smallrye.graphql.schema.Annotations.getAnnotationsForMethod;

import java.util.List;

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

// Add all fields from interface itself
for (MethodInfo methodInfo : classInfo.methods()) {
if (MethodHelper.isPropertyMethod(Direction.OUT, methodInfo)) {
if (MethodHelper.isPropertyMethod(Direction.OUT, methodInfo)
|| getAnnotationsForMethod(methodInfo).containsKeyAndValidValue(NAME)) {
fieldCreator.createFieldForInterface(methodInfo, reference)
.ifPresent(interfaceType::addField);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.smallrye.graphql.tests.records;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URL;
import java.util.List;

import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Name;
import org.eclipse.microprofile.graphql.Query;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import io.smallrye.graphql.tests.GraphQLAssured;

@RunWith(Arquillian.class)
public class RecordImplementingInterfaceTest {
@Deployment
public static WebArchive deployment() {
return ShrinkWrap.create(WebArchive.class, "default.war")
.addClasses(CustomerProductResource.class, InternetLine.class, MobileLine.class, CustomerProduct.class);
}

@ArquillianResource
URL testingURL;

@Test
public void testRecordImplementingInterface() {
GraphQLAssured graphQLAssured = new GraphQLAssured(testingURL);
final String query = """
query {
products {
name
}
}
""";
final String expected = """
{
"data": {
"products": [
{
"name": "Fiber 100"
},
{
"name": "Mobile 1"
},
{
"name": "Fiber 200"
},
{
"name": "Mobile 2"
}
]
}
}
""";
assertThat(graphQLAssured.post(query)).isEqualToIgnoringWhitespace(expected);

}

@GraphQLApi
public static class CustomerProductResource {

@Query
public List<CustomerProduct> getProducts() {
return List.of(
new InternetLine("Fiber 100", 100),
new MobileLine("Mobile 1", "123456789"),
new InternetLine("Fiber 200", 200),
new MobileLine("Mobile 2", "987654321"));
}
}

public sealed interface CustomerProduct permits InternetLine, MobileLine {
@Name("name") // @Name("") is also valid, since it automatically uses the field name
String name();
}

public record InternetLine(String name, int speed) implements CustomerProduct {
}

public record MobileLine(String name, String phoneNumber) implements CustomerProduct {
}
}

0 comments on commit 433c776

Please sign in to comment.