Skip to content

Commit

Permalink
Kotlin nullability feature for typesafe client (client model)
Browse files Browse the repository at this point in the history
  • Loading branch information
mskacelik committed Jan 30, 2025
1 parent 7a7a3ab commit c899e34
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 7 deletions.
40 changes: 40 additions & 0 deletions client/model-builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
<artifactId>smallrye-graphql-api</artifactId>
<scope>test</scope> <!--subscription annotation-->
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${version.kotlin}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -61,6 +67,40 @@
<parameters>true</parameters>
</configuration>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${version.kotlin.compiler}</version>
<executions>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
<configuration>
<compilerPlugins>
<plugin>all-open</plugin>
</compilerPlugins>
<args>
<arg>-java-parameters</arg>
</args>
</configuration>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${version.kotlin.compiler}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.jboss.jandex.MethodParameterInfo;

import io.smallrye.graphql.client.model.Annotations;
import io.smallrye.graphql.client.model.Scalars;
import io.smallrye.graphql.client.typesafe.api.Header;

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ public TypeModel getMapValueType() {
public boolean isNonNull() {
return isPrimitive() ||
type.hasAnnotation(Annotations.NON_NULL) ||
type.hasAnnotation(Annotations.JAKARTA_NON_NULL);
type.hasAnnotation(Annotations.JAKARTA_NON_NULL)
|| type.hasAnnotation(Annotations.KOTLIN_NOT_NULL);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface ScalarClientApi {
}

@Test
void sclarClientModelTest() throws IOException {
void scalarClientModelTest() throws IOException {
String configKey = "scalar";
ClientModels clientModels = ClientModelBuilder.build(Index.of(ScalarClientApi.class));
assertNotNull(clientModels.getClientModelByConfigKey(configKey));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.smallrye.graphql.client.model

import io.smallrye.graphql.client.typesafe.api.GraphQLClientApi
import org.eclipse.microprofile.graphql.Query
import org.jboss.jandex.Index
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test

class ClientModelBuilderKotlinTest {
@GraphQLClientApi(configKey = "some-client")
interface ClientApi {
@Query
fun returnNonNullFloat(someFloat: Float): Float

@Query
fun returnNullableString(someString: String?): String?

@Query
fun returnList(list: List<String>): String

// fixme: nullability is bit inconsistent from kotlin

}

@Test
fun basicClientModelTest() {
val configKey = "some-client"
val clientModels = ClientModelBuilder.build(Index.of(ClientApi::class.java))
assertNotNull(clientModels)
val clientModel = clientModels.getClientModelByConfigKey(configKey)
assertNotNull(clientModel)
assertEquals(3, clientModel.operationMap.size)
assertOperation(clientModel, MethodKey("returnNonNullFloat", arrayOf(Float::class.java)), "query returnNonNullFloat(\$someFloat: Float!) { returnNonNullFloat(someFloat: \$someFloat) }")
assertOperation(clientModel, MethodKey("returnNullableString", arrayOf(String::class.java)), "query returnNullableString(\$someString: String) { returnNullableString(someString: \$someString) }")
assertOperation(clientModel, MethodKey("returnList", arrayOf(List::class.java)), "query returnList(\$list: [String!]!) { returnList(list: \$list) }")
}

private fun assertOperation(clientModel: ClientModel, methodKey: MethodKey, expectedQuery: String) {
val actualQuery = clientModel.operationMap[methodKey]
assertEquals(expectedQuery, actualQuery)
}
}
4 changes: 2 additions & 2 deletions common/schema-builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<artifactId>smallrye-graphql-schema-builder</artifactId>
<name>SmallRye: GraphQL Common :: Schema Builder</name>
<description>Creates the model from a Jandex index</description>

<dependencies>

<!-- The API -->
Expand Down Expand Up @@ -40,7 +40,7 @@
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-metadata-jvm</artifactId>
<version>${version.kotlin.metadata.jvm}</version>
<version>${version.kotlin}</version>
</dependency>

<!-- Logging -->
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<maven.compiler.release>17</maven.compiler.release>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<version.kotlin.metadata.jvm>2.1.0</version.kotlin.metadata.jvm>
<version.kotlin>2.1.10</version.kotlin>
<version.impsort.plugin>1.12.0</version.impsort.plugin>
<version.kotlin.compiler>2.1.0</version.kotlin.compiler>

Expand Down

0 comments on commit c899e34

Please sign in to comment.