|
3 | 3 | import java.lang.annotation.Annotation;
|
4 | 4 | import java.util.*;
|
5 | 5 |
|
| 6 | +import com.fasterxml.jackson.annotation.JacksonInject; |
| 7 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
6 | 8 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
7 | 9 | import com.fasterxml.jackson.annotation.JsonInclude;
|
8 | 10 |
|
| 11 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 12 | +import com.fasterxml.jackson.annotation.OptBoolean; |
9 | 13 | import com.fasterxml.jackson.core.Version;
|
10 | 14 |
|
11 | 15 | import com.fasterxml.jackson.databind.*;
|
12 | 16 | import com.fasterxml.jackson.databind.cfg.MapperConfig;
|
13 | 17 | import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
|
14 | 18 | import com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer;
|
| 19 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
15 | 20 | import com.fasterxml.jackson.databind.jsontype.NamedType;
|
16 | 21 | import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
|
17 | 22 | import com.fasterxml.jackson.databind.ser.std.StringSerializer;
|
@@ -601,4 +606,94 @@ public void testInclusionMerging() throws Exception
|
601 | 606 | assertEquals(JsonInclude.Include.NON_EMPTY, v21.getContentInclusion());
|
602 | 607 | assertEquals(JsonInclude.Include.NON_ABSENT, v21.getValueInclusion());
|
603 | 608 | }
|
| 609 | + |
| 610 | + /* |
| 611 | + /********************************************************** |
| 612 | + /* Introspectors and test for [jackson-modules-base#134]/[databind#962] |
| 613 | + /********************************************************** |
| 614 | + */ |
| 615 | + static class TestIntrospector extends NopAnnotationIntrospector { |
| 616 | + @Override |
| 617 | + public JacksonInject.Value findInjectableValue(AnnotatedMember m) { |
| 618 | + if (m.getRawType() == UnreadableBean.class) { |
| 619 | + return JacksonInject.Value.forId("jjj"); |
| 620 | + } |
| 621 | + return null; |
| 622 | + } |
| 623 | + } |
| 624 | + |
| 625 | + static class TestInjector extends InjectableValues { |
| 626 | + @Override |
| 627 | + public Object findInjectableValue(Object valueId, DeserializationContext ctxt, BeanProperty forProperty, Object beanInstance) { |
| 628 | + if (valueId == "jjj") { |
| 629 | + UnreadableBean bean = new UnreadableBean(); |
| 630 | + bean.setValue(1); |
| 631 | + return bean; |
| 632 | + } |
| 633 | + return null; |
| 634 | + } |
| 635 | + } |
| 636 | + |
| 637 | + enum SimpleEnum { ONE, TWO } |
| 638 | + |
| 639 | + static class UnreadableBean { |
| 640 | + public SimpleEnum value; |
| 641 | + |
| 642 | + public void setValue(SimpleEnum value) { |
| 643 | + this.value = value; |
| 644 | + } |
| 645 | + |
| 646 | + public void setValue(Integer intValue) { |
| 647 | + this.value = SimpleEnum.values()[intValue]; |
| 648 | + } |
| 649 | + |
| 650 | + public SimpleEnum getValue() { |
| 651 | + return value; |
| 652 | + } |
| 653 | + } |
| 654 | + |
| 655 | + static class ReadableInjectedBean { |
| 656 | + public ReadableInjectedBean(@JacksonInject(useInput = OptBoolean.FALSE) UnreadableBean injectBean) { |
| 657 | + this.injectBean = injectBean; |
| 658 | + } |
| 659 | + @JsonProperty |
| 660 | + private String foo; |
| 661 | + @JsonIgnore |
| 662 | + private UnreadableBean injectBean; |
| 663 | + } |
| 664 | + |
| 665 | + static class UnreadableInjectedBean { |
| 666 | + public UnreadableInjectedBean(@JacksonInject UnreadableBean injectBean) { |
| 667 | + this.injectBean = injectBean; |
| 668 | + } |
| 669 | + @JsonProperty |
| 670 | + private String foo; |
| 671 | + @JsonIgnore |
| 672 | + private UnreadableBean injectBean; |
| 673 | + } |
| 674 | + |
| 675 | + public void testMergingIntrospectorsForInjection() throws Exception { |
| 676 | + AnnotationIntrospector testIntrospector = new TestIntrospector(); |
| 677 | + ObjectMapper mapper = new JsonMapper(); |
| 678 | + mapper.setInjectableValues(new TestInjector()); |
| 679 | + mapper.setAnnotationIntrospectors( |
| 680 | + new AnnotationIntrospectorPair(testIntrospector, |
| 681 | + mapper.getSerializationConfig().getAnnotationIntrospector()), |
| 682 | + new AnnotationIntrospectorPair(testIntrospector, |
| 683 | + mapper.getDeserializationConfig().getAnnotationIntrospector()) |
| 684 | + ); |
| 685 | + ReadableInjectedBean bean = mapper.readValue("{\"foo\": \"bob\"}", ReadableInjectedBean.class); |
| 686 | + assertEquals("bob", bean.foo); |
| 687 | + assertEquals(SimpleEnum.TWO, bean.injectBean.value); |
| 688 | + |
| 689 | + boolean successReadingUnreadableInjectedBean; |
| 690 | + try { |
| 691 | + UnreadableInjectedBean noBean = mapper.readValue("{\"foo\": \"bob\"}", UnreadableInjectedBean.class); |
| 692 | + successReadingUnreadableInjectedBean = true; |
| 693 | + } catch (JsonMappingException e) { |
| 694 | + successReadingUnreadableInjectedBean = false; |
| 695 | + assertTrue(e.getMessage().contains("Conflicting setter definitions")); |
| 696 | + } |
| 697 | + assertFalse(successReadingUnreadableInjectedBean); |
| 698 | + } |
604 | 699 | }
|
0 commit comments