Conversation
Previously, DataClassCodec pre-populated all constructor parameters with null before reading the document, which prevented callBy from using Kotlin default parameter values. Now optional parameters missing from the document are left absent from the args map so callBy invokes their defaults, and a clear CodecConfigurationException is thrown when a required non-nullable field is missing. Ported the bson-kotlin test cases to bson-kotlinx to ensure coverage and prevent future regressions. JAVA-6162
There was a problem hiding this comment.
Pull request overview
Fixes Kotlin BSON decoding of data classes so that missing optional constructor parameters can correctly fall back to Kotlin default parameter values, while missing required non-nullable fields fail with a clear exception. Adds/updates test coverage (ported from bson-kotlin to bson-kotlinx) around defaults + null-handling to prevent regressions.
Changes:
- Update
DataClassCodec.decodeto stop pre-populating constructor args withnull, and to validate missing required fields after reading the document. - Add a new
DataClassWithDefaultsAndNullssample in bothbson-kotlinandbson-kotlinx. - Expand tests in both modules to explicitly assert decoding behavior for missing fields vs explicit
nulls vs defaults.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| bson-kotlinx/src/test/kotlin/org/bson/codecs/kotlinx/samples/DataClasses.kt | Adds DataClassWithDefaultsAndNulls sample for kotlinx-serialization codec tests. |
| bson-kotlinx/src/test/kotlin/org/bson/codecs/kotlinx/KotlinSerializerCodecTest.kt | Adds decode assertions for defaults/nulls and a missing-required-field failure case. |
| bson-kotlin/src/test/kotlin/org/bson/codecs/kotlin/samples/DataClasses.kt | Adds DataClassWithDefaultsAndNulls sample for reflection-based DataClassCodec tests. |
| bson-kotlin/src/test/kotlin/org/bson/codecs/kotlin/DataClassCodecTest.kt | Expands tests to assert decoding behavior for defaults/nulls and missing required field exception. |
| bson-kotlin/src/main/kotlin/org/bson/codecs/kotlin/DataClassCodec.kt | Core logic change: don’t prefill args with null; validate missing required fields and preserve defaults via callBy. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Previously, DataClassCodec pre-populated all constructor parameters with null before reading the document, which prevented callBy from using Kotlin default parameter values.
Now optional parameters missing from the document are left absent from the args map so callBy invokes their defaults, and a clear CodecConfigurationException is thrown when a required non-nullable field is missing.
Ported the bson-kotlin test cases to bson-kotlinx to ensure coverage and prevent future regressions.
JAVA-6162