|
| 1 | +package com.fasterxml.jackson.dataformat.xml.deser.creator; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 4 | +import com.fasterxml.jackson.annotation.JsonValue; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.databind.*; |
| 7 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 8 | +import com.fasterxml.jackson.databind.introspect.AnnotatedMember; |
| 9 | +import com.fasterxml.jackson.databind.introspect.AnnotatedParameter; |
| 10 | +import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; |
| 13 | + |
| 14 | +// copied form [jackson-databind] |
| 15 | +public class ImplicitParamsForCreatorTest |
| 16 | + extends XmlTestBase |
| 17 | +{ |
| 18 | + @SuppressWarnings("serial") |
| 19 | + static class MyParamIntrospector extends JacksonAnnotationIntrospector |
| 20 | + { |
| 21 | + @Override |
| 22 | + public String findImplicitPropertyName(AnnotatedMember param) { |
| 23 | + if (param instanceof AnnotatedParameter) { |
| 24 | + AnnotatedParameter ap = (AnnotatedParameter) param; |
| 25 | + return "paramName"+ap.getIndex(); |
| 26 | + } |
| 27 | + return super.findImplicitPropertyName(param); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + static class XY { |
| 32 | + protected int x, y; |
| 33 | + |
| 34 | + // annotation should NOT be needed with 2.6 any more (except for single-arg case) |
| 35 | + //@com.fasterxml.jackson.annotation.JsonCreator |
| 36 | + public XY(int x, int y) { |
| 37 | + this.x = x; |
| 38 | + this.y = y; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // [databind#2932] |
| 43 | + static class Bean2932 |
| 44 | + { |
| 45 | + int _a, _b; |
| 46 | + |
| 47 | +// @JsonCreator |
| 48 | + public Bean2932(/*@com.fasterxml.jackson.annotation.JsonProperty("paramName0")*/ |
| 49 | + @JsonDeserialize() int a, int b) { |
| 50 | + _a = a; |
| 51 | + _b = b; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // [databind#3654]: infer "DELEGATING" style from `@JsonValue` |
| 56 | + static class XY3654 { |
| 57 | + public int paramName0; // has to be public to misdirect |
| 58 | + |
| 59 | + @JsonCreator |
| 60 | + public XY3654(int paramName0) { |
| 61 | + this.paramName0 = paramName0; |
| 62 | + } |
| 63 | + |
| 64 | + @JsonValue |
| 65 | + public int serializedAs() { |
| 66 | + return paramName0; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /* |
| 71 | + /********************************************************** |
| 72 | + /* Test methods |
| 73 | + /********************************************************** |
| 74 | + */ |
| 75 | + |
| 76 | + private final ObjectMapper MAPPER = mapperBuilder() |
| 77 | + .annotationIntrospector(new MyParamIntrospector()) |
| 78 | + .build(); |
| 79 | + |
| 80 | + public void testNonSingleArgCreator() throws Exception |
| 81 | + { |
| 82 | + XY value = MAPPER.readValue( |
| 83 | + "<XY><paramName0>1</paramName0><paramName1>2</paramName1></XY>", |
| 84 | + XY.class); |
| 85 | + assertNotNull(value); |
| 86 | + assertEquals(1, value.x); |
| 87 | + assertEquals(2, value.y); |
| 88 | + } |
| 89 | + |
| 90 | + // [databind#2932] |
| 91 | + public void testJsonCreatorWithOtherAnnotations() throws Exception |
| 92 | + { |
| 93 | + Bean2932 bean = MAPPER.readValue( |
| 94 | + "<Bean2932><paramName0>1</paramName0><paramName1>2</paramName1></Bean2932>", |
| 95 | + Bean2932.class); |
| 96 | + assertNotNull(bean); |
| 97 | + assertEquals(1, bean._a); |
| 98 | + assertEquals(2, bean._b); |
| 99 | + } |
| 100 | + |
| 101 | + // [databind#3654] |
| 102 | + // 04-Feb-2024, tatu: XML does not have type information wrt Integer so this |
| 103 | + // can't work |
| 104 | + /* |
| 105 | + public void testDelegatingInferFromJsonValue() throws Exception |
| 106 | + { |
| 107 | + // First verify serialization via `@JsonValue` |
| 108 | + assertEquals("<XY3654>123</XY3654>", MAPPER.writeValueAsString(new XY3654(123))); |
| 109 | +
|
| 110 | + // And then deser, should infer despite existence of "matching" property |
| 111 | + XY3654 result = MAPPER.readValue("<XY3654>345</XY3654>", XY3654.class); |
| 112 | + assertEquals(345, result.paramName0); |
| 113 | + } |
| 114 | + */ |
| 115 | +} |
0 commit comments