Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include interface fields when annotated with @Name #2238

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
<maven.compiler.release>17</maven.compiler.release>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<version.impsort.plugin>1.8.0</version.impsort.plugin>
<version.kotlin.metadata.jvm>2.1.0</version.kotlin.metadata.jvm>
<version.impsort.plugin>1.12.0</version.impsort.plugin>
<version.kotlin.compiler>2.1.0</version.kotlin.compiler>

</properties>
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 {
}
}
Loading