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

@JsonClassDiscriminator and other serial info annotations in sealed, polymorphic and object serializables #1515

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
package kotlinx.serialization

import kotlinx.serialization.descriptors.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.*

class SerialDescriptorAnnotationsTest {

Expand All @@ -17,7 +16,33 @@ class SerialDescriptorAnnotationsTest {
@Serializable
@SerialName("MyClass")
@CustomAnnotation("onClass")
data class WithNames(val a: Int, @CustomAnnotation("onProperty") val veryLongName: String)
data class WithNames(
val a: Int,
@CustomAnnotationWithDefault @CustomAnnotation("onProperty") val veryLongName: String
)

@SerialInfo
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
annotation class CustomAnnotationWithDefault(val value: String = "foo")

@SerialInfo
@Target(AnnotationTarget.PROPERTY)
public annotation class JShort(val order: SByteOrder = SByteOrder.BE, val mod: SByteMod = SByteMod.Add)

public enum class SByteOrder {
BE, LE
}

public enum class SByteMod {
None, Add
}

@Serializable
public class Foo(
@JShort(SByteOrder.LE, SByteMod.None) public val bar: Short,
@JShort public val baz: Short
)


@Test
fun testSerialNameOnClass() {
Expand All @@ -40,5 +65,20 @@ class SerialDescriptorAnnotationsTest {
assertEquals("onClass", name)
}

@Test
fun testCustomAnnotationWithDefaultValue() {
val value =
WithNames.serializer().descriptor
.getElementAnnotations(1).filterIsInstance<CustomAnnotationWithDefault>().single()
assertEquals("foo", value.value)
sandwwraith marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
fun testAnnotationWithMultipleArgs() {
fun SerialDescriptor.getValues(i: Int) = getElementAnnotations(i).filterIsInstance<JShort>().single().run { order to mod }
assertEquals(SByteOrder.LE to SByteMod.None, Foo.serializer().descriptor.getValues(0))
assertEquals(SByteOrder.BE to SByteMod.Add, Foo.serializer().descriptor.getValues(1))
}

private fun List<Annotation>.getCustom() = filterIsInstance<CustomAnnotation>().single().value
}