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

When using SerialName, the Bson document generated by kmongo is incorrect #415

Closed
FishHawk opened this issue Apr 26, 2023 · 2 comments
Closed

Comments

@FishHawk
Copy link

When using SerialName, the Bson document generated by kmongo is incorrect.

Code:

@Serializable
data class BookEpisode(
    ...
    @SerialName("paragraphsZh")
    val baiduParagraphs: List<String>?,
    val youdaoParagraphs: List<String>? = null,
)

println(setValue(BookEpisode::youdaoParagraphs.pos(0), "test").toBsonDocument())
println(setValue(BookEpisode::baiduParagraphs.pos(0), "test").toBsonDocument())

Output:

{"$set": {"youdaoParagraphs.0": "test"}}
{"$set": {"paragraphsZh.paragraphsZh": "test"}}
@miguelalexteixeira
Copy link

miguelalexteixeira commented May 11, 2023

Found a similar issue with posOp. When adding an array positional operator, the lib is creating an instance of CustomProperty which inherits the annotations from the previous path property:

private class CustomProperty<T, R>(val previous: KPropertyPath<*, T>, path: String) : KProperty1<T, R> {
    override val annotations: List<Annotation> get() = previous.annotations
    // ...
}

When resolving the property path, it calls calculatePath in SerializationClassMappingTypeService, which will return the SerialName's value:

override fun <T> calculatePath(property: KProperty<T>): String =
    property.findAnnotation<SerialName>()?.value
        ?: (if (property.hasAnnotation<MongoId>()) "_id" else property.findAnnotation<MongoProperty>()?.value)
        ?: property.name

@zigzago
Copy link
Member

zigzago commented May 19, 2023

Thank you for reporting. Will be fixed in next release (coming soon)

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

No branches or pull requests

3 participants