|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JacksonInject; |
| 4 | +import com.fasterxml.jackson.annotation.JsonAutoDetect; |
| 5 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 6 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 7 | +import com.fasterxml.jackson.annotation.JsonSetter; |
| 8 | +import com.fasterxml.jackson.annotation.Nulls; |
| 9 | +import com.fasterxml.jackson.annotation.PropertyAccessor; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.databind.*; |
| 12 | + |
| 13 | +public class JacksonInject2465Test extends BaseMapTest |
| 14 | +{ |
| 15 | + public static final class TestCase2465 { |
| 16 | + private final Internal2465 str; |
| 17 | + private final int id; |
| 18 | + |
| 19 | + @JsonCreator |
| 20 | + public TestCase2465(@JacksonInject Internal2465 str, @JsonProperty("id") int id) { |
| 21 | + this.str = str; |
| 22 | + this.id = id; |
| 23 | + } |
| 24 | + |
| 25 | + public int fetchId() { return id; } |
| 26 | + public Internal2465 fetchInternal() { return str; } |
| 27 | + } |
| 28 | + |
| 29 | + public static final class Internal2465 { |
| 30 | + final String val; |
| 31 | + |
| 32 | + public Internal2465(String val) { |
| 33 | + this.val = val; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // [databind#2465] |
| 38 | + public void testInjectWithCreator() throws Exception |
| 39 | + { |
| 40 | + ObjectMapper mapper = jsonMapperBuilder() |
| 41 | + .defaultSetterInfo(JsonSetter.Value.construct(Nulls.AS_EMPTY, Nulls.AS_EMPTY)) |
| 42 | + .build(); |
| 43 | + mapper.setVisibility(mapper.getVisibilityChecker().withVisibility(PropertyAccessor.FIELD, |
| 44 | + JsonAutoDetect.Visibility.ANY)); |
| 45 | + |
| 46 | + final Internal2465 injected = new Internal2465("test"); |
| 47 | + TestCase2465 o = mapper.readerFor(TestCase2465.class) |
| 48 | + .with(new InjectableValues.Std().addValue(Internal2465.class, injected)) |
| 49 | + .readValue("{\"id\":3}"); |
| 50 | + assertEquals(3, o.fetchId()); |
| 51 | + assertNotNull(o.fetchInternal()); |
| 52 | + } |
| 53 | +} |
0 commit comments