Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
Fix #57 (NPE for @JsonAppend)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 11, 2015
1 parent d68ed5e commit bda2f9b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Project: jackson-module-afterburner

#56: Afterburner does not respect DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS
(reported by shollander@github)
#57: `@JsonAppend` causes `NullPointerException`
(reported by cleiner@github)

2.6.1 (09-Aug-2015)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.module.afterburner.ser;

import java.lang.reflect.Member;
import java.lang.reflect.Modifier;
import java.util.*;

Expand All @@ -8,7 +9,6 @@
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.ser.*;
import com.fasterxml.jackson.databind.util.ClassUtil;

import com.fasterxml.jackson.module.afterburner.util.MyClassLoader;

public class SerializerModifier extends BeanSerializerModifier
Expand Down Expand Up @@ -78,8 +78,13 @@ protected PropertyAccessorCollector findProperties(Class<?> beanClass,
Class<?> type = bpw.getPropertyType();
AnnotatedMember member = bpw.getMember();

// First: we can't access private fields or methods....
if (Modifier.isPrivate(member.getMember().getModifiers())) {
Member jdkMember = member.getMember();
// 11-Sep-2015, tatu: Let's skip virtual members (related to #57)
if (jdkMember == null) {
continue;
}
// We can't access private fields or methods, skip:
if (Modifier.isPrivate(jdkMember.getModifiers())) {
continue;
}
// (although, interestingly enough, can seem to access private classes...)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.fasterxml.jackson.module.afterburner.ser;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter;
import com.fasterxml.jackson.databind.util.Annotations;
import com.fasterxml.jackson.module.afterburner.AfterburnerTestBase;

// most for [module-afterburner#57]
public class JsonAppendTest extends AfterburnerTestBase
{
@JsonAppend(props = @JsonAppend.Prop(name = "virtual", value = MyVirtualPropertyWriter.class))
public static class Pojo {
public final String name;
public Pojo(String name) {
this.name = name;
}
}

public static class MyVirtualPropertyWriter extends VirtualBeanPropertyWriter {
protected MyVirtualPropertyWriter() { }

protected MyVirtualPropertyWriter(BeanPropertyDefinition propDef, Annotations contextAnnotations,
JavaType declaredType) {
super(propDef, contextAnnotations, declaredType);
}
@Override
protected Object value(Object bean, JsonGenerator g, SerializerProvider prov) throws Exception {
return "bar";
}
@Override
public VirtualBeanPropertyWriter withConfig(MapperConfig<?> config, AnnotatedClass declaringClass,
BeanPropertyDefinition propDef, JavaType type) {
return new MyVirtualPropertyWriter(propDef, declaringClass.getAnnotations(), type);
}
}

public void testSimpleAppend() throws Exception
{
ObjectMapper mapper = mapperWithModule();
// ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(new Pojo("foo"));
assertEquals("{\"name\":\"foo\",\"virtual\":\"bar\"}", json);
}
}

0 comments on commit bda2f9b

Please sign in to comment.