Skip to content

Deserialization of "empty" subtype with DEDUCTION failed #3139

@xJoeWoo

Description

@xJoeWoo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions