Skip to content

Commit ce11473

Browse files
committed
Fix #1543
1 parent ef17f26 commit ce11473

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

release-notes/CREDITS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,10 @@ Stephan Schroevers (Stephan202@github)
587587
* Reported #1505: @JsonEnumDefaultValue should take precedence over FAIL_ON_NUMBERS_FOR_ENUMS
588588
(2.8.7)
589589

590+
Alex Panchenko (panchenko@github)
591+
* Reported #1543: JsonFormat.Shape.NUMBER_INT does not work when defined on enum type in 2.8
592+
(2.8.8)
593+
590594
Connor Kuhn (ckuhn@github)
591595
* Contributed #1341: FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY
592596
(2.9.0)

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Project: jackson-databind
66
2.8.8 (not yet released)
77

88
#1533: `AsPropertyTypeDeserializer` ignores `DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT`
9+
#1543: JsonFormat.Shape.NUMBER_INT does not work when defined on enum type in 2.8
10+
(reported by Alex P)
911
- Minor fix to creation of `PropertyMetadata`, had one path that could lead to NPE
1012

1113
2.8.7 (21-Feb-2017)

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public EnumSerializer(EnumValues v, Boolean serializeAsIndex)
6767
_values = v;
6868
_serializeAsIndex = serializeAsIndex;
6969
}
70-
70+
7171
/**
7272
* Factory method used by {@link com.fasterxml.jackson.databind.ser.BasicSerializerFactory}
7373
* for constructing serializer instance of Enum types.
@@ -83,7 +83,7 @@ public static EnumSerializer construct(Class<?> enumClass, SerializationConfig c
8383
* handle toString() case dynamically (for example)
8484
*/
8585
EnumValues v = EnumValues.constructFromName(config, (Class<Enum<?>>) enumClass);
86-
Boolean serializeAsIndex = _isShapeWrittenUsingIndex(enumClass, format, true);
86+
Boolean serializeAsIndex = _isShapeWrittenUsingIndex(enumClass, format, true, null);
8787
return new EnumSerializer(v, serializeAsIndex);
8888
}
8989

@@ -100,7 +100,8 @@ public JsonSerializer<?> createContextual(SerializerProvider serializers,
100100
JsonFormat.Value format = findFormatOverrides(serializers,
101101
property, handledType());
102102
if (format != null) {
103-
Boolean serializeAsIndex = _isShapeWrittenUsingIndex(property.getType().getRawClass(), format, false);
103+
Boolean serializeAsIndex = _isShapeWrittenUsingIndex(property.getType().getRawClass(),
104+
format, false, _serializeAsIndex);
104105
if (serializeAsIndex != _serializeAsIndex) {
105106
return new EnumSerializer(_values, serializeAsIndex);
106107
}
@@ -209,18 +210,20 @@ protected final boolean _serializeAsIndex(SerializerProvider serializers)
209210
}
210211

211212
/**
212-
* Helper method called to check whether
213+
* Helper method called to check whether serialization should be done using
214+
* index (number) or not.
213215
*/
214216
protected static Boolean _isShapeWrittenUsingIndex(Class<?> enumClass,
215-
JsonFormat.Value format, boolean fromClass)
217+
JsonFormat.Value format, boolean fromClass,
218+
Boolean defaultValue)
216219
{
217220
JsonFormat.Shape shape = (format == null) ? null : format.getShape();
218221
if (shape == null) {
219-
return null;
222+
return defaultValue;
220223
}
221224
// i.e. "default", check dynamically
222225
if (shape == Shape.ANY || shape == Shape.SCALAR) {
223-
return null;
226+
return defaultValue;
224227
}
225228
// 19-May-2016, tatu: also consider "natural" shape
226229
if (shape == Shape.STRING || shape == Shape.NATURAL) {
@@ -230,9 +233,9 @@ protected static Boolean _isShapeWrittenUsingIndex(Class<?> enumClass,
230233
if (shape.isNumeric() || (shape == Shape.ARRAY)) {
231234
return Boolean.TRUE;
232235
}
233-
throw new IllegalArgumentException("Unsupported serialization shape ("+shape+") for Enum "+enumClass.getName()
234-
+", not supported as "
235-
+ (fromClass? "class" : "property")
236-
+" annotation");
236+
// 07-Mar-2017, tatu: Also means `OBJECT` not available as property annotation...
237+
throw new IllegalArgumentException(String.format(
238+
"Unsupported serialization shape (%s) for Enum %s, not supported as %s annotation",
239+
shape, enumClass.getName(), (fromClass? "class" : "property")));
237240
}
238241
}

src/test/java/com/fasterxml/jackson/databind/struct/EnumFormatShapeTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,13 @@ public void testOverrideEnumAsNumber() throws Exception {
104104
}
105105

106106
// for [databind#1543]
107-
public void testEnumAsNumber() throws Exception {
107+
public void testEnumValueAsNumber() throws Exception {
108108
assertEquals(String.valueOf(Color.GREEN.ordinal()),
109109
MAPPER.writeValueAsString(Color.GREEN));
110-
assertEquals(String.format(aposToQuotes("{'color':'%s'}"), Color.GREEN.ordinal()),
110+
}
111+
112+
public void testEnumPropertyAsNumber() throws Exception {
113+
assertEquals(String.format(aposToQuotes("{'color':%s}"), Color.GREEN.ordinal()),
111114
MAPPER.writeValueAsString(new ColorWrapper(Color.GREEN)));
112115
}
113116
}

0 commit comments

Comments
 (0)