FasterXML / FasterXML/jackson-modules-base

Add support for JAXB's `@XmlSeeAlso` (maybe similar to `@JsonSubTypes`) to improve JAXB compatibility

Đang mở
#195 5 bình luận 0 reaction 0 người được giao Xem trên GitHub
jaxb-annotations
Ngôn ngữ chính
Java
Star
180
Fork
80
Merge trung bình
3 giờ 26 phút
Pull request đã merge (30 ngày)
1

Mô tả

## Problem description

Jackson's XML annotation handler does not currently process the `XmlSeeAlso` of the JAXB specification. As a result, it is always possible to serialize objects based on JAXB, but it is not possible to deserialize them in all cases. This problem occurs for example if JAXB objects are created by the XJC tool from XSD files that include subtyping.

For example, the following class structure represents a valid JAXB object representation:
```java
class Root {
Base element;
}

@XmlSeeAlso({First.class, Second.class})
abstract class Base { }

@XmlType(name = "FirstType")
class First extends Base { }

@XmlType(name = "SecondType")
class Second extends Base { }
```
If `base` is set to `First`, JAXB will declare a property in the XML-instance namespace which allows to serialize and to deserialize any input as the type information is retained.
```xml

```
Currently, Jackson discards the type information and neither processes it by resolving against `XmlSeeAlso` if it was available. If the same object was marshalled to JSON, it would result in the following JSON:
```json
{"element": {}}
```
which cannot be marshalled to an object and where the necessary information to unmarshall this object is neither retained.

## Suggested solution

Jackson should introduce a pseudo-attribute to retain type information to support the same form of type discovery. The created JSON should add the JAXB-type within an annotation as in:
```json
{"element": {"@type": "FirstType"}}
```
where the client should process this information when unmarshalling to recover the instance type.

I tried an [implementation](https://github.com/raphw/jacksom-xml-see-also) of such support, without knowing if this is the most efficient implementation. Also, I discovered that my solution created a `null` instance for an empty `element` object as in the example what I [worked around](https://github.com/raphw/jacksom-xml-see-also/blob/main/src/main/java/org/example/hierarchy/XmlSeeAlsoDeserializer.java#L53). If there is a better solution to that corner-case, I'd appreciate any feedback. I am happy to offer my time to integrate this into the official JAXB support if this is a desired extension.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.