-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Set default to null when the field type is nullable (activable …
…by the configuration)
- Loading branch information
Showing
8 changed files
with
138 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/test/kotlin/com/github/avrokotlin/avro4k/schema/NullableWithDefaultsSchemaTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.github.avrokotlin.avro4k.schema | ||
|
||
import com.github.avrokotlin.avro4k.Avro | ||
import com.github.avrokotlin.avro4k.AvroConfiguration | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.serialization.Serializable | ||
|
||
class NullableWithDefaultsSchemaTest : FunSpec({ | ||
|
||
test("generate null as Union[T, Null]") { | ||
|
||
val expected = org.apache.avro.Schema.Parser().parse(javaClass.getResourceAsStream("/nullables-with-defaults.json")) | ||
val schema = Avro(AvroConfiguration(defaultNullForNullableFields = true)).schema(Test.serializer()) | ||
schema.toString(true) shouldBe expected.toString(true) | ||
} | ||
|
||
// test("move default option values to first schema as per avro spec") { | ||
// val schema = AvroSchema[OptionWithDefault] | ||
// val expected = new org . apache . avro . Schema . Parser ().parse(getClass.getResourceAsStream("/option_default_value.json")) | ||
// schema.toString(true) shouldBe expected.toString(true) | ||
// } | ||
// | ||
// test("if a field has a default value of null then define the field to be nullable") { | ||
// val schema = AvroSchema[FieldWithNull] | ||
// val expected = new org . apache . avro . Schema . Parser ().parse(getClass.getResourceAsStream("/option_from_null_default.json")) | ||
// schema.toString(true) shouldBe expected.toString(true) | ||
// } | ||
|
||
}) { | ||
@Serializable | ||
data class Test(val nullableString: String?, val nullableBoolean: Boolean?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"type": "record", | ||
"name": "Test", | ||
"namespace": "com.github.avrokotlin.avro4k.schema.NullableWithDefaultsSchemaTest", | ||
"fields": [ | ||
{ | ||
"name": "nullableString", | ||
"type": [ | ||
"null", | ||
"string" | ||
], | ||
"default": null | ||
}, | ||
{ | ||
"name": "nullableBoolean", | ||
"type": [ | ||
"null", | ||
"boolean" | ||
], | ||
"default": null | ||
} | ||
] | ||
} |