Skip to content

Fix Kotlin bson decoding of optionals#1941

Open
rozza wants to merge 3 commits intomongodb:mainfrom
rozza:JAVA-6162
Open

Fix Kotlin bson decoding of optionals#1941
rozza wants to merge 3 commits intomongodb:mainfrom
rozza:JAVA-6162

Conversation

@rozza
Copy link
Copy Markdown
Member

@rozza rozza commented Apr 13, 2026

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

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
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.decode to stop pre-populating constructor args with null, and to validate missing required fields after reading the document.
  • Add a new DataClassWithDefaultsAndNulls sample in both bson-kotlin and bson-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.

@rozza rozza requested a review from Copilot April 13, 2026 15:53
@rozza rozza marked this pull request as ready for review April 13, 2026 15:53
@rozza rozza requested a review from a team as a code owner April 13, 2026 15:53
@rozza rozza requested a review from vbabanin April 13, 2026 15:53
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants