FasterXML / FasterXML/jackson-databind

`@JsonDeserialize` on a super class pointing to a sibling class makes Jackson deserialize using that sibling class

未关闭
#3,145 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
3.x
主要语言
Java
星标
3.7k
派生
1.5k
平均合并
3 天 6 小时
30 天内合并 PR
28

描述

This at first sounds like a configuration issue on my end. But then I think Jackson could be a bit more clever here, or in fact in this case maybe it'd be less clever.

Suppose we have an interface with `@JsonDeserialize`:
```java
@JsonDeserialize(as = DataShapeImpl.class)
interface DataShape {
String getSomething();
}
```
and its implementation
```java
class DataShapeImpl implements DataShape { ... }
```

And now if we happen to have another implementation of the same interface:
```java
class ApiDataShape implements DataShape { ... }
```

then when I invoke deserialization pointing Jackson to the second implementation:
```java
ApiDataShape shape = objectMapper
.readerFor(ApiDataShape.class)
.readValue("{\"something\": \"foo\"}");
```

I'll get an exception saying `Failed to narrow type ApiDataShape with annotation (value DataShapeImpl)`. Meaning that Jackson decides to deserialize the given JSON to `DataShapeImpl` because it follows the `JsonDeserialize` annotation on the super-type, even though I asked it for `ApiDataShape` instance and those classes are not compatible with each other.

**Version information**
2.13 / master

**Expected behavior**
What I'd propose is that when Jackson reads the `JsonDeserialize(as = ...)` annotation on a super-type, that it tries to be more lenient with regards to the `JsonDeserialize` annotation on a super-type. There's actually a [comment in the code](https://github.com/FasterXML/jackson-databind/blob/2.13/src/main/java/com/fasterxml/jackson/databind/deser/DeserializerCache.java#L531) that leads to that exception, about whether Jackson should handle this a bit differently.
I think if `refineDeserializationType()` behaved differently when called from `modifyTypeByAnnotation()`, as the comment suggests, maybe that would work more in line with what is expected. Specifically, if `modifyTypeByAnnotation()` wouldn't fail like that and ignored the `JsonDeserialize(as = ...)` if types are not compatible.

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。