forked from FasterXML/jackson-databind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge findInjectableValues() results in AnnotationIntrospectorPair
As many AnnotationIntrospector implementations use default values for useInput, allow the secondary introspector's useInput value to combine with the primary's id to prevent losing the useInput value. Fixes a special case of FasterXML#962 seen by the GuiceAnnotationInspector in FasterXML/jackson-modules-base#134
- Loading branch information
1 parent
e85f7f0
commit 8e3f5d2
Showing
2 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/test/java/com/fasterxml/jackson/databind/introspect/AnnotationIntrospectorPairTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.fasterxml.jackson.databind.introspect; | ||
|
||
import com.fasterxml.jackson.annotation.JacksonInject; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.OptBoolean; | ||
import com.fasterxml.jackson.databind.AnnotationIntrospector; | ||
import com.fasterxml.jackson.databind.BeanProperty; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.InjectableValues; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import junit.framework.TestCase; | ||
|
||
public class AnnotationIntrospectorPairTest extends TestCase { | ||
|
||
static class TestIntrospector extends NopAnnotationIntrospector { | ||
@Override | ||
public JacksonInject.Value findInjectableValue(AnnotatedMember m) { | ||
if (m.getRawType() == UnreadableBean.class) { | ||
return JacksonInject.Value.forId("jjj"); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
static class TestInjector extends InjectableValues { | ||
|
||
@Override | ||
public Object findInjectableValue(Object valueId, DeserializationContext ctxt, BeanProperty forProperty, Object beanInstance) { | ||
if (valueId == "jjj") { | ||
UnreadableBean bean = new UnreadableBean(); | ||
bean.setValue(1); | ||
return bean; | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
enum SimpleEnum { ONE, TWO } | ||
|
||
static class UnreadableBean { | ||
public SimpleEnum value; | ||
|
||
public void setValue(SimpleEnum value) { | ||
this.value = value; | ||
} | ||
|
||
public void setValue(Integer intValue) { | ||
this.value = SimpleEnum.values()[intValue]; | ||
} | ||
|
||
public SimpleEnum getValue() { | ||
return value; | ||
} | ||
} | ||
|
||
static class ReadableInjectedBean { | ||
public ReadableInjectedBean(@JacksonInject(useInput = OptBoolean.FALSE) UnreadableBean injectBean) { | ||
this.injectBean = injectBean; | ||
} | ||
@JsonProperty | ||
private String foo; | ||
@JsonIgnore | ||
private UnreadableBean injectBean; | ||
} | ||
|
||
static class UnreadableInjectedBean { | ||
public UnreadableInjectedBean(@JacksonInject UnreadableBean injectBean) { | ||
this.injectBean = injectBean; | ||
} | ||
@JsonProperty | ||
private String foo; | ||
@JsonIgnore | ||
private UnreadableBean injectBean; | ||
} | ||
|
||
public void testMergingIntrospectors() throws Exception { | ||
AnnotationIntrospector testInstrospector = new TestIntrospector(); | ||
ObjectMapper mapper = new JsonMapper(); | ||
mapper.setInjectableValues(new TestInjector()); | ||
mapper.setAnnotationIntrospectors( | ||
new AnnotationIntrospectorPair(testInstrospector, | ||
mapper.getSerializationConfig().getAnnotationIntrospector()), | ||
new AnnotationIntrospectorPair(testInstrospector, | ||
mapper.getDeserializationConfig().getAnnotationIntrospector()) | ||
); | ||
ReadableInjectedBean bean = mapper.readValue("{\"foo\": \"bob\"}", ReadableInjectedBean.class); | ||
assertEquals("bob", bean.foo); | ||
assertEquals(SimpleEnum.TWO, bean.injectBean.value); | ||
|
||
boolean successReadingUnreadableInjectedBean; | ||
try { | ||
UnreadableInjectedBean noBean = mapper.readValue("{\"foo\": \"bob\"}", UnreadableInjectedBean.class); | ||
successReadingUnreadableInjectedBean = true; | ||
} catch (JsonMappingException e) { | ||
successReadingUnreadableInjectedBean = false; | ||
assertTrue(e.getMessage().contains("Conflicting setter definitions")); | ||
} | ||
assertFalse(successReadingUnreadableInjectedBean); | ||
} | ||
} |