Skip to content

Annotating test class with SerdeImport does not work for MappedEntity #980

Open
@LegoRocks

Description

@LegoRocks

Expected Behavior

Using the @SerdeImport on a test class should enable the serialization of a class annotated with @MappedEntity the same way as if the entity was annotated with @Serdable.

Actual Behaviour

@SerdeImport has seemingly no effect and trying to serialize the entity during a test causes an IntrospectionException.

This only affects tests. @SerdeImport works as expected when used in the actual application code.

Caused by: io.micronaut.core.beans.exceptions.IntrospectionException: No serializable introspection present for type Foo. Consider adding Serdeable. Serializable annotate to type Foo. Alternatively if you are not in control of the project's source code, you can use @SerdeImport(Foo.class) to enable serialization of this type.
        at io.micronaut.serde.support.DefaultSerdeIntrospections.getSerializableIntrospection(DefaultSerdeIntrospections.java:111)
        at io.micronaut.serde.support.serializers.SerBean.<init>(SerBean.java:117)
        at io.micronaut.serde.support.serializers.ObjectSerializer.lambda$getSerializableBean$0(ObjectSerializer.java:165)
        at io.micronaut.core.util.SupplierUtil$2.get(SupplierUtil.java:79)
        at io.micronaut.serde.support.serializers.ObjectSerializer.getSerializableBean(ObjectSerializer.java:170)
        at io.micronaut.serde.support.serializers.ObjectSerializer.createSpecificInternal(ObjectSerializer.java:107)
        at io.micronaut.serde.support.serializers.ObjectSerializer.createSpecific(ObjectSerializer.java:99)
        at io.micronaut.serde.jackson.JacksonJsonMapper.writeValue(JacksonJsonMapper.java:227)

Steps To Reproduce

  1. Create class annotated with @MappedEntity in the applications source main folder
@MappedEntity
data class Foo(
    val id: Long,
    val name: String,
)
  1. Run test that using @SerdeImport for the created class and attempt to serialize it.
@MicronautTest
@SerdeImport(Foo::class)
class FooTest(
    private val jsonMapper: JsonMapper,
) : ShouldSpec({

    should("be able to serialize") {
        shouldNotThrow<SerdeException> {
            jsonMapper.writeValueAsString(Foo(1, "bar"))
        }
    }
})

When either removing @MappedEntity or adding @Serdable the test runs successful.

Environment Information

  • Kotlin Version: 1.9.25
  • Java Version: 21
  • Kotest Version: 5

Example Application

No response

Version

4.6.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions