question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

External/Contextual serializers are not supported

See original GitHub issue

Description

External and Contextual serializers registered via SerialModule are ignored at the moment.

(https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/custom_serializers.md#registering-and-context)

Issue

Seems like serializer is obtained using the serializerByTypeToken(type) method, which is not aware of registered serial modules. https://github.com/JakeWharton/retrofit2-kotlinx-serialization-converter/blob/d7d74722e8e5adf05f716d2d6e524ee34ce611f1/src/main/java/com/jakewharton/retrofit2/converter/kotlinx/serialization/Factory.kt#L23

Not sure if it’s feasible to fix it in a reasonably clean manner without breaking changes but as quick check this line makes the test pass:

    val loader = (serializer as FromString).format.context.getContextualOrDefault((type as Class<*>).kotlin)

To Reproduce

Failing Test (following existing deserialize() test):


    data class User(val name: String)

    object UserSerializer : KSerializer<User> {
        override val descriptor = PrimitiveDescriptor("User", STRING)

        override fun deserialize(decoder: Decoder): User =
                UserResponse.serializer().deserialize(decoder).run {
                    User(name = name)
                }

        override fun serialize(encoder: Encoder, value: User): Unit = TODO()

        @Serializable
        private data class UserResponse(val name: String)
    }

    @Before
    fun setUp() {
        val contentType = MediaType.get("application/json; charset=utf-8")
        val retrofit = Retrofit.Builder()
                .baseUrl(server.url("/"))
                .addConverterFactory(Json {
                    serialModule = serializersModuleOf(User::class, UserSerializer)
                }.asConverterFactory(contentType))
                .build()
        service = retrofit.create(Service::class.java)
    }

    @Test
    fun deserialize() {
        server.enqueue(MockResponse().setBody("""{"name":"Bob"}"""))
        val user = service.deserialize().execute().body()!!
        assertEquals(User("Bob"), user)
    }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
sgarfinkelcommented, Aug 19, 2020

Good question. I should have some time to look into the changes to make sure this feature can be implemented. If so I’ll try and get a PR pushed.

0reactions
JakeWhartoncommented, Sep 1, 2020

Released in 0.7.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pass request context to serializer from Viewset in Django Rest ...
I have seen how to add the user to the context when initializing a serializer, but I am not sure how to do...
Read more >
Third-party sealed classes serialization with `kotlinx ... - Medium
All subclasses of a sealed class must be explicitly marked as @Serializable. This is unfortunate because it is impossible to annotate 3rd party ......
Read more >
serializer - GitHub Pages
Retrieves serializer for the given reflective Java type using reflective construction and contextual lookup for non-serializable types.
Read more >
Serializer relations - Django REST framework
Note: The relational fields are declared in relations.py , but by convention you should import them from the serializers module, using from rest_framework ......
Read more >
How to Use the Serializer (Symfony Docs)
Serializer Context. The serializer can define a context to control the (de)serialization of resources. This context is passed to all normalizers. For example:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found