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

@JsonUnwrapped cannot serialize subtypes #263

Closed
yawkat opened this issue Sep 5, 2022 · 0 comments · Fixed by #624
Closed

@JsonUnwrapped cannot serialize subtypes #263

yawkat opened this issue Sep 5, 2022 · 0 comments · Fixed by #624
Assignees

Comments

@yawkat
Copy link
Member

yawkat commented Sep 5, 2022

Feature description

In cases where serde normally serializes using the dynamic type of a field (e.g. when the class is abstract), @JsonUnwrapped should also use the dynamic type.

This is required for micronaut-projects/micronaut-problem-json#196 .

Failing test case:

    void "test @JsonUnwrapped - subtyping"() {
        given:
        def context = buildContext("""
package unwrapped;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.serde.annotation.Serdeable;

@Serdeable
@Introspected(accessKind = Introspected.AccessKind.FIELD)
class Wrapper {
  @JsonUnwrapped
  public final SuperClass name;

  Wrapper(@JsonUnwrapped SuperClass name) {
      this.name = name;
  }
}

@Serdeable
@Introspected(accessKind = Introspected.AccessKind.FIELD)
abstract class SuperClass {
  public final String first;
  SuperClass(String first) {
      this.first = first;
  }
}

@Serdeable
@Introspected(accessKind = Introspected.AccessKind.FIELD)
class SubClass extends SuperClass {
  public final String last;
  SubClass(String first, String last) {
      super(first);
      this.last = last;
  }
}
""")

        when:
        def name = newInstance(context, 'unwrapped.SubClass', "Fred", "Flinstone")
        def wrapper = newInstance(context, 'unwrapped.Wrapper', name)

        def result = writeJson(jsonMapper, wrapper)

        then:
        result == '{"first":"Fred","last":"Flinstone"}'

        cleanup:
        context.close()
    }
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.

3 participants