|
| 1 | +package com.fasterxml.jackson.databind.tofix; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Assertions; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 7 | +import com.fasterxml.jackson.databind.*; |
| 8 | +import com.fasterxml.jackson.databind.deser.*; |
| 9 | +import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator; |
| 10 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 11 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 12 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 13 | + |
| 14 | +public class AaaCreator4777Test extends DatabindTestUtil |
| 15 | +{ |
| 16 | + static class Foo { |
| 17 | + String whoMadeWho; |
| 18 | + |
| 19 | + protected Foo() { whoMadeWho = "creator"; } |
| 20 | + |
| 21 | + @JsonCreator |
| 22 | + static Foo create() { |
| 23 | + Foo foo = new Foo(); |
| 24 | + foo.whoMadeWho = "factory"; |
| 25 | + return foo; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + @SuppressWarnings("serial") |
| 30 | + static class Instantiator extends StdValueInstantiator { |
| 31 | + public Instantiator(StdValueInstantiator src) { |
| 32 | + super(src); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + static class Instantiators implements ValueInstantiators { |
| 37 | + Instantiator last = null; |
| 38 | + |
| 39 | + @Override |
| 40 | + public ValueInstantiator findValueInstantiator( |
| 41 | + DeserializationConfig config, |
| 42 | + BeanDescription beanDesc, |
| 43 | + ValueInstantiator defaultInstantiator |
| 44 | + ) { |
| 45 | + if (defaultInstantiator instanceof StdValueInstantiator) { |
| 46 | + Instantiator instantiator = new Instantiator((StdValueInstantiator) defaultInstantiator); |
| 47 | + last = instantiator; |
| 48 | + return instantiator; |
| 49 | + } else { |
| 50 | + return defaultInstantiator; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void test() throws Exception { |
| 57 | + Instantiators i = new Instantiators(); |
| 58 | + |
| 59 | + SimpleModule sm = new SimpleModule() { |
| 60 | + @Override |
| 61 | + public void setupModule(SetupContext context) { |
| 62 | + super.setupModule(context); |
| 63 | + context.addValueInstantiators(i); |
| 64 | + } |
| 65 | + }; |
| 66 | + ObjectMapper mapper = JsonMapper.builder().addModule(sm).build(); |
| 67 | + |
| 68 | + Foo result = mapper.readValue("{}", Foo.class); |
| 69 | + |
| 70 | + Assertions.assertEquals("factory", result.whoMadeWho); |
| 71 | + |
| 72 | + Assertions.assertNull(i.last.getWithArgsCreator()); |
| 73 | + } |
| 74 | +} |
0 commit comments