Skip to content

Commit 643f15a

Browse files
committed
Add test for #682 (passing with jackson-databind fix)
1 parent 5b78983 commit 643f15a

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Project: jackson-dataformat-xml
88

99
#678: XML module not registered correctly when setting a custom `SerializerFactory`
1010
(reported by @SimonCockx)
11+
#682: `MismatchedInputException` encountered while deserializing XML to an Enum type
12+
using a factory method
13+
(reported by @WannabeSoftwareEngineer)
1114

1215
2.18.1 (28-Oct-2024)
1316

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.fasterxml.jackson.dataformat.xml.deser;
2+
3+
import com.fasterxml.jackson.annotation.JsonValue;
4+
import com.fasterxml.jackson.annotation.JsonCreator;
5+
import com.fasterxml.jackson.databind.*;
6+
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
7+
8+
public class EnumDeser682Test extends XmlTestBase
9+
{
10+
static enum Country {
11+
ITALY("Italy"),
12+
NETHERLANDS("Netherlands");
13+
14+
private String value;
15+
16+
Country(String value) {
17+
this.value = value;
18+
}
19+
20+
@JsonValue
21+
public String getValue() {
22+
return value;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return String.valueOf(value);
28+
}
29+
30+
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
31+
public static Country fromValue(String value) {
32+
for (Country b : Country.values()) {
33+
if (b.value.equals(value)) {
34+
return b;
35+
}
36+
}
37+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
38+
}
39+
}
40+
41+
private final ObjectMapper MAPPER = newMapper();
42+
43+
public void testEnumDeser682() throws Exception {
44+
String xml = MAPPER.writeValueAsString(Country.ITALY);
45+
assertEquals("<Country>Italy</Country>", xml);
46+
47+
assertEquals(Country.ITALY, MAPPER.readValue(xml, Country.class));
48+
}
49+
}

0 commit comments

Comments
 (0)