Skip to content

[Kotlin CR] Support naming strategy for bson-kotlinx #209

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

Merged
merged 9 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions examples/src/test/kotlin/KotlinXSerializationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.bson.codecs.configuration.CodecRegistries
import org.bson.codecs.kotlinx.BsonConfiguration
import org.bson.codecs.kotlinx.BsonDecoder
import org.bson.codecs.kotlinx.BsonEncoder
import org.bson.codecs.kotlinx.BsonNamingStrategy
import org.bson.codecs.kotlinx.KotlinSerializerCodec
import org.bson.codecs.kotlinx.ObjectIdSerializer
import org.bson.types.ObjectId
Expand Down Expand Up @@ -133,6 +134,34 @@ internal class KotlinXSerializationTest {
collection.drop()
}

@Test
fun snakeCaseNamingTest() = runBlocking {
@Serializable
data class PaintOrder(
val ManufacturerName: String,
val QuantityOfCans: Int,
)

val collection = database.getCollection<PaintOrder>("orders2")

// :snippet-start: snake-case-naming
val myCustomCodec = KotlinSerializerCodec.create<PaintOrder>(
bsonConfiguration = BsonConfiguration(bsonNamingStrategy = BsonNamingStrategy.SNAKE_CASE)
)

val registry = CodecRegistries.fromRegistries(
CodecRegistries.fromCodecs(myCustomCodec), collection.codecRegistry
)
// :snippet-end:

val paint = PaintOrder("Acme", 10)
collection.withCodecRegistry(registry).insertOne(paint)
val result = collection.withDocumentClass<Document>().find().first().toJson()
assertTrue(result.contains("quantity_of_cans"))
assertFalse(result.contains("ManufacturerName"))
collection.drop()
}

// :snippet-start: kserializer
object InstantAsBsonDateTime : KSerializer<Instant> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("InstantAsBsonDateTime", PrimitiveKind.LONG)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
val myCustomCodec = KotlinSerializerCodec.create<PaintOrder>(
bsonConfiguration = BsonConfiguration(bsonNamingStrategy = BsonNamingStrategy.SNAKE_CASE)
)

val registry = CodecRegistries.fromRegistries(
CodecRegistries.fromCodecs(myCustomCodec), collection.codecRegistry
)
24 changes: 23 additions & 1 deletion source/fundamentals/data-formats/serialization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ package to create a codec for your ``@Serializable`` data classes and
customize what is stored.

Use the ``BsonConfiguration`` class to define the configuration,
including whether to encode defaults, encode nulls, or define class discriminators.
including whether to encode defaults, encode nulls, define class discriminators,
or enforce snake case.

To create a custom codec, install the ``bson-kotlinx``
dependency to your project. Select from the following tabs to see how to
Expand Down Expand Up @@ -241,12 +242,33 @@ The following example shows how to create a codec using the
.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.custom-serialization.kt
:language: kotlin

.. _kotlin-serialization-snake-case-eg:

Implement Snake Case Naming Strategy
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When using ``bson-kotlinx`` package v5.4 or later, you can direct the driver to
serialize data class field names written in camel case to snake case in MongoDB.
The following example shows how to create and register a custom codec
to convert data class field names into snake case by setting the
``bsonNamingStrategy`` parameter in a codec:

.. code-block:: kotlin
:copyable: true

import org.bson.codecs.kotlinx.BsonConfiguration
import org.bson.codecs.kotlinx.BsonNamingStrategy

.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.snake-case-naming.kt
:language: kotlin

For more information about the methods and classes mentioned in this section,
see the following API documentation:

- `KotlinSerializerCodec <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-kotlin-serializer-codec/index.html>`__
- `KotlinSerializerCodec.create() <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-kotlin-serializer-codec/-companion/create.html>`__
- `BsonConfiguration <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-configuration/index.html>`__
- `BsonNamingStrategy <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-naming-strategy/index.html>`__

.. _kotlin-polymorphic:

Expand Down
4 changes: 4 additions & 0 deletions source/whats-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ What's New in 5.4
The 5.4 driver release includes the following changes, fixes,
and features:

- Adds ``BsonConfiguration`` support for ``bson-kotlinx`` snake case conversion
during serialization. To learn more, see the
:ref:`kotlin-serialization-snake-case-eg` section on the Serialization page.

.. sharedinclude:: dbx/jvm/v5.4-wn-items.rst

.. replacement:: install-bom-link
Expand Down
Loading