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

Change the retention of the attribute "SerialName" back to "RUNTIME" #1081

Closed
FelixEngl opened this issue Sep 17, 2020 · 3 comments
Closed
Assignees
Labels

Comments

@FelixEngl
Copy link

FelixEngl commented Sep 17, 2020

What is your use-case and why do you need this feature?
I am using the Kotlin-Serializer for a MongoDB driver (MongoDB Java Drivers + KBson and some additional features like a commons library for sharing the data classes representing the documents). The Kotlin-Serializer acts in this case as serializer for the codec-provider of the MongoDB driver. (see MongoDBExample.kt for a simplified code example)

To keep the maintainability of the queries used in the application as simple as possible, i wrote a small DSL wrapping the MongoDB Java Driver (see MongoDBExample.kt - Query DSL Example). Usually the default name reflection of the data class properties is enouth, but due to legacy code, changes of the datamodel, and naming conventions the names in the database and the data class may differ e.g. @SerialName("_id") val id: Id. (see MongoDBExample.kt - MongoDB-naming-convention vs Kotlin-naming-convention).
Hence, by retrieving the SerialName of the KProperty it is possible to write this kind of queries in a very safe and low maintenance manner, given that i don't need additional property-fieldname-mapping in my code. Furthermore the approach in MongoDBExample.kt replaces the use of plain strings for the field-names in the query-definitions, lowering the code maintenance by schema-changes and the error rate by new code even more.

In Kotlin 1.3.72 with kotlinx.serialize 0.20.0 it was possible to get SerialName attribute by reflection, providing a simple way of retrieving the name used in the database-representation (see MongoDBExample.kt - KProperty SerialName Reflection). In Kotlin 1.4.10 with kotlinx.serialize 1.0.0-RC this approach isn't possible anymore. My workaround is a second attribute MongoDBName, redundantly keeping the information of the SerialName attribute for the runtime reflection:

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.PROPERTY)
annotation class MongoDBName(val value: String)

@Serializeable
data class ExampleProduct(
        @SerialName("product_name") @MongoDBName("product_name") val productName: String,
        val price: Float,
        val currency: ECurrency,
        @SerialName("_id") @MongoDBName("_id") val id: Id = Random.nextLong().toString(16)
)

This does not only increase the maintenance costs, but also increases the probability of code errors due to the fact, that one has to add two annotations with the exact same information.

Describe the solution you'd like
Change the retention of the SerialName annotation back to RUNTIME, making it possible to retrieve the SerialName at runtime.

@qwwdfsad qwwdfsad self-assigned this Sep 17, 2020
@FelixEngl
Copy link
Author

FelixEngl commented Sep 17, 2020

@sandwwraith
Copy link
Member

I wonder what other annotations you need with runtime retention. E.g. @Transient, @Required — they all have binary retention. Does this cause problems to you?

@FelixEngl
Copy link
Author

FelixEngl commented Sep 17, 2020

For my use-case i only need SerialName, but i can imagine that these could be usefull during runtime, too:

  • Like checking for a @Transient property when (unit) testing the query templates written with my DSL.
  • For @Required comes nothing specific to my mind, but checking if a property has the @Required-attribute could be usefull when you want to make sure, that a specific class fulfills exactly the specification of your organisation and that it stays this way in future releases.

Talking about retention, why were they set to binary retention anyway?

Edit: This @Transient-checking sounds quite usefull, especially when the source-code grows or when scripting is allowed.

qwwdfsad added a commit that referenced this issue Sep 17, 2020
    * SerialInfo, Transient, Required: binary -> runtime
    * Contextual: runtime -> binary

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

No branches or pull requests

3 participants