Skip to content
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

Updates bson-kotlin DataClassCodec.kt to allow the usage of default values #1260

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

filipeportes
Copy link

@filipeportes filipeportes commented Nov 15, 2023

avoids providing null to parameters that are not nullable, allowing for the usage of default values.

example:

given a data Class:

data class Item(val name:String, val category:String = "default")

the following objects will produce CodecConfigurationException: Unable to invoke primary constructor of Item data class:

[
 {
  name: "item1"
 },
 {
  name: "item2",
  category: null
 }
]

with the changes proposed in this PR, the result will be decoding the documents and using the default values in case no actual values are provided.

PS:

  • In case the parameter is required and no default value is provided, the current behaviour of throwing an exception will continue as expected.
  • In case the parameter is nullable with a default value and a null value is provided, the current behaviour of using null will continue as expected

avoids providing null to parameters that are not nullable, allowing for the usage of default values
@filipeportes filipeportes changed the title Updates DataClassCodec.kt to allow the usage of default values when decoding a document with null or missing properties Updates Kotlin DataClassCodec.kt to allow the usage of default values Nov 15, 2023
@filipeportes filipeportes changed the title Updates Kotlin DataClassCodec.kt to allow the usage of default values Updates bson-kotlin DataClassCodec.kt to allow the usage of default values Nov 15, 2023
@filipeportes
Copy link
Author

Hi @jyemin could you take a look please 🙏

@rozza
Copy link
Member

rozza commented Nov 27, 2023

The kotlin bson codec already supports default values for data classes. This PR is something slightly different, it wants to use default values where the data stored is a BsonNull.

What is the correct thing to when a field is nullable and there is a null value in the database? eg:

data class Item(val name:String, val category:String? = "default")

Will all users want to allow a null value in the database to be replaced as the default value for a data class?

@filipeportes
Copy link
Author

What is the correct thing to when a field is nullable and there is a null value in the database? eg:

data class Item(val name:String, val category:String? = "default")

Will all users want to allow a null value in the database to be replaced as the default value for a data class?

hey @rozza thanks for taking a look 🙏

the changes on the PR will not affect the case you described, since in this case category is nullable, the filter on isRequiredParameterWithNullValue will not get this field and if the value is a BsonNull it will be passed to the constructor as usual

@filipeportes
Copy link
Author

Hi @rozza could you take another quick look?

@rozza
Copy link
Member

rozza commented Jan 3, 2024

Hi @filipeportes,

Thanks for your perservance with this issue. I've added JAVA-5281 to track it. This will be triaged and if successful scheduled for a future release.

I will update the ticket here also, the open question is, if the change in behavior is the correct change to make and will it also apply to bson-kotlinx.

Ross

@rozza
Copy link
Member

rozza commented Jan 10, 2024

Just to update it looks like there is a way for kotlinx to do this with json, via configuration.

So its now on the backlog for applying to both kotlin-bson and kotlinx-bson. However, there is no firm timeline as to when this work will be scheduled. Please watch the ticket for further details.

Ross

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants