-
Notifications
You must be signed in to change notification settings - Fork 1.5k
JAVA-6224 Ensure Kotlin can encode ByteArrays correctly #2019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rozza
wants to merge
9
commits into
mongodb:main
Choose a base branch
from
rozza:JAVA-6224
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ad6f7ae
Encode ByteArray data class fields as BSON Binary
rozza b25de95
Add opt-in ByteArray as BSON Binary serializer for bson-kotlinx
rozza 0562f68
Merge branch 'main' into JAVA-6224
rozza 0f29d4d
Merge branch 'main' into JAVA-6224
rozza 38c0df7
JAVA-6224 Address review feedback
rozza 9342143
Merge branch 'main' into JAVA-6224
rozza 69e2447
Handle legacy arrays of int32 when decoding into byte arrays
rozza 3129e4d
PR updates
rozza 94f7a38
Update bson-kotlinx/src/test/kotlin/org/bson/codecs/kotlinx/KotlinSer…
rozza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
69 changes: 69 additions & 0 deletions
69
bson-kotlin/src/main/kotlin/org/bson/codecs/kotlin/LenientByteArrayCodec.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.bson.codecs.kotlin | ||
|
|
||
| import java.io.ByteArrayOutputStream | ||
| import org.bson.BsonInvalidOperationException | ||
| import org.bson.BsonReader | ||
| import org.bson.BsonType | ||
| import org.bson.codecs.ByteArrayCodec | ||
| import org.bson.codecs.DecoderContext | ||
|
|
||
| /** | ||
| * A [ByteArrayCodec] that also decodes the legacy BSON Array representation of a `ByteArray`. | ||
| * | ||
| * Versions 5.2.0 - 5.9.x encoded `ByteArray` data class fields as a BSON Array of Int32, one element per byte, rather | ||
| * than as a compact BSON Binary. This codec still decodes those documents, so data written by those versions remains | ||
| * readable. | ||
| * | ||
| * Encoding is inherited unchanged from [ByteArrayCodec] and always produces BSON Binary. Re-saving a document therefore | ||
| * migrates it away from the legacy representation. | ||
| * | ||
| * Only values the legacy encoder could have produced are accepted: every element must be an Int32 within the signed | ||
| * byte range. Anything else throws [BsonInvalidOperationException] rather than silently decoding an unrelated BSON | ||
| * Array into bytes. | ||
| */ | ||
| internal object LenientByteArrayCodec : ByteArrayCodec() { | ||
|
|
||
| override fun decode(reader: BsonReader, decoderContext: DecoderContext): ByteArray = | ||
| if (reader.currentBsonType == BsonType.ARRAY) { | ||
| decodeLegacyArray(reader) | ||
| } else { | ||
| super.decode(reader, decoderContext) | ||
| } | ||
|
|
||
| private fun decodeLegacyArray(reader: BsonReader): ByteArray { | ||
| val bytes = ByteArrayOutputStream() | ||
| reader.readStartArray() | ||
| while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) { | ||
| if (reader.currentBsonType != BsonType.INT32) { | ||
| throw invalidElement(reader.currentBsonType.toString()) | ||
| } | ||
| val value = reader.readInt32() | ||
| if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { | ||
| throw invalidElement(value.toString()) | ||
| } | ||
| bytes.write(value) | ||
| } | ||
| reader.readEndArray() | ||
| return bytes.toByteArray() | ||
| } | ||
|
|
||
| private fun invalidElement(found: String) = | ||
| BsonInvalidOperationException( | ||
| "Invalid element while decoding a byte array from a BSON Array: " + | ||
| "expected an INT32 in the range -128..127, but found $found.") | ||
| } |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fable picked this.
Nested
Array<ByteArray>legacy decoding still fails with the default registryArrayCodec.createresolves element codecs viacodecRegistry.get(byte[].class). InMongoClientSettings.DEFAULT_CODEC_REGISTRY,ValueCodecProviderprecedesKotlinCodecProvider, so nested elements get the plainByteArrayCodecinstead ofLenientByteArrayCodec. The new test passes only because the test registry listsArrayCodecProviderfirst.Repro (production ordering):
Suggested fix in
ArrayCodec.create— resolveByteArrayelements directly, like the existing top-level special cases:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!