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

Deserialization of "empty" subtype with DEDUCTION failed #3139

Closed
xJoeWoo opened this issue May 1, 2021 · 1 comment
Closed

Deserialization of "empty" subtype with DEDUCTION failed #3139

xJoeWoo opened this issue May 1, 2021 · 1 comment
Milestone

Comments

@xJoeWoo
Copy link

xJoeWoo commented May 1, 2021

Describe the bug
Using @JsonTypeInfo(use = DEDUCTION) to deserialize a subclass with no properties/fields causing exception.

Sometimes an empty class will be used to identifying different business types:

public class StartGame {
    public PaymentType paymentType;
}

@JsonTypeInfo(use = DEDUCTION)
@JsonSubTypes({@Type(Postpaid.class), @Type(Prepaid.class)})
public interface PaymentType {
}

public class Postpaid implements PaymentType {
}

public class Prepaid implements PaymentType {
    public int billId;
}

Version information
2.12.3

To Reproduce

@JsonTypeInfo(use = DEDUCTION)
@JsonSubTypes( {@Type(Empty.class), @Type(NonEmpty.class)})
interface Base {
}

static class Empty implements Base {
}

static class NonEmpty implements Base{
  public int i;
}


public static void main(String[] args) {
  String json = "{\"i\":1}";
  Base base = mapper.readValue(json, Base.class);

  json = "{}";

  // com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class Base]: Cannot deduce unique subtype of `Base` (2 candidates match)
  base = mapper.readValue(json, Base.class); 
}

Expected behavior
Deserializing to Empty class in the example above.

@xJoeWoo xJoeWoo added the to-evaluate Issue that has been received but not yet evaluated label May 1, 2021
@cowtowncoder
Copy link
Member

Sounds reasonable, as a special case (in general, cannot use absence of properties, but "none" is different from "missing 'x'").

@cowtowncoder cowtowncoder removed the to-evaluate Issue that has been received but not yet evaluated label May 2, 2021
@cowtowncoder cowtowncoder changed the title Deserialization sub empty class with DEDUCTION failed Deserialization of "empty" subtype with DEDUCTIONfailed May 3, 2021
@cowtowncoder cowtowncoder changed the title Deserialization of "empty" subtype with DEDUCTIONfailed Deserialization of "empty" subtype with DEDUCTION failed May 3, 2021
@cowtowncoder cowtowncoder added this to the 2.12.4 milestone May 3, 2021
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