|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Assertions; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.annotation.JacksonInject; |
| 7 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 8 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 9 | +import com.fasterxml.jackson.databind.*; |
| 10 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 11 | + |
| 12 | +public class JacksonInject4218Test |
| 13 | +{ |
| 14 | + static class Dto { |
| 15 | + @JacksonInject("id") |
| 16 | + String id; |
| 17 | + |
| 18 | + @JsonCreator |
| 19 | + Dto(@JacksonInject("id") |
| 20 | + @JsonProperty("id") |
| 21 | + String id |
| 22 | + ) { |
| 23 | + this.id = id; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + class MyInjectableValues extends InjectableValues.Std |
| 28 | + { |
| 29 | + private static final long serialVersionUID = 1L; |
| 30 | + |
| 31 | + int nextId = 1; // count up if injected |
| 32 | + |
| 33 | + @Override |
| 34 | + public Object findInjectableValue( |
| 35 | + Object valueId, |
| 36 | + DeserializationContext ctxt, |
| 37 | + BeanProperty forProperty, |
| 38 | + Object beanInstance |
| 39 | + ) throws JsonMappingException { |
| 40 | + if (valueId.equals("id")) { |
| 41 | + return "id" + nextId++; |
| 42 | + } else { |
| 43 | + return super.findInjectableValue(valueId, ctxt, forProperty, beanInstance); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void test() throws Exception |
| 50 | + { |
| 51 | + ObjectReader reader = new JsonMapper() |
| 52 | + .readerFor(Dto.class) |
| 53 | + .with(new MyInjectableValues()); |
| 54 | + |
| 55 | + Dto dto = reader.readValue("{}"); |
| 56 | + String actual = dto.id; |
| 57 | + |
| 58 | + Assertions.assertEquals("id1", actual); |
| 59 | + } |
| 60 | +} |
0 commit comments