Ensure Kotlin can encode ByteArrays correctly#2019
Open
rozza wants to merge 3 commits into
Open
Conversation
Narrow the DataClassCodec array interception to exclude ByteArray so it falls through to ByteArrayCodec (BSON Binary), restoring pre-5.1.3 behavior. ArrayCodecProvider is registered before ByteArrayCodec in both the test registry and the production KotlinCodecProvider, so it must apply the same ByteArray exclusion and return null to let the registry fall through to ByteArrayCodec. Object arrays still route through ArrayCodec (JAVA-5122). JAVA-6224
kotlinx.serialization encodes a ByteArray as a BSON array of int32 elements by default, which differs from bson-kotlin's DataClassCodec (compact BSON Binary). Add an opt-in ByteArrayAsBsonBinary KSerializer so users can store ByteArray fields as BsonBinary via @serializable(with = ByteArrayAsBsonBinary::class) or @contextual plus its serializersModule. It is not registered in defaultSerializersModule, so existing behavior and on-disk format are unchanged. JAVA-6224
rozza
requested review from
Copilot and
nhachicha
and removed request for
nhachicha
July 20, 2026 14:55
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses ByteArray encoding differences/regressions across the Kotlin codecs by ensuring bson-kotlin encodes ByteArray as compact BSON Binary and by adding an opt-in kotlinx.serialization serializer to encode ByteArray as BSON Binary when desired.
Changes:
- Update
bson-kotlincodec selection soByteArrayusesByteArrayCodec(BSON Binary) instead of the generic array codec. - Add an opt-in
kotlinx.serializationKSerializer<ByteArray>(ByteArrayAsBsonBinary) for compact BSON Binary encoding inbson-kotlinx. - Extend tests and sample data classes in both modules to cover
ByteArrayfields and nestedByteArrayarrays.
Reviewed changes
Copilot reviewed 7 out of 7 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 sample serializable data classes exercising ByteArray and nested arrays with opt-in binary serialization. |
| bson-kotlinx/src/test/kotlin/org/bson/codecs/kotlinx/KotlinSerializerCodecTest.kt | Adds round-trip tests documenting default ByteArray behavior and verifying opt-in binary encoding via ByteArrayAsBsonBinary. |
| bson-kotlinx/src/main/kotlin/org/bson/codecs/kotlinx/ByteArrayAsBsonBinary.kt | Introduces a KSerializer<ByteArray> that encodes/decodes as compact BsonBinary and provides a contextual SerializersModule. |
| bson-kotlin/src/test/kotlin/org/bson/codecs/kotlin/samples/DataClasses.kt | Extends Kotlin (non-kotlinx) sample data classes to include ByteArray and nested ByteArray arrays with correct equality semantics. |
| bson-kotlin/src/test/kotlin/org/bson/codecs/kotlin/DataClassCodecTest.kt | Adds/updates tests to assert ByteArray encodes as BSON Binary and remains size-efficient; extends array test coverage. |
| bson-kotlin/src/main/kotlin/org/bson/codecs/kotlin/DataClassCodec.kt | Prevents ByteArray from being treated as a generic array so the registry can select ByteArrayCodec. |
| bson-kotlin/src/main/kotlin/org/bson/codecs/kotlin/ArrayCodecProvider.kt | Ensures ByteArray falls through to ByteArrayCodec by returning null for ByteArray rather than an ArrayCodec. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JAVA-6224