FasterXML / FasterXML/jackson-modules-base

ClassCastException when using XmlAdapter with XmlEnum

未關閉
#256 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
jaxb-annotations
主要語言
Java
星號
180
分支
80
平均合併
3 小時 26 分鐘
30 天內合併 PR
1

描述

I wanted to serialize a fairly complex object structure and came across this ClassCastException.
It looks like the XML Adapter is called too early when the type is an enum.

Below snippet demonstrates the error as a JUnit test

```java

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import org.junit.jupiter.api.Test;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;

import static org.assertj.core.api.Assertions.assertThat;

public class XmlEnumAdapterTest {

private static final XmlMapper MAPPER =
XmlMapper.builder()
.defaultUseWrapper(true)
.addModule(new JaxbAnnotationModule())
.build();

@Test
void serialize() throws Exception {

final Document document = new Document(Code.RED);

String xml = MAPPER.writeValueAsString(document);

assertThat(xml).isEqualTo("RED");
}

@XmlRootElement(
name = "document"
)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
propOrder = {"_code"}
)
public static class Document {

@XmlElement(name = "code")
private final Code _code;

public Document(Code code) { _code = code; }

public Code getCode() {
return _code;
}
}

@XmlEnum
@XmlType(
name = "CodeType"
)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
public enum Code {

@XmlEnumValue("RED")
RED;
}
}

```

The test was executed with jackson-module-jaxb-annotations-2.17.2.jar

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。