Skip to content

Commit

Permalink
Merge findInjectableValues() results in AnnotationIntrospectorPair
Browse files Browse the repository at this point in the history
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 #962 seen by the GuiceAnnotationInspector
in FasterXML/jackson-modules-base#134
  • Loading branch information
josephlbarnett committed May 6, 2021
1 parent e85f7f0 commit 8e3f5d2
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,11 @@ public NameTransformer findUnwrappingNameTransformer(AnnotatedMember member) {
@Override
public JacksonInject.Value findInjectableValue(AnnotatedMember m) {
JacksonInject.Value r = _primary.findInjectableValue(m);
return (r == null) ? _secondary.findInjectableValue(m) : r;
if (r == null || r.getUseInput() == null) {
JacksonInject.Value secondary = _secondary.findInjectableValue(m);
r = (r == null || secondary == null) ? secondary : r.withUseInput(secondary.getUseInput());
}
return r;
}

@Override
Expand Down
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);
}
}

0 comments on commit 8e3f5d2

Please sign in to comment.