jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

defining different JsonTypeInfo annotations in both the superclass and the subtype results in ambiguity

Open wesleyking0 opened this issue 1 year ago • 3 comments

Search before asking

  • [X] I searched in the issues and found nothing similar.

Describe the bug

There is my case:

@JsonTypeInfo(use = Id.NAME, property = "animalType")
@JsonSubTypes({
    @Type(value = Humanity.class, name = "mammal")
})
class Animal {
    String animalType;
}

@JsonTypeInfo(use = Id.NAME, property = "mammalType")
@JsonSubTypes({
    @Type(value = Humanity.class, name = "humanity")
})
class Mammal extends Animal {
    String mammalType;
}

class Humanity extends Mammal {
}

I have a JSON string:

{
    "animalType": "mammal",
    "mammalType": "humanity"
}

I wish to return an instance of the Humanity when I parse the JSON string, but with the version 2.17.1 i am using, I am getting an instance of Mammal returned instead.

Version Information

2.17.1

Reproduction

Expected behavior

No response

Additional context

No response

wesleyking0 avatar Aug 17 '24 09:08 wesleyking0

Subtypes should not try to override @JsonTypeInfo: this is not supported functionality. And as such, results of trying to do that are undefined.

So... I don't think there's necessarily bug to fix. Although example is missing piece of code used for writing/reading json.

cowtowncoder avatar Aug 20 '24 19:08 cowtowncoder

Unn... I think it is a new feature. I don't known how much requirement the feature will have, but it is a good enhance to the Jackson.

wesleyking0 avatar Sep 06 '24 11:09 wesleyking0

@wesleyking0 I do not think this is something that should be done, ever. I am ok leaving this open, but currently disagree with the idea fundamentally.

cowtowncoder avatar Sep 06 '24 22:09 cowtowncoder