|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.*; |
| 6 | +import com.fasterxml.jackson.databind.cfg.ConstructorDetector; |
| 7 | +import com.fasterxml.jackson.databind.introspect.AnnotatedMember; |
| 8 | +import com.fasterxml.jackson.databind.introspect.AnnotatedParameter; |
| 9 | +import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; |
| 10 | + |
| 11 | +public class CreatorNullPrimitives2977Test extends BaseMapTest |
| 12 | +{ |
| 13 | + @SuppressWarnings("serial") |
| 14 | + static class ABCParamIntrospector extends JacksonAnnotationIntrospector |
| 15 | + { |
| 16 | + @Override |
| 17 | + public String findImplicitPropertyName(AnnotatedMember param) { |
| 18 | + if (param instanceof AnnotatedParameter) { |
| 19 | + AnnotatedParameter ap = (AnnotatedParameter) param; |
| 20 | + switch (ap.getIndex()) { |
| 21 | + case 0: return "a"; |
| 22 | + case 1: return "b"; |
| 23 | + case 2: return "c"; |
| 24 | + default: |
| 25 | + return "param"+ap.getIndex(); |
| 26 | + } |
| 27 | + } |
| 28 | + return super.findImplicitPropertyName(param); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + static class TestClass2977 { |
| 33 | + @JsonProperty("aa") |
| 34 | + final int a; |
| 35 | + |
| 36 | + // work-around: add JsonProperty on parameter name, or JsonCreator |
| 37 | +// @com.fasterxml.jackson.annotation.JsonCreator |
| 38 | + public TestClass2977(int a) { |
| 39 | + this.a = a; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // [databind#2977] |
| 44 | + public void testDefaultingWithNull2977() throws Exception |
| 45 | + { |
| 46 | + ObjectMapper mapper = jsonMapperBuilder() |
| 47 | + .enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) |
| 48 | + .annotationIntrospector(new ABCParamIntrospector()) |
| 49 | + .constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED) |
| 50 | + .build(); |
| 51 | + TestClass2977 result = mapper.readValue(a2q("{'aa': 8}"), TestClass2977.class); |
| 52 | + assertEquals(8, result.a); |
| 53 | + } |
| 54 | +} |
0 commit comments