FasterXML / FasterXML/jackson-dataformat-xml

JAXB `@XmlJavaTypeAdapter` not working in conjunction with `java.util.Optional`

オープン
#588 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Java
スター
631
フォーク
246
平均マージ
7日 9時間
マージ済み PR(30日)
13

説明

`@XmlJavaTypeAdapter` doesn't seem to work in conjunction with JDK's `Optional` class. I have written a unit test in order to replicate this issue:

```java
package org.example;

import static org.example.OptionalWithTypeAdapterTest.TestEnum.A;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationModule;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class OptionalWithTypeAdapterTest {

public enum TestEnum {
A
}

public static class TestEnumAdapter extends XmlAdapter {

@Override
public TestEnum unmarshal(String aString) {
return TestEnum.valueOf(aString.toUpperCase());
}

@Override
public String marshal(TestEnum testEnum) {
return testEnum.name().toLowerCase();
}
}

public record TestObject1(
@XmlElement @XmlJavaTypeAdapter(TestEnumAdapter.class) TestEnum testEnum) {
}

public record TestObject2(
@XmlElement Optional testEnum) {
}

public record TestObject3(
@XmlElement @XmlJavaTypeAdapter(TestEnumAdapter.class) Optional testEnum) {
}

private XmlMapper xmlMapper;

@BeforeEach
public void setUp() {
xmlMapper = new XmlMapper();
xmlMapper.registerModule(new JakartaXmlBindAnnotationModule());
xmlMapper.registerModule(new Jdk8Module());
}

@Test
public void writeValueAsStringWithTypeAdapter() throws JsonProcessingException {
TestObject1 testObject = new TestObject1(A);
String actual = xmlMapper.writeValueAsString(testObject);
String expected = "a";
assertEquals(expected, actual);
}

@Test
public void writeValueAsStringWithOptional() throws JsonProcessingException {
TestObject2 testObject = new TestObject2(Optional.of(A));
String actual = xmlMapper.writeValueAsString(testObject);
String expected = "A";
assertEquals(expected, actual);
}

/**
* This test fails
*/
@Disabled
@Test
public void writeValueAsStringWithTypeAdapterAndOptional() throws JsonProcessingException {
TestObject3 testObject = new TestObject3(Optional.of(A));
String actual = xmlMapper.writeValueAsString(testObject);
String expected = "a";
assertEquals(expected, actual);
}
}
```

This doesn't seem to have anything to do with the fact that I'm using records. I tried the same test with plain classes and the third test still fails. I'm using java 17 and jackson 2.15.0-rc1.

[test.zip](https://github.com/FasterXML/jackson-dataformat-xml/files/11076267/test.zip)

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。