|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.*; |
| 4 | +import com.fasterxml.jackson.databind.*; |
| 5 | +import com.fasterxml.jackson.databind.introspect.AnnotatedMember; |
| 6 | +import com.fasterxml.jackson.databind.introspect.AnnotatedMethod; |
| 7 | +import com.fasterxml.jackson.databind.introspect.AnnotatedParameter; |
| 8 | +import com.fasterxml.jackson.databind.introspect.AnnotatedWithParams; |
| 9 | +import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; |
| 10 | + |
| 11 | +public class DelegatingCreatorImplicitNames1001Test extends BaseMapTest |
| 12 | +{ |
| 13 | + static class D |
| 14 | + { |
| 15 | + private String raw1 = ""; |
| 16 | + private String raw2 = ""; |
| 17 | + |
| 18 | + private D(String raw1, String raw2) { |
| 19 | + this.raw1 = raw1; |
| 20 | + this.raw2 = raw2; |
| 21 | + } |
| 22 | + |
| 23 | + // not needed strictly speaking, but added for good measure |
| 24 | + @JsonCreator(mode=JsonCreator.Mode.DELEGATING) |
| 25 | + public static D make(String value) { |
| 26 | + String[] split = value.split(":"); |
| 27 | + return new D(split[0], split[1]); |
| 28 | + } |
| 29 | + |
| 30 | + @JsonValue |
| 31 | + public String getMyValue() { |
| 32 | + return raw1 + ":" + raw2; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public String toString() { |
| 37 | + return getMyValue(); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean equals(Object o) { |
| 42 | + D other = (D) o; |
| 43 | + return other.raw1.equals(raw1) |
| 44 | + && other.raw2.equals(raw2); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // To test equivalent of parameter-names, let's use this one |
| 49 | + protected static class CreatorNameIntrospector extends JacksonAnnotationIntrospector |
| 50 | + { |
| 51 | + private static final long serialVersionUID = 1L; |
| 52 | + |
| 53 | + @Override |
| 54 | + public String findImplicitPropertyName(AnnotatedMember member) { |
| 55 | + if (member instanceof AnnotatedParameter) { |
| 56 | + AnnotatedParameter p = (AnnotatedParameter) member; |
| 57 | + AnnotatedWithParams owner = p.getOwner(); |
| 58 | + if (owner instanceof AnnotatedMethod) { |
| 59 | + if (p.getIndex() == 0) { |
| 60 | + return "value"; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + return super.findImplicitPropertyName(member); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Baseline test to show how things should work |
| 69 | + public void testWithoutNamedParameters() throws Exception |
| 70 | + { |
| 71 | + ObjectMapper sut = new ObjectMapper(); |
| 72 | + |
| 73 | + D d = D.make("abc:def"); |
| 74 | + |
| 75 | + String actualJson = sut.writeValueAsString(d); |
| 76 | + D actualD = sut.readValue(actualJson, D.class); |
| 77 | + |
| 78 | + assertEquals("\"abc:def\"", actualJson); |
| 79 | + assertEquals(d, actualD); |
| 80 | + } |
| 81 | + |
| 82 | + // And then case that fails with [databind#1001] |
| 83 | + public void testWithNamedParameters() throws Exception |
| 84 | + { |
| 85 | + ObjectMapper sut = new ObjectMapper() |
| 86 | + .setAnnotationIntrospector(new CreatorNameIntrospector()); |
| 87 | + |
| 88 | + D d = D.make("abc:def"); |
| 89 | + |
| 90 | + String actualJson = sut.writeValueAsString(d); |
| 91 | + D actualD = sut.readValue(actualJson, D.class); |
| 92 | + |
| 93 | + assertEquals("\"abc:def\"", actualJson); |
| 94 | + assertEquals(d, actualD); |
| 95 | + } |
| 96 | +} |
0 commit comments