Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
- A detailed exception is thrown if more than duplicate is detected w…
…ith snake case

- Fixing formatting
  • Loading branch information
nhachicha committed Feb 7, 2025
commit f24526dc28f647c62dc944d7f4b90888b8fe417e
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ internal object BsonCodecUtils {
.groupBy { entry -> entry.value }
.filter { group -> group.value.size > 1 }
.entries
.forEach { group ->
.fold(StringBuilder("")) { acc, group ->
val keys = group.value.joinToString(", ") { entry -> entry.key }
throw SerializationException(
"$keys in ${descriptor.serialName} generate same name: ${group.key}.")
acc.append("$keys in ${descriptor.serialName} generate same name: ${group.key}.\n")
}
.toString()
.takeIf { it.trim().isNotEmpty() }
?.let { errorMessage: String -> throw SerializationException(errorMessage) }

snakeCasedNames.entries.associate { it.value to it.key }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1141,16 +1141,18 @@ class KotlinSerializerCodecTest {

@Test
fun testSameSnakeCaseName() {
val expected = """{"my_http_auth": ""}"""
val expected = """{"my_http_auth": "", "my_http_auth1": ""}"""
val dataClass = DataClassWithSameSnakeCaseName()
val exception =
assertThrows<SerializationException> {
assertRoundTrips(
expected, dataClass, BsonConfiguration(bsonNamingStrategy = BsonNamingStrategy.SNAKE_CASE))
}
assertEquals(
"myHTTPAuth, myHttpAuth in org.bson.codecs.kotlinx.samples.DataClassWithSameSnakeCaseName " +
"generate same name: my_http_auth.",
"myHTTPAuth, myHttpAuth in org.bson.codecs.kotlinx.samples.DataClassWithSameSnakeCaseName" +
" generate same name: my_http_auth.\n" +
"myHTTPAuth1, myHttpAuth1 in org.bson.codecs.kotlinx.samples.DataClassWithSameSnakeCaseName" +
" generate same name: my_http_auth1.\n",
exception.message)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ data class DataClassWithCamelCase(
data class DataClassWithSameSnakeCaseName(
val myHTTPAuth: String = "",
val myHttpAuth: String = "",
val myHTTPAuth1: String = "",
val myHttpAuth1: String = "",
)

@Serializable
Expand Down