FasterXML / FasterXML/jackson-modules-base

ClassCastException when using XmlAdapter with XmlEnum

Offen
#256 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
jaxb-annotations
Vorherrschende Sprache
Java
Sterne
180
Forks
80
Ø Merge
3 Std. 26 Min.
Gemergte PRs (30 T.)
1

Beschreibung

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

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.