Skip to content

Commit 2777032

Browse files
committed
fail: kotlin data classes are not correctly instantiated
1 parent f69aa39 commit 2777032

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

spring-graphql/build.gradle

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
plugins {
2+
id 'org.jetbrains.kotlin.jvm' version '1.5.31'
3+
}
14
description = "GraphQL Support for Spring Applications"
25

36
dependencies {
@@ -34,6 +37,7 @@ dependencies {
3437

3538
testRuntimeOnly 'org.apache.logging.log4j:log4j-core'
3639
testRuntimeOnly 'org.apache.logging.log4j:log4j-slf4j-impl'
40+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
3741
}
3842

3943
test {
@@ -42,3 +46,16 @@ test {
4246
events "passed", "skipped", "failed"
4347
}
4448
}
49+
repositories {
50+
mavenCentral()
51+
}
52+
compileKotlin {
53+
kotlinOptions {
54+
jvmTarget = "1.8"
55+
}
56+
}
57+
compileTestKotlin {
58+
kotlinOptions {
59+
jvmTarget = "1.8"
60+
}
61+
}

spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/ArgumentMethodArgumentResolverTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ void shouldResolveJavaBeanArgument() throws Exception {
8686
.hasFieldOrPropertyWithValue("authorId", 42L);
8787
}
8888

89+
@Test
90+
void shouldResolveKotlinBeanArgument() throws Exception {
91+
Method addBook = ClassUtils.getMethod(BookController.class, "ktAddBook", KotlinBookInput.class);
92+
String payload = "{\"bookInput\": { \"name\": \"test name\", \"authorId\": 42} }";
93+
DataFetchingEnvironment environment = initEnvironment(payload);
94+
MethodParameter methodParameter = getMethodParameter(addBook, 0);
95+
Object result = resolver.resolveArgument(methodParameter, environment);
96+
assertThat(result).isNotNull().isInstanceOf(KotlinBookInput.class);
97+
assertThat((KotlinBookInput) result).hasFieldOrPropertyWithValue("name", "test name")
98+
.hasFieldOrPropertyWithValue("authorId", 42L);
99+
}
100+
89101
@Test
90102
void shouldResolveListOfJavaBeansArgument() throws Exception {
91103
Method addBooks = ClassUtils.getMethod(BookController.class, "addBooks", List.class);
@@ -147,6 +159,11 @@ public Book addBook(@Argument BookInput bookInput) {
147159
return null;
148160
}
149161

162+
@MutationMapping
163+
public Book ktAddBook(@Argument KotlinBookInput bookInput) {
164+
return null;
165+
}
166+
150167
@MutationMapping
151168
public List<Book> addBooks(@Argument List<Book> books) {
152169
return null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package org.springframework.graphql.data.method.annotation.support;
2+
3+
data class KotlinBookInput(val name: String, val authorId: Long)

0 commit comments

Comments
 (0)