From f8da558becd20cf031e3982c67fb2b1a2bf4d7b9 Mon Sep 17 00:00:00 2001 From: Cowtowncoder Date: Fri, 6 Mar 2015 15:08:35 -0800 Subject: [PATCH] Fix #20 --- release-notes/VERSION | 5 +++ .../jackson/module/mrbean/BeanBuilder.java | 5 ++- .../jackson/module/mrbean/POJOProperty.java | 5 ++- .../jackson/module/mrbean/RoundTripTest.java | 38 +++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/fasterxml/jackson/module/mrbean/RoundTripTest.java diff --git a/release-notes/VERSION b/release-notes/VERSION index 1d2d274..86d346b 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -4,6 +4,11 @@ Project: jackson-module-mrbean = Releases ------------------------------------------------------------------------ +2.5.2 (not yet released) + +#20: Serialized beans have extra parameters in JSON + (reported by Craig B) + 2.5.1 (06-Feb-2015) 2.5.0 (01-Jan-2015) diff --git a/src/main/java/com/fasterxml/jackson/module/mrbean/BeanBuilder.java b/src/main/java/com/fasterxml/jackson/module/mrbean/BeanBuilder.java index 7941d75..96fb364 100644 --- a/src/main/java/com/fasterxml/jackson/module/mrbean/BeanBuilder.java +++ b/src/main/java/com/fasterxml/jackson/module/mrbean/BeanBuilder.java @@ -240,7 +240,10 @@ protected void createField(ClassWriter cw, POJOProperty prop, TypeDescription ty { String sig = type.hasGenerics() ? type.genericSignature() : null; String desc = type.erasedSignature(); - FieldVisitor fv = cw.visitField(ACC_PUBLIC, prop.getFieldName(), desc, sig, null); + /* 15-Mar-2015, tatu: Should not be created public as that can cause problems + * like [mrbean#20] + */ + FieldVisitor fv = cw.visitField(ACC_PROTECTED, prop.getFieldName(), desc, sig, null); fv.visitEnd(); } diff --git a/src/main/java/com/fasterxml/jackson/module/mrbean/POJOProperty.java b/src/main/java/com/fasterxml/jackson/module/mrbean/POJOProperty.java index 065c9b8..006cbd3 100644 --- a/src/main/java/com/fasterxml/jackson/module/mrbean/POJOProperty.java +++ b/src/main/java/com/fasterxml/jackson/module/mrbean/POJOProperty.java @@ -29,8 +29,11 @@ public POJOProperty(String name, Class ctxt) { _name = name; _context = ctxt; + /* 06-Mar-2015, tatu: We used to use '_' prefix, but that leads to issues + * like [#20]; as well as prevents expected use without explicit setter. + */ // Let's just prefix field name with single underscore for fun... - _fieldName = "_"+name; + _fieldName = name; } public String getName() { return _name; } diff --git a/src/test/java/com/fasterxml/jackson/module/mrbean/RoundTripTest.java b/src/test/java/com/fasterxml/jackson/module/mrbean/RoundTripTest.java new file mode 100644 index 0000000..17d106e --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/module/mrbean/RoundTripTest.java @@ -0,0 +1,38 @@ +package com.fasterxml.jackson.module.mrbean; + +import com.fasterxml.jackson.databind.*; + +public class RoundTripTest extends BaseTest +{ + public interface Bean { + String getField(); + void setField(String field); + } + + public interface ReadOnlyBean { + String getField(); + } + + // [mrbean#20]: naming convention caused under-score prefixed duplicates + public void testSimple() throws Exception + { + ObjectMapper mapper = new ObjectMapper() + .registerModule(new MrBeanModule()); + final String input = "{\"field\":\"testing\"}"; + final Bean bean = mapper.readValue(input, Bean.class); + assertEquals("testing", bean.getField()); + final String output = mapper.writeValueAsString(bean); + assertEquals(input, output); + } + + public void testSimpleWithoutSetter() throws Exception + { + ObjectMapper mapper = new ObjectMapper() + .registerModule(new MrBeanModule()); + final String input = "{\"field\":\"testing\"}"; + final ReadOnlyBean bean = mapper.readValue(input, ReadOnlyBean.class); + assertEquals("testing", bean.getField()); + final String output = mapper.writeValueAsString(bean); + assertEquals(input, output); + } +}