micronaut-projects / micronaut-serialization

Build Time Serialization APIs for Micronaut
Apache License 2.0
26 stars 18 forks source link

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

Open LegoRocks opened 1 week ago

LegoRocks commented 1 week ago

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,
    )
  2. 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

Example Application

No response

Version

4.6.3

dstepanov commented 1 week ago

You cannot reintrospect already introspected class

LegoRocks commented 1 week ago

But then why does it work if you add @SerdeImport to class that is part of the source set like the following?

@Singleton
@SerdeImport(Foo::class)
class FooService
dstepanov commented 1 week ago

MappedEntity adds introspected annotation