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

JsonSubTypes annotated class not serialized properly when no property using Micronaut Serdes #579

Closed
loicgreffier opened this issue Sep 24, 2023 · 1 comment · Fixed by #622

Comments

@loicgreffier
Copy link

loicgreffier commented Sep 24, 2023

Expected Behavior

Using Micronaut serdes, a class with no property but annotated with @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "validation-type") and @JsonSubTypes should contain the JsonType property validation-type when serialized.

Actual Behaviour

When serializing a class that has no property but a property defined by @JsonTypeInfo jackson annotation:

E.g.:

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        property = "validation-type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = NonEmptyString.class, name = "NonEmptyString"),
})
public interface Validator { }

@Data
@Serdeable
@NoArgsConstructor
public static class NonEmptyString implements ResourceValidator.Validator { }

I was expecting the serialization output to contain the property validation-type, but it does contain any field at all, using the Micronaut serdes.

The same implementation is working with the jackson-databind feature.

Using micronaut-serde-jackson

13:46:55.987 [Test worker] INFO  com.example.DemoTest - Result: Optional[{validator={}}]

The output does not contain the property validation-type.

Using micronaut-jackson-databind

13:46:55.987 [Test worker] INFO  com.example.DemoTest - Result: Optional[{validator={validation-type=NonEmptyString}}]

The output contains the property validation-type as expected.

Additional Info

When adding any field to the class, it is serialized as expected using Micronaut serdes:

13:51:04.925 [Test worker] INFO  com.example.DemoTest - Result: Optional[{validator={validation-type=NonEmptyStringWithField, anyField=true}}]

The output contains the property validation-type as expected.

Steps To Reproduce

  1. Clone https://github.com/loicgreffier/issue-micronaut-serdes
  2. Checkout the branch subtypes-nofield and run the unit test. It runs with implementation("io.micronaut.serde:micronaut-serde-jackson").
  3. See the output logs
  4. Checkout the branch subtypes-nofield-jackson and run the unit test. It runes with implementation("io.micronaut:micronaut-jackson-databind").
  5. See the output logs

Environment Information

  • JDK Eclipse Temurin v17.0.2

Example Application

https://github.com/loicgreffier/issue-micronaut-serdes

Version

4.1.0

@loicgreffier
Copy link
Author

Investigated a little bit more...

Based on the fact that it works for a class with attributes (e.g., Range in my example), but does not for a class without attributes (e.g., NonEmptyString, I could find, from my example, that:

  • Range child class is serialized by a CustomObjectSerializer, which is able to get all the attribute, including @JsonTypeInfo
  • NonEmptyString child class is serialized by a SimpleObjectSerializer, which is not able to

I could see this in the CustomizedObjectSerializer class itself: https://github.com/micronaut-projects/micronaut-serialization/blob/master/serde-support/src/main/java/io/micronaut/serde/support/serializers/CustomizedObjectSerializer.java#L81.

Going deeper, I could see that when selecting the appropriate serializer for child attributes, it drives me to this condition:

which precisely compute the attribute of @JsonTypeInfo. Unfortunately, it requires properties not to be empty to be honored.. which is not the case of my NonEmptyString child class that has no properties.

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

Successfully merging a pull request may close this issue.

1 participant