• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.failing;
2 
3 import java.util.List;
4 
5 import com.fasterxml.jackson.annotation.JsonProperty;
6 import com.fasterxml.jackson.core.type.TypeReference;
7 import com.fasterxml.jackson.databind.*;
8 
9 /**
10  * NOTE: not assumed to be actual bug -- real numbers are not coerced into
11  * Strings, and are instead assumed to always mean index numbers.
12  * But test retained in case there might be ways to improve support
13  * here: as is, one MUST use Creator method to resolve from number to
14  * enum.
15  */
16 public class EnumDeserialization1626Test extends BaseMapTest
17 {
18     static class JsonResponseEnvelope<T> {
19         @JsonProperty("d")
20         public T data;
21     }
22 
23     static class ShippingMethodInfo {
24         @JsonProperty("typeId")
25         public int typeId;
26 
27         @JsonProperty("value")
28         private ShippingMethods value;
29 
30         @JsonProperty("coverage")
31         public int coverage;
32     }
33 
34     enum ShippingMethods {
35         @JsonProperty("0")
36         SHIPPING_METHODS_UNSPECIFIED(0),
37 
38         @JsonProperty("10")
39         SHIPPING_METHODS_FED_EX_PRIORITY_OVERNIGHT(10),
40 
41         @JsonProperty("17")
42         SHIPPING_METHODS_FED_EX_1DAY_FREIGHT(17),
43         ;
44 
45         private final int shippingMethodId;
46 
ShippingMethods(final int shippingMethodId)47         ShippingMethods(final int shippingMethodId) {
48             this.shippingMethodId = shippingMethodId;
49         }
50 
getShippingMethodId()51         public int getShippingMethodId() {
52             return shippingMethodId;
53         }
54     }
55 
56     /*
57     /**********************************************************
58     /* Test methods
59     /**********************************************************
60      */
61 
62     protected final ObjectMapper MAPPER = new ObjectMapper();
63 
64     // [databind#1626]
testSparseNumericEnum626()65     public void testSparseNumericEnum626() throws Exception
66     {
67         String jsonResponse =
68                 "{\n" +
69                         "    \"d\": [\n" +
70                         "        {\n" +
71                         "            \"typeId\": 0,\n" +
72                         // NOTE! Only real number fails; quoted-as-String is bound as expected
73                         "            \"value\": 17,\n" +
74                         "            \"coverage\": 1"+
75                         "        }\n" +
76                         "     ]\n" +
77                         "}";
78 
79         JsonResponseEnvelope<List<ShippingMethodInfo>> mappedResponse =
80                 MAPPER.readValue(jsonResponse,
81                         new TypeReference<JsonResponseEnvelope<List<ShippingMethodInfo>>>() { });
82         List<ShippingMethodInfo> shippingMethods = mappedResponse.data;
83 
84         assertEquals(ShippingMethods.SHIPPING_METHODS_FED_EX_1DAY_FREIGHT,
85                 shippingMethods.get(0).value);
86     }
87 }
88