|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import java.beans.ConstructorProperties; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 6 | +import com.fasterxml.jackson.databind.*; |
| 7 | + |
| 8 | +public class CreatorProperties1122Test 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 can not be used as the source of property names. |
| 20 | + public Issue905Bean(int a, int b) { |
| 21 | + _x = a; |
| 22 | + _y = b; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | +// @JsonIgnoreProperties(ignoreUnknown = true) |
| 27 | + public class Ambiguity { |
| 28 | + |
| 29 | + @JsonProperty("bar") |
| 30 | + private int foo; |
| 31 | + |
| 32 | + protected Ambiguity() {} |
| 33 | + |
| 34 | + @ConstructorProperties({ "foo" }) |
| 35 | + public Ambiguity(int foo) { |
| 36 | + this.foo = foo; |
| 37 | + } |
| 38 | + |
| 39 | + public int getFoo() { |
| 40 | + return foo; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String toString() { |
| 45 | + return "Ambiguity [foo=" + foo + "]"; |
| 46 | + } |
| 47 | + |
| 48 | + } |
| 49 | + /* |
| 50 | + /********************************************************** |
| 51 | + /* Test methods |
| 52 | + /********************************************************** |
| 53 | + */ |
| 54 | + |
| 55 | + private final ObjectMapper MAPPER = new ObjectMapper(); |
| 56 | + |
| 57 | + // [databind#905] |
| 58 | + public void testCreatorPropertiesAnnotation() throws Exception |
| 59 | + { |
| 60 | + Issue905Bean b = MAPPER.readValue(aposToQuotes("{'y':3,'x':2}"), |
| 61 | + Issue905Bean.class); |
| 62 | + assertEquals(2, b._x); |
| 63 | + assertEquals(3, b._y); |
| 64 | + } |
| 65 | + |
| 66 | + public void testIssue1122() throws Exception |
| 67 | + { |
| 68 | + String json = "{\"bar\":3}"; |
| 69 | + Ambiguity amb = MAPPER.readValue(json, Ambiguity.class); |
| 70 | + assertNotNull(amb); |
| 71 | + assertEquals(3, amb.getFoo()); |
| 72 | + } |
| 73 | +} |
0 commit comments