External/Contextual serializers are not supported
See original GitHub issueDescription
External and Contextual serializers registered via SerialModule
are ignored at the moment.
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:
- Created 3 years ago
- Comments:11 (10 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
Released in 0.7.0