From 7e07143a3ed20e83d3a7be2f488e04ca083401c0 Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh Date: Wed, 2 Apr 2025 09:58:15 -0400 Subject: [PATCH 1/9] add snake case serialization eg --- ...onTest.snippet.snake-case-serialization.kt | 3 +++ .../data-formats/serialization.txt | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 source/examples/generated/KotlinXSerializationTest.snippet.snake-case-serialization.kt diff --git a/source/examples/generated/KotlinXSerializationTest.snippet.snake-case-serialization.kt b/source/examples/generated/KotlinXSerializationTest.snippet.snake-case-serialization.kt new file mode 100644 index 00000000..5d725353 --- /dev/null +++ b/source/examples/generated/KotlinXSerializationTest.snippet.snake-case-serialization.kt @@ -0,0 +1,3 @@ +val myCustomCodec = KotlinSerializerCodec.create( + bsonConfiguration = BsonConfiguration(bsonNamingStrategy = BsonNamingStrategy.SNAKE_CASE) +) diff --git a/source/fundamentals/data-formats/serialization.txt b/source/fundamentals/data-formats/serialization.txt index 0a95e955..5756f0d0 100644 --- a/source/fundamentals/data-formats/serialization.txt +++ b/source/fundamentals/data-formats/serialization.txt @@ -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 @@ -241,12 +242,28 @@ The following example shows how to create a codec using the .. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.custom-serialization.kt :language: kotlin +Enforcing Snake Case Naming Example +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The following example shows how to convert field names from their data class +fields into snake case by setting the ``bsonNamingStrategy`` parameter: + +.. code-block:: kotlin + :copyable: true + + import org.bson.codecs.kotlinx.BsonConfiguration + import org.bson.codecs.kotlinx.BsonNamingStrategy + +.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.snake-case-serialization.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: From 5f885f0fe038053504da162827b86e8945144cb8 Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh Date: Wed, 2 Apr 2025 10:07:37 -0400 Subject: [PATCH 2/9] version note --- source/fundamentals/data-formats/serialization.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/fundamentals/data-formats/serialization.txt b/source/fundamentals/data-formats/serialization.txt index 5756f0d0..9acdcee6 100644 --- a/source/fundamentals/data-formats/serialization.txt +++ b/source/fundamentals/data-formats/serialization.txt @@ -257,6 +257,10 @@ fields into snake case by setting the ``bsonNamingStrategy`` parameter: .. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.snake-case-serialization.kt :language: kotlin +.. note:: + + The ``bsonNamingStrategy`` parameter is available for MongoDB v5.4 and later. + For more information about the methods and classes mentioned in this section, see the following API documentation: From beeda54dfb94e3b5880fd2ec922691eebe1a48da Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh Date: Wed, 2 Apr 2025 10:11:25 -0400 Subject: [PATCH 3/9] driver reference --- source/fundamentals/data-formats/serialization.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/fundamentals/data-formats/serialization.txt b/source/fundamentals/data-formats/serialization.txt index 9acdcee6..721cf3aa 100644 --- a/source/fundamentals/data-formats/serialization.txt +++ b/source/fundamentals/data-formats/serialization.txt @@ -259,7 +259,7 @@ fields into snake case by setting the ``bsonNamingStrategy`` parameter: .. note:: - The ``bsonNamingStrategy`` parameter is available for MongoDB v5.4 and later. + The ``bsonNamingStrategy`` parameter is available for {+driver-short+} v5.4 and later. For more information about the methods and classes mentioned in this section, see the following API documentation: From 8f361d9d6f8acdef1f367b1749fe469f2e78733a Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh Date: Wed, 2 Apr 2025 10:21:14 -0400 Subject: [PATCH 4/9] what's new --- source/fundamentals/data-formats/serialization.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/fundamentals/data-formats/serialization.txt b/source/fundamentals/data-formats/serialization.txt index 721cf3aa..141e4765 100644 --- a/source/fundamentals/data-formats/serialization.txt +++ b/source/fundamentals/data-formats/serialization.txt @@ -242,6 +242,8 @@ 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: + Enforcing Snake Case Naming Example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 3df43e1598d530c7778fc7491e119d31dcc337c3 Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh Date: Wed, 2 Apr 2025 10:29:17 -0400 Subject: [PATCH 5/9] release note --- source/whats-new.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/whats-new.txt b/source/whats-new.txt index 12874633..c7e71718 100644 --- a/source/whats-new.txt +++ b/source/whats-new.txt @@ -31,6 +31,10 @@ What's New in 5.4 The 5.4 driver release includes the following changes, fixes, and features: +- Adds support for converting field names to snake case during serialization. To + learn more, see the :ref:`kotlin-serialization-snake-case-eg` on the + Serialization page. + .. sharedinclude:: dbx/jvm/v5.4-wn-items.rst .. replacement:: install-bom-link From a1ba7f3a1eaccf12904e5da7788318206b5f781f Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh Date: Wed, 2 Apr 2025 12:57:56 -0400 Subject: [PATCH 6/9] dependency name --- source/fundamentals/data-formats/serialization.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/fundamentals/data-formats/serialization.txt b/source/fundamentals/data-formats/serialization.txt index 141e4765..b508f66b 100644 --- a/source/fundamentals/data-formats/serialization.txt +++ b/source/fundamentals/data-formats/serialization.txt @@ -261,7 +261,7 @@ fields into snake case by setting the ``bsonNamingStrategy`` parameter: .. note:: - The ``bsonNamingStrategy`` parameter is available for {+driver-short+} v5.4 and later. + The ``bsonNamingStrategy`` parameter is available for bson-kotlinx v5.4 and later. For more information about the methods and classes mentioned in this section, see the following API documentation: From 462f26f923eee55d269ce7a58890892538b62002 Mon Sep 17 00:00:00 2001 From: rustagir Date: Thu, 3 Apr 2025 10:14:38 -0400 Subject: [PATCH 7/9] bluehawk --- .../test/kotlin/KotlinXSerializationTest.kt | 29 +++++++++++++++++++ ...lizationTest.snippet.snake-case-naming.kt} | 4 +++ .../data-formats/serialization.txt | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) rename source/examples/generated/{KotlinXSerializationTest.snippet.snake-case-serialization.kt => KotlinXSerializationTest.snippet.snake-case-naming.kt} (56%) diff --git a/examples/src/test/kotlin/KotlinXSerializationTest.kt b/examples/src/test/kotlin/KotlinXSerializationTest.kt index 5839be6e..f943aa78 100644 --- a/examples/src/test/kotlin/KotlinXSerializationTest.kt +++ b/examples/src/test/kotlin/KotlinXSerializationTest.kt @@ -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 @@ -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("orders2") + + // :snippet-start: snake-case-naming + val myCustomCodec = KotlinSerializerCodec.create( + 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().find().first().toJson() + assertTrue(result.contains("quantity_of_cans")) + assertFalse(result.contains("ManufacturerName")) + collection.drop() + } + // :snippet-start: kserializer object InstantAsBsonDateTime : KSerializer { override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("InstantAsBsonDateTime", PrimitiveKind.LONG) diff --git a/source/examples/generated/KotlinXSerializationTest.snippet.snake-case-serialization.kt b/source/examples/generated/KotlinXSerializationTest.snippet.snake-case-naming.kt similarity index 56% rename from source/examples/generated/KotlinXSerializationTest.snippet.snake-case-serialization.kt rename to source/examples/generated/KotlinXSerializationTest.snippet.snake-case-naming.kt index 5d725353..2f374d2b 100644 --- a/source/examples/generated/KotlinXSerializationTest.snippet.snake-case-serialization.kt +++ b/source/examples/generated/KotlinXSerializationTest.snippet.snake-case-naming.kt @@ -1,3 +1,7 @@ val myCustomCodec = KotlinSerializerCodec.create( bsonConfiguration = BsonConfiguration(bsonNamingStrategy = BsonNamingStrategy.SNAKE_CASE) ) + +val registry = CodecRegistries.fromRegistries( + CodecRegistries.fromCodecs(myCustomCodec), collection.codecRegistry +) diff --git a/source/fundamentals/data-formats/serialization.txt b/source/fundamentals/data-formats/serialization.txt index b508f66b..1a1ba432 100644 --- a/source/fundamentals/data-formats/serialization.txt +++ b/source/fundamentals/data-formats/serialization.txt @@ -256,7 +256,7 @@ fields into snake case by setting the ``bsonNamingStrategy`` parameter: import org.bson.codecs.kotlinx.BsonConfiguration import org.bson.codecs.kotlinx.BsonNamingStrategy -.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.snake-case-serialization.kt +.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.snake-case-naming.kt :language: kotlin .. note:: From 74302246454dbd087293920996734756da71abdd Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh Date: Thu, 3 Apr 2025 10:57:03 -0400 Subject: [PATCH 8/9] RR feedback --- .../fundamentals/data-formats/serialization.txt | 15 +++++++-------- source/whats-new.txt | 6 +++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/source/fundamentals/data-formats/serialization.txt b/source/fundamentals/data-formats/serialization.txt index 1a1ba432..36391089 100644 --- a/source/fundamentals/data-formats/serialization.txt +++ b/source/fundamentals/data-formats/serialization.txt @@ -244,11 +244,14 @@ The following example shows how to create a codec using the .. _kotlin-serialization-snake-case-eg: -Enforcing Snake Case Naming Example -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Implement Snake Case Naming Strategy +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -The following example shows how to convert field names from their data class -fields into snake case by setting the ``bsonNamingStrategy`` parameter: +When using ``bson-kotlinx`` package v5.4 or later, you can direct the driver to +serialize data class fields 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 @@ -259,10 +262,6 @@ fields into snake case by setting the ``bsonNamingStrategy`` parameter: .. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.snake-case-naming.kt :language: kotlin -.. note:: - - The ``bsonNamingStrategy`` parameter is available for bson-kotlinx v5.4 and later. - For more information about the methods and classes mentioned in this section, see the following API documentation: diff --git a/source/whats-new.txt b/source/whats-new.txt index c7e71718..0a079421 100644 --- a/source/whats-new.txt +++ b/source/whats-new.txt @@ -31,9 +31,9 @@ What's New in 5.4 The 5.4 driver release includes the following changes, fixes, and features: -- Adds support for converting field names to snake case during serialization. To - learn more, see the :ref:`kotlin-serialization-snake-case-eg` on the - Serialization page. +- Adds ``BsonConfiguration`` support for bson-kotlinx snake case conversion + during serialization. To learn more, see the + :ref:`kotlin-serialization-snake-case-eg` on the Serialization page. .. sharedinclude:: dbx/jvm/v5.4-wn-items.rst From fdc81be33ef2810db3367a4f27acb425f444f4e5 Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh Date: Fri, 4 Apr 2025 11:13:22 -0400 Subject: [PATCH 9/9] MW feedback --- source/fundamentals/data-formats/serialization.txt | 2 +- source/whats-new.txt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/fundamentals/data-formats/serialization.txt b/source/fundamentals/data-formats/serialization.txt index 36391089..d6fe5f10 100644 --- a/source/fundamentals/data-formats/serialization.txt +++ b/source/fundamentals/data-formats/serialization.txt @@ -248,7 +248,7 @@ Implement Snake Case Naming Strategy ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ When using ``bson-kotlinx`` package v5.4 or later, you can direct the driver to -serialize data class fields names written in camel case to snake case in MongoDB. +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: diff --git a/source/whats-new.txt b/source/whats-new.txt index 0a079421..e3d7c6ef 100644 --- a/source/whats-new.txt +++ b/source/whats-new.txt @@ -31,9 +31,9 @@ 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 +- Adds ``BsonConfiguration`` support for ``bson-kotlinx`` snake case conversion during serialization. To learn more, see the - :ref:`kotlin-serialization-snake-case-eg` on the Serialization page. + :ref:`kotlin-serialization-snake-case-eg` section on the Serialization page. .. sharedinclude:: dbx/jvm/v5.4-wn-items.rst