Skip to content

Commit 7f5b3c8

Browse files
Kotlin: fix default value (#882)
1 parent 4fb7dbf commit 7f5b3c8

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

src/main/resources/templates/kotlin-lang/request.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ open class ${className}(private val alias: String?) : GraphQLOperationRequest {
107107
<#else>
108108
<#if MapperUtil.isKotlinPrimitive(field.type)>
109109
<#assign default = MapperUtil.defaultValueKotlinPrimitive(field.type)/>
110-
private var ${field.name}: ${field.type} = default
110+
private var ${field.name}: ${field.type} = ${default}
111111
<#else>
112112
private lateinit var ${field.name}: ${field.type}
113113
</#if>

src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenGitHubTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static java.util.Collections.singletonList;
2020
import static org.hamcrest.MatcherAssert.assertThat;
2121

22+
2223
class GraphQLCodegenGitHubTest {
2324

2425
private final File outputBuildDir = new File("build/generated");
@@ -187,4 +188,16 @@ void generate_CustomFieldsResolvers() throws Exception {
187188
getFileByName(files, "AcceptTopicSuggestionPayloadResolver.kt"));
188189
}
189190

191+
@Test
192+
void generate_RequestWithDefaultValue() throws Exception {
193+
mappingConfig.setGenerateBuilder(true);
194+
mappingConfig.setGenerateClient(true);
195+
new KotlinGraphQLCodegen(singletonList("src/test/resources/schemas/kt/default.graphqls"),
196+
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
197+
File[] files = Objects.requireNonNull(outputktClassesDir.listFiles());
198+
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/default/" +
199+
"FriendsQueryRequest.kt.txt"),
200+
getFileByName(files, "FriendsQueryRequest.kt"));
201+
}
202+
190203
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.github.graphql
2+
3+
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation
4+
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperationRequest
5+
import java.util.Objects
6+
7+
@javax.annotation.Generated(
8+
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
9+
date = "2020-12-31T23:59:59-0500"
10+
)
11+
open class FriendsQueryRequest(private val alias: String?) : GraphQLOperationRequest {
12+
13+
companion object {
14+
const val OPERATION_NAME: String = "friends"
15+
val OPERATION_TYPE: GraphQLOperation = GraphQLOperation.QUERY
16+
17+
@JvmStatic fun builder(): Builder = Builder()
18+
}
19+
20+
private val input: MutableMap<String, Any?> = LinkedHashMap()
21+
private val useObjectMapperForInputSerialization: MutableSet<String> = HashSet()
22+
23+
constructor(): this(null)
24+
25+
fun setNum(num: Int) {
26+
this.input["num"] = num
27+
}
28+
29+
override fun getOperationType(): GraphQLOperation = OPERATION_TYPE
30+
31+
override fun getOperationName(): String = OPERATION_NAME
32+
33+
override fun getAlias(): String? = alias ?: OPERATION_NAME
34+
35+
override fun getInput(): MutableMap<String, Any?> = input
36+
37+
override fun getUseObjectMapperForInputSerialization(): MutableSet<String> = useObjectMapperForInputSerialization
38+
39+
override fun equals(other: Any?): Boolean {
40+
if (this === other) {
41+
return true
42+
}
43+
if (other == null || javaClass != other.javaClass) {
44+
return false
45+
}
46+
val that = other as FriendsQueryRequest
47+
return Objects.equals(operationType, that.operationType) &&
48+
Objects.equals(operationName, that.operationName) &&
49+
Objects.equals(input, that.input)
50+
}
51+
52+
override fun hashCode(): Int = Objects.hash(operationType, operationName, input)
53+
54+
override fun toString(): String = Objects.toString(input)
55+
56+
class Builder {
57+
58+
private var `$alias`: String? = null
59+
private var num: Int = 0
60+
61+
fun alias(alias: String?): Builder {
62+
this.`$alias` = alias
63+
return this
64+
}
65+
66+
fun setNum(num: Int): Builder {
67+
this.num = num
68+
return this
69+
}
70+
71+
fun build(): FriendsQueryRequest {
72+
val obj = FriendsQueryRequest(`$alias`)
73+
obj.setNum(num)
74+
return obj
75+
}
76+
77+
}
78+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type Query {
2+
friends(num: Int!): [Friend]
3+
}
4+
5+
type Friend {
6+
name: String
7+
}

0 commit comments

Comments
 (0)