未验证 提交 7f5b3c8a 编写于 作者: 梦境迷离's avatar 梦境迷离 提交者: GitHub

Kotlin: fix default value (#882)

上级 4fb7dbf9
......@@ -107,7 +107,7 @@ open class ${className}(private val alias: String?) : GraphQLOperationRequest {
<#else>
<#if MapperUtil.isKotlinPrimitive(field.type)>
<#assign default = MapperUtil.defaultValueKotlinPrimitive(field.type)/>
private var ${field.name}: ${field.type} = default
private var ${field.name}: ${field.type} = ${default}
<#else>
private lateinit var ${field.name}: ${field.type}
</#if>
......
......@@ -19,6 +19,7 @@ import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName;
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
class GraphQLCodegenGitHubTest {
private final File outputBuildDir = new File("build/generated");
......@@ -187,4 +188,16 @@ class GraphQLCodegenGitHubTest {
getFileByName(files, "AcceptTopicSuggestionPayloadResolver.kt"));
}
@Test
void generate_RequestWithDefaultValue() throws Exception {
mappingConfig.setGenerateBuilder(true);
mappingConfig.setGenerateClient(true);
new KotlinGraphQLCodegen(singletonList("src/test/resources/schemas/kt/default.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
File[] files = Objects.requireNonNull(outputktClassesDir.listFiles());
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/default/" +
"FriendsQueryRequest.kt.txt"),
getFileByName(files, "FriendsQueryRequest.kt"));
}
}
\ No newline at end of file
package com.github.graphql
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperationRequest
import java.util.Objects
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
open class FriendsQueryRequest(private val alias: String?) : GraphQLOperationRequest {
companion object {
const val OPERATION_NAME: String = "friends"
val OPERATION_TYPE: GraphQLOperation = GraphQLOperation.QUERY
@JvmStatic fun builder(): Builder = Builder()
}
private val input: MutableMap<String, Any?> = LinkedHashMap()
private val useObjectMapperForInputSerialization: MutableSet<String> = HashSet()
constructor(): this(null)
fun setNum(num: Int) {
this.input["num"] = num
}
override fun getOperationType(): GraphQLOperation = OPERATION_TYPE
override fun getOperationName(): String = OPERATION_NAME
override fun getAlias(): String? = alias ?: OPERATION_NAME
override fun getInput(): MutableMap<String, Any?> = input
override fun getUseObjectMapperForInputSerialization(): MutableSet<String> = useObjectMapperForInputSerialization
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
if (other == null || javaClass != other.javaClass) {
return false
}
val that = other as FriendsQueryRequest
return Objects.equals(operationType, that.operationType) &&
Objects.equals(operationName, that.operationName) &&
Objects.equals(input, that.input)
}
override fun hashCode(): Int = Objects.hash(operationType, operationName, input)
override fun toString(): String = Objects.toString(input)
class Builder {
private var `$alias`: String? = null
private var num: Int = 0
fun alias(alias: String?): Builder {
this.`$alias` = alias
return this
}
fun setNum(num: Int): Builder {
this.num = num
return this
}
fun build(): FriendsQueryRequest {
val obj = FriendsQueryRequest(`$alias`)
obj.setNum(num)
return obj
}
}
}
\ No newline at end of file
type Query {
friends(num: Int!): [Friend]
}
type Friend {
name: String
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册