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

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

Open
LegoRocks opened this issue Nov 26, 2024 · 3 comments
Open

Comments

@LegoRocks
Copy link

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

@dstepanov
Copy link
Contributor

You cannot reintrospect already introspected class

@LegoRocks
Copy link
Author

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
Copy link
Contributor

MappedEntity adds introspected annotation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants