1 package com.fasterxml.jackson.databind.introspect; 2 3 import com.fasterxml.jackson.annotation.*; 4 5 import com.fasterxml.jackson.core.JsonProcessingException; 6 7 import com.fasterxml.jackson.databind.*; 8 9 /** 10 * Unit tests verifying handling of potential and actual 11 * conflicts, regarding property handling. 12 */ 13 public class TestPropertyConflicts extends BaseMapTest 14 { 15 // error message for conflicting getters sub-optimal 16 static class BeanWithConflict 17 { getX()18 public int getX() { return 3; } getx()19 public boolean getx() { return false; } 20 } 21 22 // [databind#238] 23 protected static class Getters1A 24 { 25 @JsonProperty 26 protected int value = 3; 27 getValue()28 public int getValue() { return value+1; } isValue()29 public boolean isValue() { return false; } 30 } 31 32 // variant where order of declarations is reversed; to try to 33 // ensure declaration order won't break things 34 protected static class Getters1B 35 { isValue()36 public boolean isValue() { return false; } 37 38 @JsonProperty 39 protected int value = 3; 40 getValue()41 public int getValue() { return value+1; } 42 } 43 44 protected static class InferingIntrospector extends JacksonAnnotationIntrospector 45 { 46 private static final long serialVersionUID = 1L; 47 48 @Override findImplicitPropertyName(AnnotatedMember member)49 public String findImplicitPropertyName(AnnotatedMember member) { 50 String name = member.getName(); 51 if (name.startsWith("_")) { 52 return name.substring(1); 53 } 54 return null; 55 } 56 } 57 58 static class Infernal { _name()59 public String _name() { return "foo"; } getName()60 public String getName() { return "Bob"; } 61 setStuff(String value)62 public void setStuff(String value) { ; // ok 63 } 64 _stuff(String value)65 public void _stuff(String value) { 66 throw new UnsupportedOperationException(); 67 } 68 } 69 70 // For [databind#541] 71 static class Bean541 { 72 protected String str; 73 74 @JsonCreator Bean541(@sonProperty"str") String str)75 public Bean541(@JsonProperty("str") String str) { 76 this.str = str; 77 } 78 79 @JsonProperty("s") getStr()80 public String getStr() { 81 return str; 82 } 83 } 84 85 /* 86 /********************************************************** 87 /* Test methods 88 /********************************************************** 89 */ 90 testFailWithDupProps()91 public void testFailWithDupProps() throws Exception 92 { 93 BeanWithConflict bean = new BeanWithConflict(); 94 try { 95 String json = objectWriter().writeValueAsString(bean); 96 fail("Should have failed due to conflicting accessor definitions; got JSON = "+json); 97 } catch (JsonProcessingException e) { 98 verifyException(e, "Conflicting getter definitions"); 99 } 100 } 101 102 // [databind#238]: ok to have getter, "isGetter" testRegularAndIsGetter()103 public void testRegularAndIsGetter() throws Exception 104 { 105 final ObjectWriter writer = objectWriter(); 106 107 // first, serialize without probs: 108 assertEquals("{\"value\":4}", writer.writeValueAsString(new Getters1A())); 109 assertEquals("{\"value\":4}", writer.writeValueAsString(new Getters1B())); 110 111 // and similarly, deserialize 112 ObjectMapper mapper = objectMapper(); 113 assertEquals(1, mapper.readValue("{\"value\":1}", Getters1A.class).value); 114 assertEquals(2, mapper.readValue("{\"value\":2}", Getters1B.class).value); 115 } 116 testInferredNameConflictsWithGetters()117 public void testInferredNameConflictsWithGetters() throws Exception 118 { 119 ObjectMapper mapper = jsonMapperBuilder() 120 .annotationIntrospector(new InferingIntrospector()) 121 .build(); 122 String json = mapper.writeValueAsString(new Infernal()); 123 assertEquals(aposToQuotes("{'name':'Bob'}"), json); 124 } 125 testInferredNameConflictsWithSetters()126 public void testInferredNameConflictsWithSetters() throws Exception 127 { 128 ObjectMapper mapper = new ObjectMapper(); 129 mapper.setAnnotationIntrospector(new InferingIntrospector()); 130 Infernal inf = mapper.readValue(aposToQuotes("{'stuff':'Bob'}"), Infernal.class); 131 assertNotNull(inf); 132 } 133 testIssue541()134 public void testIssue541() throws Exception { 135 ObjectMapper mapper = jsonMapperBuilder() 136 .disable( 137 MapperFeature.AUTO_DETECT_CREATORS, 138 MapperFeature.AUTO_DETECT_FIELDS, 139 MapperFeature.AUTO_DETECT_GETTERS, 140 MapperFeature.AUTO_DETECT_IS_GETTERS, 141 MapperFeature.AUTO_DETECT_SETTERS, 142 MapperFeature.USE_GETTERS_AS_SETTERS 143 ).build(); 144 Bean541 data = mapper.readValue("{\"str\":\"the string\"}", Bean541.class); 145 if (data == null) { 146 throw new IllegalStateException("data is null"); 147 } 148 if (!"the string".equals(data.getStr())) { 149 throw new IllegalStateException("bad value for data.str"); 150 } 151 } 152 } 153