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

Deserialized collection fields ignores @Serdeable.Deserializable(as = ...) #635

Closed
oujesky opened this issue Nov 2, 2023 · 0 comments · Fixed by #646
Closed

Deserialized collection fields ignores @Serdeable.Deserializable(as = ...) #635

oujesky opened this issue Nov 2, 2023 · 0 comments · Fixed by #646
Labels
type: improvement A minor improvement to an existing feature
Milestone

Comments

@oujesky
Copy link
Contributor

oujesky commented Nov 2, 2023

Expected Behavior

When @Serdeable.Deserializable(as = X.class) is specified on a deserialized collection/map field, Instance of this type will be used for this field instead of the default one

Same behaviour should be observed with @JsonDeserialize(as = X.class) which is mentioned as supported in the documentation (https://micronaut-projects.github.io/micronaut-serialization/latest/guide/#jacksonAnnotations)

Jackson deserialization respects @JsonDeserialize(as = X.class) and constructs the field with the specified type.

Actual Behaviour

Default collection type is constructed for given field (i.e. ArrayList<X> for List<X> property, etc.) instead the specified type.

Steps To Reproduce

Example:

@Serdeable.Deserializable
record RecordWithList(
    @Serdeable.Deserializable(as = LinkedList.class) List<Integer> list
) {}

...

var result = objectMapper.readValue("{\"list\": [1, 2, 3]}"), RecordWithList.class);

result.list() instanceof LinkedList // false
result.list() instanceof ArrayList // true

The problem is captured in the reproducer in: https://github.com/oujesky/micronaut-serde-as-bug/blob/main/src/test/java/com/example/MicronautSerdeAsBugTest.java including comparison with Jackson (which passes).

Both testMicronautObjectMapperWithMicronautAnnotations and testMicronautObjectMapperWithJacksonAnnotations fails with Expecting actual to be an instance of: java.util.LinkedList but was instance of: java.util.ArrayList

Environment Information

  • JDK 17
  • MacOS

Example Application

https://github.com/oujesky/micronaut-serde-as-bug/

Version

4.1.6

@oujesky oujesky changed the title Deserialized collections ignores @Serdeable.Deserializable(as = ...) Deserialized collection fields ignores @Serdeable.Deserializable(as = ...) Nov 2, 2023
@dstepanov dstepanov added this to the 2.3.1 milestone Nov 6, 2023
@dstepanov dstepanov added the type: improvement A minor improvement to an existing feature label Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A minor improvement to an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants