FasterXML / jackson-databind

General data-binding package for Jackson (2.x): works on streaming API (core) implementation(s)
Apache License 2.0
3.51k stars 1.37k forks source link

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

Open wesleyking0 opened 4 weeks ago

wesleyking0 commented 4 weeks ago

Search before asking

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

cowtowncoder commented 3 weeks ago

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.

wesleyking0 commented 1 week ago

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.

cowtowncoder commented 1 week ago

@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.