From 338989bfc2a2b2b471890e6003b3fa872c4f1d5c Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Sat, 2 Dec 2023 00:27:49 +1100 Subject: [PATCH] fix(kt): optional fields now default to null (#688) Before: ```kotlin data class Foo( val bar: String?, ) ``` After: ```kotlin data class Foo( val bar: String? = null ) ``` --- .../main/kotlin/xyz/block/ftl/generator/ModuleGenerator.kt | 7 ++++++- .../kotlin/xyz/block/ftl/generator/ModuleGeneratorTest.kt | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/kotlin-runtime/ftl-generator/src/main/kotlin/xyz/block/ftl/generator/ModuleGenerator.kt b/kotlin-runtime/ftl-generator/src/main/kotlin/xyz/block/ftl/generator/ModuleGenerator.kt index 71bb91ad42..25e3840431 100644 --- a/kotlin-runtime/ftl-generator/src/main/kotlin/xyz/block/ftl/generator/ModuleGenerator.kt +++ b/kotlin-runtime/ftl-generator/src/main/kotlin/xyz/block/ftl/generator/ModuleGenerator.kt @@ -69,7 +69,12 @@ class ModuleGenerator() { type.fields.forEach { field -> dataClassBuilder.addKdoc(field.comments.joinToString("\n")) field.type?.let { - dataConstructorBuilder.addParameter(field.name, getTypeClass(it, namespace)) + var parameter = ParameterSpec + .builder(field.name, getTypeClass(it, namespace)) + if (it.optional != null) { + parameter = parameter.defaultValue("null") + } + dataConstructorBuilder.addParameter(parameter.build()) dataClassBuilder.addProperty( PropertySpec.builder(field.name, getTypeClass(it, namespace)).initializer(field.name).build() ) diff --git a/kotlin-runtime/ftl-generator/src/test/kotlin/xyz/block/ftl/generator/ModuleGeneratorTest.kt b/kotlin-runtime/ftl-generator/src/test/kotlin/xyz/block/ftl/generator/ModuleGeneratorTest.kt index 4e5431135b..a3b76c482f 100644 --- a/kotlin-runtime/ftl-generator/src/test/kotlin/xyz/block/ftl/generator/ModuleGeneratorTest.kt +++ b/kotlin-runtime/ftl-generator/src/test/kotlin/xyz/block/ftl/generator/ModuleGeneratorTest.kt @@ -108,7 +108,7 @@ public data class TestResponse( public val string: String, public val bool: Boolean, public val time: OffsetDateTime, - public val optional: String?, + public val optional: String? = null, public val array: ArrayList, public val nestedArray: ArrayList>, public val dataRefArray: ArrayList,