Skip to content

Commit da9bf61

Browse files
mskacelikjmartisk
authored andcommittedFeb 3, 2025
Kotlin nullability feature for typesafe client (client model)
1 parent 7a7a3ab commit da9bf61

File tree

7 files changed

+93
-7
lines changed

7 files changed

+93
-7
lines changed
 

‎client/model-builder/pom.xml

+41
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
<artifactId>smallrye-graphql-api</artifactId>
5151
<scope>test</scope> <!--subscription annotation-->
5252
</dependency>
53+
<dependency>
54+
<groupId>org.jetbrains.kotlin</groupId>
55+
<artifactId>kotlin-reflect</artifactId>
56+
<version>${version.kotlin}</version>
57+
<scope>test</scope>
58+
</dependency>
5359
</dependencies>
5460

5561
<build>
@@ -61,6 +67,41 @@
6167
<parameters>true</parameters>
6268
</configuration>
6369
</plugin>
70+
<plugin>
71+
<artifactId>kotlin-maven-plugin</artifactId>
72+
<groupId>org.jetbrains.kotlin</groupId>
73+
<version>${version.kotlin.compiler}</version>
74+
<executions>
75+
<execution>
76+
<id>test-compile</id>
77+
<goals>
78+
<goal>test-compile</goal>
79+
</goals>
80+
<configuration>
81+
<sourceDirs>
82+
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
83+
</sourceDirs>
84+
</configuration>
85+
</execution>
86+
</executions>
87+
<configuration>
88+
<compilerPlugins>
89+
<plugin>all-open</plugin>
90+
</compilerPlugins>
91+
<args>
92+
<arg>-java-parameters</arg>
93+
<arg>-Xemit-jvm-type-annotations</arg>
94+
</args>
95+
</configuration>
96+
97+
<dependencies>
98+
<dependency>
99+
<groupId>org.jetbrains.kotlin</groupId>
100+
<artifactId>kotlin-maven-allopen</artifactId>
101+
<version>${version.kotlin.compiler}</version>
102+
</dependency>
103+
</dependencies>
104+
</plugin>
64105
</plugins>
65106
</build>
66107
</project>

‎client/model-builder/src/main/java/io/smallrye/graphql/client/model/helper/ParameterModel.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import org.jboss.jandex.MethodParameterInfo;
1515

16+
import io.smallrye.graphql.client.model.Annotations;
1617
import io.smallrye.graphql.client.model.Scalars;
1718
import io.smallrye.graphql.client.typesafe.api.Header;
1819

@@ -179,11 +180,11 @@ private String graphQlInputTypeName(TypeModel type) {
179180
/**
180181
* Adds an optional exclamation mark to the GraphQL input type name based on nullability.
181182
*
182-
* @param type The {@link TypeModel} representing the type of the parameter.
183183
* @return An optional exclamation mark.
184184
*/
185185
private String optionalExclamationMark(TypeModel type) {
186-
return type.isNonNull() ? "!" : "";
186+
// for some reason KOTLIN_NOT_NULL is not applied on type, but on parameter
187+
return (parameter.hasAnnotation(Annotations.KOTLIN_NOT_NULL) || type.isNonNull()) ? "!" : "";
187188
}
188189

189190
/**

‎client/model-builder/src/main/java/io/smallrye/graphql/client/model/helper/TypeModel.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ public TypeModel getMapValueType() {
141141
public boolean isNonNull() {
142142
return isPrimitive() ||
143143
type.hasAnnotation(Annotations.NON_NULL) ||
144-
type.hasAnnotation(Annotations.JAKARTA_NON_NULL);
144+
type.hasAnnotation(Annotations.JAKARTA_NON_NULL)
145+
|| type.hasAnnotation(Annotations.KOTLIN_NOT_NULL);
145146
}
146147

147148
/**

‎client/model-builder/src/test/java/io/smallrye/graphql/client/model/ClientModelBuilderTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ interface ScalarClientApi {
4040
}
4141

4242
@Test
43-
void sclarClientModelTest() throws IOException {
43+
void scalarClientModelTest() throws IOException {
4444
String configKey = "scalar";
4545
ClientModels clientModels = ClientModelBuilder.build(Index.of(ScalarClientApi.class));
4646
assertNotNull(clientModels.getClientModelByConfigKey(configKey));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.smallrye.graphql.client.model
2+
3+
import io.smallrye.graphql.client.typesafe.api.GraphQLClientApi
4+
import org.eclipse.microprofile.graphql.Query
5+
import org.jboss.jandex.Index
6+
import org.junit.jupiter.api.Assertions.assertEquals
7+
import org.junit.jupiter.api.Assertions.assertNotNull
8+
import org.junit.jupiter.api.Test
9+
10+
class ClientModelBuilderKotlinTest {
11+
@GraphQLClientApi(configKey = "some-client")
12+
interface ClientApi {
13+
@Query
14+
fun returnNonNullFloat(someFloat: Float): Float
15+
16+
@Query
17+
fun returnNullableString(someString: String?): String?
18+
19+
@Query
20+
fun returnList(list: List<String>): String
21+
22+
// fixme: nullability is bit inconsistent from kotlin
23+
24+
}
25+
26+
@Test
27+
fun basicClientModelTest() {
28+
val configKey = "some-client"
29+
val clientModels = ClientModelBuilder.build(Index.of(ClientApi::class.java))
30+
assertNotNull(clientModels)
31+
val clientModel = clientModels.getClientModelByConfigKey(configKey)
32+
assertNotNull(clientModel)
33+
assertEquals(3, clientModel.operationMap.size)
34+
assertOperation(clientModel, MethodKey("returnNonNullFloat", arrayOf(Float::class.java)), "query returnNonNullFloat(\$someFloat: Float!) { returnNonNullFloat(someFloat: \$someFloat) }")
35+
assertOperation(clientModel, MethodKey("returnNullableString", arrayOf(String::class.java)), "query returnNullableString(\$someString: String) { returnNullableString(someString: \$someString) }")
36+
assertOperation(clientModel, MethodKey("returnList", arrayOf(List::class.java)), "query returnList(\$list: [String!]!) { returnList(list: \$list) }")
37+
}
38+
39+
private fun assertOperation(clientModel: ClientModel, methodKey: MethodKey, expectedQuery: String) {
40+
val actualQuery = clientModel.operationMap[methodKey]
41+
assertEquals(expectedQuery, actualQuery)
42+
}
43+
}

‎common/schema-builder/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<artifactId>smallrye-graphql-schema-builder</artifactId>
1212
<name>SmallRye: GraphQL Common :: Schema Builder</name>
1313
<description>Creates the model from a Jandex index</description>
14-
14+
1515
<dependencies>
1616

1717
<!-- The API -->
@@ -40,7 +40,7 @@
4040
<dependency>
4141
<groupId>org.jetbrains.kotlin</groupId>
4242
<artifactId>kotlin-metadata-jvm</artifactId>
43-
<version>${version.kotlin.metadata.jvm}</version>
43+
<version>${version.kotlin}</version>
4444
</dependency>
4545

4646
<!-- Logging -->

‎pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
<maven.compiler.release>17</maven.compiler.release>
6363
<maven.compiler.source>17</maven.compiler.source>
6464
<maven.compiler.target>17</maven.compiler.target>
65-
<version.kotlin.metadata.jvm>2.1.0</version.kotlin.metadata.jvm>
65+
<version.kotlin>2.1.10</version.kotlin>
6666
<version.impsort.plugin>1.12.0</version.impsort.plugin>
6767
<version.kotlin.compiler>2.1.0</version.kotlin.compiler>
6868

0 commit comments

Comments
 (0)
Please sign in to comment.