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

Contextual with Element throws Inline classes can not be decoded directly #220

Closed
hfhbd opened this issue Jun 18, 2024 · 10 comments
Closed
Labels
bug Something isn't working indev The issue is fixed/implemented in the dev branch

Comments

@hfhbd
Copy link

hfhbd commented Jun 18, 2024

This did work with 0.86.3:

@Serializable
data class Box(
  @Contextual
  @XmlElement val i: Instant
)

@Test
fun a() {
  val box: Box = XML.defaultInstance.decodeFromString(
  """<?xml version="1.0" encoding="UTF-8"?>
     <Box>
       <i>2023-11-02T15:56:49.364+01:00</i>
     </Box>
  """.trimIndent()
  )
}

With 0.90.1:

In: Box/i Error: 3:21 - Inline classes can not be decoded directly
nl.adaptivity.xmlutil.XmlException: In: Box/i Error: 3:21 - Inline classes can not be decoded directly
	at nl.adaptivity.xmlutil.serialization.XmlDecoderBase$TagDecoderBase.decodeSerializableElement(XMLDecoder.kt:856)
	at SerializationTest$Box$$serializer.deserialize(SerializationTest.kt:14)
@pdvrieze
Copy link
Owner

Contextual has only (very recently) been made to (mostly) work. To diagnose this I need to know the serializer for i. But just from the error message it appears that the serializer attemps to decode an inline class without calling beginStructure. So I suspect you have an incorrect custom serializer.

@hfhbd
Copy link
Author

hfhbd commented Jun 18, 2024

I didn't setup any custom serializer, i is kotlinx.datetime.Instant, version 0.6.0, using JVM target.

@pdvrieze
Copy link
Owner

@hfhbd I notice that kotlinx.datetime.Instant is serializable. This means you shouldn't have/need a Contextual annotation (if you do, it must be registered in the SerializersModule). The error is however not indicative of that issue.

@hfhbd
Copy link
Author

hfhbd commented Jun 18, 2024

Is this expected? I didn't need to register it in SerializerModule with 0.86.3 and I also don't need it with the Json format.

@pdvrieze
Copy link
Owner

@hfhbd I'll have a look, but the whole point of marking it Contextual is that it would be resolved at runtime. 0.86.3 didn't support contextual at all. I suspect that the default implementation falls back to the type-specific serializer, and somehow xmlutil doesn't.

@hfhbd
Copy link
Author

hfhbd commented Jun 19, 2024

Thanks!
Yes, you can mark an attribute as @Contextual to resolve a serializer as runtime, but according to the documentation you don't need to register a serializer for @Serializable types:

If type of the property marked with `@Contextual` is `@Serializable` by itself, the plugin-generated serializer is
 * used as a fallback if no serializers associated with a given type is registered in the module.
 * The fallback serializer is determined by the static type of the property, not by its actual type.

@pdvrieze
Copy link
Owner

I've fixed it. I forgot to handle contextual in the decoding case (it needs to determine the actual descriptor, rather than the "fake" one for the contextual serializer). I guess that it worked in 0.86.3 is more by accident than intent.

@hfhbd
Copy link
Author

hfhbd commented Jun 19, 2024

@pdvrieze Thank you for the fast implementation! Do you publish snapshots? If so, I will try it.

@pdvrieze
Copy link
Owner

Yes, there should be a snapshot that works.

@hfhbd
Copy link
Author

hfhbd commented Jun 19, 2024

Thanks, the snapshot (0.90.2-SNAPSHOT) does work :)

@pdvrieze pdvrieze added bug Something isn't working indev The issue is fixed/implemented in the dev branch labels Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working indev The issue is fixed/implemented in the dev branch
Projects
None yet
Development

No branches or pull requests

2 participants