|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import java.lang.annotation.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonSetter; |
| 6 | +import com.fasterxml.jackson.annotation.Nulls; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.*; |
| 9 | +import com.fasterxml.jackson.databind.cfg.*; |
| 10 | +import com.fasterxml.jackson.databind.exc.InvalidNullException; |
| 11 | +import com.fasterxml.jackson.databind.introspect.AnnotatedMember; |
| 12 | +import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; |
| 13 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 14 | + |
| 15 | +// Tests for [databind#1498] (Jackson 2.12) |
| 16 | +public class ConstructorDetector3241Test extends BaseMapTest |
| 17 | +{ |
| 18 | + // Helper annotation to work around lack of implicit name access with Jackson 2.x |
| 19 | + @Target(ElementType.PARAMETER) |
| 20 | + @Retention(RetentionPolicy.RUNTIME) |
| 21 | + @interface ImplicitName { |
| 22 | + String value(); |
| 23 | + } |
| 24 | + |
| 25 | + // And annotation introspector to make use of it |
| 26 | + @SuppressWarnings("serial") |
| 27 | + static class CtorNameIntrospector extends JacksonAnnotationIntrospector |
| 28 | + { |
| 29 | + @Override |
| 30 | + public String findImplicitPropertyName(//MapperConfig<?> config, |
| 31 | + AnnotatedMember member) { |
| 32 | + final ImplicitName ann = member.getAnnotation(ImplicitName.class); |
| 33 | + return (ann == null) ? null : ann.value(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // [databind#3241] |
| 38 | + static class Input3241 { |
| 39 | + private final Boolean field; |
| 40 | + |
| 41 | + // @JsonCreator gone! |
| 42 | + public Input3241(@ImplicitName("field") Boolean field) { |
| 43 | + if (field == null) { |
| 44 | + throw new NullPointerException("Should not get here!"); |
| 45 | + } |
| 46 | + this.field = field; |
| 47 | + } |
| 48 | + |
| 49 | + public Boolean field() { |
| 50 | + return field; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // [databind#3241] |
| 55 | + public void testNullHandlingCreator3241() throws Exception |
| 56 | + { |
| 57 | + ObjectMapper mapper = mapperBuilder() |
| 58 | + .constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED) // new! |
| 59 | + .defaultSetterInfo(JsonSetter.Value.construct(Nulls.FAIL, Nulls.FAIL)) |
| 60 | + .build(); |
| 61 | + |
| 62 | + try { |
| 63 | + mapper.readValue("{ \"field\": null }", Input3241.class); |
| 64 | + fail("InvalidNullException expected"); |
| 65 | + } catch (InvalidNullException e) { |
| 66 | + verifyException(e, "Invalid `null` value encountered"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /* |
| 71 | + /********************************************************************** |
| 72 | + /* Helper methods |
| 73 | + /********************************************************************** |
| 74 | + */ |
| 75 | + |
| 76 | + private JsonMapper.Builder mapperBuilder() { |
| 77 | + return JsonMapper.builder() |
| 78 | + .annotationIntrospector(new CtorNameIntrospector()); |
| 79 | + } |
| 80 | +} |
0 commit comments