|
| 1 | +package com.fasterxml.jackson.databind.tofix; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 6 | +import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.MapperFeature; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 11 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 12 | +import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | + |
| 16 | +// [databind#5292] Need support for creators `MapperFeature.FIX_FIELD_NAME_UPPER_CASE_PREFIX` |
| 17 | +public class FixFieldNameUpperCasePrefix5292Test |
| 18 | + extends DatabindTestUtil |
| 19 | +{ |
| 20 | + static class AppleSingleNonTarget { |
| 21 | + private final String name; |
| 22 | + |
| 23 | + public AppleSingleNonTarget(@ImplicitName("name") String name) { |
| 24 | + this.name = name; |
| 25 | + } |
| 26 | + |
| 27 | + public String getName() { |
| 28 | + return name; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + static class AppleSingleIsTarget { |
| 33 | + private final String iPhone; |
| 34 | + |
| 35 | + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) |
| 36 | + public AppleSingleIsTarget(@ImplicitName("iPhone") String iPhone) { |
| 37 | + this.iPhone = iPhone; |
| 38 | + } |
| 39 | + |
| 40 | + public String getIPhone() { |
| 41 | + return iPhone; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Creator order should be used but just in case, define explicit order |
| 46 | + @JsonPropertyOrder({ "iPhone", "name" }) |
| 47 | + static class AppleDouble { |
| 48 | + private final String iPhone; |
| 49 | + private final String name; |
| 50 | + |
| 51 | + public AppleDouble(@ImplicitName("iPhone") String iPhone, |
| 52 | + @ImplicitName("name") String name) { |
| 53 | + this.iPhone = iPhone; |
| 54 | + this.name = name; |
| 55 | + } |
| 56 | + |
| 57 | + public String getIPhone() { |
| 58 | + return iPhone; |
| 59 | + } |
| 60 | + |
| 61 | + public String getName() { |
| 62 | + return name; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private final ObjectMapper MAPPER = JsonMapper.builder() |
| 67 | + .annotationIntrospector(new ImplicitNameIntrospector()) |
| 68 | + .enable(MapperFeature.FIX_FIELD_NAME_UPPER_CASE_PREFIX) |
| 69 | + .build(); |
| 70 | + |
| 71 | + @JacksonTestFailureExpected |
| 72 | + @Test |
| 73 | + public void testDeserDouble() throws Exception |
| 74 | + { |
| 75 | + AppleDouble apple = new AppleDouble("iPhone 15", "Jay"); |
| 76 | + String json = MAPPER.writeValueAsString(apple); |
| 77 | + assertEquals("{\"iPhone\":\"iPhone 15\",\"name\":\"Jay\"}", json); |
| 78 | + |
| 79 | + AppleDouble result = MAPPER.readValue(json, AppleDouble.class); // Error thrown |
| 80 | + |
| 81 | + assertEquals("Jay", result.getName()); |
| 82 | + assertEquals("iPhone 15", result.getName()); |
| 83 | + } |
| 84 | + |
| 85 | + @JacksonTestFailureExpected |
| 86 | + @Test |
| 87 | + public void testSingleArgCase() throws Exception |
| 88 | + { |
| 89 | + AppleSingleIsTarget apple = new AppleSingleIsTarget("iPhone 15"); |
| 90 | + String json = MAPPER.writeValueAsString(apple); |
| 91 | + assertEquals("{\"iPhone\":\"iPhone 15\"}", json); |
| 92 | + |
| 93 | + AppleSingleIsTarget result = MAPPER.readValue(json, AppleSingleIsTarget.class); // Error thrown |
| 94 | + assertEquals("iPhone 15", result.getIPhone()); |
| 95 | + } |
| 96 | + |
| 97 | + // Just for comparison |
| 98 | + @Test |
| 99 | + public void testHappyCaseSingleArgString() throws Exception |
| 100 | + { |
| 101 | + AppleSingleNonTarget apple = new AppleSingleNonTarget("Jay"); |
| 102 | + String json = MAPPER.writeValueAsString(apple); |
| 103 | + assertEquals("{\"name\":\"Jay\"}", json); |
| 104 | + |
| 105 | + AppleSingleNonTarget result = MAPPER.readValue(json, AppleSingleNonTarget.class); // Error thrown |
| 106 | + assertEquals("Jay", result.getName()); |
| 107 | + } |
| 108 | +} |
0 commit comments