Skip to content

Commit 958c824

Browse files
committed
Add failing test case for #1322
1 parent 5b5c3c3 commit 958c824

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,15 @@ public static JsonSerializer<Object> getStdKeySerializer(SerializationConfig con
6565
* @since 2.7
6666
*/
6767
public static JsonSerializer<Object> getFallbackKeySerializer(SerializationConfig config,
68-
Class<?> rawKeyType) {
68+
Class<?> rawKeyType)
69+
{
6970
if (rawKeyType != null) {
7071
// 29-Sep-2015, tatu: Odd case here, of `Enum`, which we may get for `EnumMap`; not sure
7172
// if that is a bug or feature. Regardless, it seems to require dynamic handling
7273
// (compared to getting actual fully typed Enum).
7374
// Note that this might even work from the earlier point, but let's play it safe for now
75+
// 11-Aug-2016, tatu: Turns out we get this if `EnumMap` is the root value because
76+
// then there is no static type
7477
if (rawKeyType == Enum.class) {
7578
return new Dynamic();
7679
}

src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeIdTest1288.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
package com.fasterxml.jackson.databind.jsontype.ext;
22

3-
import java.io.IOException;
43
import java.util.UUID;
54

6-
import org.junit.Test;
7-
85
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
96
import com.fasterxml.jackson.annotation.JsonTypeInfo;
107
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
118

12-
import com.fasterxml.jackson.core.JsonParseException;
13-
149
import com.fasterxml.jackson.databind.*;
1510
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
1611
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
1712
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
18-
import com.fasterxml.jackson.databind.jsontype.ext.ExternalTypeIdTest1288.ClassesWithBuilder.PaymentMean;
1913
import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase;
2014

2115
@SuppressWarnings("hiding")

src/test/java/com/fasterxml/jackson/databind/ser/TestEnumSerialization.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private EnumWithJsonValue(String n) {
6161
@JsonValue
6262
public String external() { return "value:"+name; }
6363
}
64-
64+
6565
protected static interface ToStringMixin {
6666
@Override
6767
@JsonValue public String toString();
@@ -215,6 +215,12 @@ private LC749Enum() { }
215215
public String toString() { return name().toLowerCase(); }
216216
}
217217

218+
// for [databind#1322]
219+
protected enum EnumWithJsonProperty {
220+
@JsonProperty("aleph")
221+
A;
222+
}
223+
218224
/*
219225
/**********************************************************
220226
/* Tests
@@ -254,8 +260,7 @@ public void testSubclassedEnums() throws Exception
254260
assertEquals("\"B\"", MAPPER.writeValueAsString(EnumWithSubClass.B));
255261
}
256262

257-
public void testEnumsWithJsonValue() throws Exception
258-
{
263+
public void testEnumsWithJsonValue() throws Exception {
259264
assertEquals("\"value:bar\"", MAPPER.writeValueAsString(EnumWithJsonValue.B));
260265
}
261266

@@ -415,6 +420,18 @@ public void testEnumMapSerEnableToString() throws Exception {
415420
m.put(LC749Enum.A, "value");
416421
assertEquals("{\"a\":\"value\"}", w.writeValueAsString(m));
417422
}
423+
424+
// [databind#1322]
425+
public void testEnumsWithJsonProperty() throws Exception {
426+
assertEquals(quote("aleph"), MAPPER.writeValueAsString(EnumWithJsonProperty.A));
427+
}
428+
429+
// [databind#1322]
430+
public void testEnumsWithJsonPropertyInSet() throws Exception
431+
{
432+
assertEquals("[\"aleph\"]",
433+
MAPPER.writeValueAsString(EnumSet.of(EnumWithJsonProperty.A)));
434+
}
418435
}
419436

420437
// [JACKSON-757], non-inner enum
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.util.EnumMap;
4+
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.databind.BaseMapTest;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
9+
public class EnumSerialization1322Test extends BaseMapTest
10+
{
11+
// for [databind#1322]
12+
protected enum EnumWithJsonProperty {
13+
@JsonProperty("aleph")
14+
A;
15+
}
16+
17+
private final ObjectMapper MAPPER = new ObjectMapper();
18+
19+
// [databind#1322]
20+
public void testEnumsWithJsonPropertyAsKey() throws Exception
21+
{
22+
EnumMap<EnumWithJsonProperty,String> input = new EnumMap<EnumWithJsonProperty,String>(EnumWithJsonProperty.class);
23+
input.put(EnumWithJsonProperty.A, "b");
24+
assertEquals("{\"aleph\":\"b\"}", MAPPER.writeValueAsString(input));
25+
}
26+
}

0 commit comments

Comments
 (0)