1 package com.fasterxml.jackson.databind.deser.creators; 2 3 import java.beans.ConstructorProperties; 4 5 import com.fasterxml.jackson.annotation.*; 6 import com.fasterxml.jackson.databind.*; 7 8 public class CreatorPropertiesTest extends BaseMapTest 9 { 10 static class Issue905Bean { 11 // 08-Nov-2015, tatu: Note that in real code we would most likely use same 12 // names for properties; but here we use different name on purpose to 13 // ensure that Jackson has no way of binding JSON properties "x" and "y" 14 // using any other mechanism than via `@ConstructorProperties` annotation 15 public int _x, _y; 16 17 @ConstructorProperties({"x", "y"}) 18 // Same as above; use differing local parameter names so that parameter name 19 // introspection cannot be used as the source of property names. Issue905Bean(int a, int b)20 public Issue905Bean(int a, int b) { 21 _x = a; 22 _y = b; 23 } 24 } 25 26 // for [databind#1122] 27 static class Ambiguity { 28 @JsonProperty("bar") 29 private int foo; 30 Ambiguity()31 protected Ambiguity() {} 32 33 @ConstructorProperties({ "foo" }) Ambiguity(int foo)34 public Ambiguity(int foo) { 35 this.foo = foo; 36 } 37 getFoo()38 public int getFoo() { 39 return foo; 40 } 41 42 @Override toString()43 public String toString() { 44 return "Ambiguity [foo=" + foo + "]"; 45 } 46 } 47 48 // for [databind#1371] 49 static class Lombok1371Bean { 50 public int x, y; 51 Lombok1371Bean()52 protected Lombok1371Bean() { } 53 54 @ConstructorProperties({ "x", "y" }) Lombok1371Bean(int _x, int _y)55 public Lombok1371Bean(int _x, int _y) { 56 x = _x + 1; 57 y = _y + 1; 58 } 59 } 60 61 /* 62 /********************************************************** 63 /* Test methods 64 /********************************************************** 65 */ 66 67 private final ObjectMapper MAPPER = new ObjectMapper(); 68 69 // [databind#905] testCreatorPropertiesAnnotation()70 public void testCreatorPropertiesAnnotation() throws Exception 71 { 72 Issue905Bean b = MAPPER.readValue(aposToQuotes("{'y':3,'x':2}"), 73 Issue905Bean.class); 74 assertEquals(2, b._x); 75 assertEquals(3, b._y); 76 } 77 78 // [databind#1122] testPossibleNamingConflict()79 public void testPossibleNamingConflict() throws Exception 80 { 81 String json = "{\"bar\":3}"; 82 Ambiguity amb = MAPPER.readValue(json, Ambiguity.class); 83 assertNotNull(amb); 84 assertEquals(3, amb.getFoo()); 85 } 86 87 // [databind#1371]: MapperFeature.INFER_CREATOR_FROM_CONSTRUCTOR_PROPERTIES testConstructorPropertiesInference()88 public void testConstructorPropertiesInference() throws Exception 89 { 90 final String JSON = aposToQuotes("{'x':3,'y':5}"); 91 92 // by default, should detect and use arguments-taking constructor as creator 93 assertTrue(MAPPER.isEnabled(MapperFeature.INFER_CREATOR_FROM_CONSTRUCTOR_PROPERTIES)); 94 Lombok1371Bean result = MAPPER.readValue(JSON, Lombok1371Bean.class); 95 assertEquals(4, result.x); 96 assertEquals(6, result.y); 97 98 // but change if configuration changed 99 ObjectMapper mapper = jsonMapperBuilder() 100 .disable(MapperFeature.INFER_CREATOR_FROM_CONSTRUCTOR_PROPERTIES) 101 .build(); 102 // in which case fields are set directly: 103 result = mapper.readValue(JSON, Lombok1371Bean.class); 104 assertEquals(3, result.x); 105 assertEquals(5, result.y); 106 } 107 } 108