|
| 1 | +package com.fasterxml.jackson.databind.deser.jdk; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.*; |
| 4 | +import com.fasterxml.jackson.databind.exc.InvalidDefinitionException; |
| 5 | +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; |
| 6 | + |
| 7 | +// [databind#2675]: Void-valued "properties" |
| 8 | +public class VoidProperties2675Test extends BaseMapTest |
| 9 | +{ |
| 10 | + static class VoidBean { |
| 11 | + protected Void value; |
| 12 | + |
| 13 | + public Void getValue() { return null; } |
| 14 | + |
| 15 | +// public void setValue(Void v) { } |
| 16 | + } |
| 17 | + |
| 18 | + /* |
| 19 | + /********************************************************************** |
| 20 | + /* Test methods |
| 21 | + /********************************************************************** |
| 22 | + */ |
| 23 | + |
| 24 | + private final ObjectMapper DEFAULT_MAPPER = sharedMapper(); |
| 25 | + |
| 26 | + private final ObjectMapper VOID_MAPPER = jsonMapperBuilder() |
| 27 | + .enable(MapperFeature.ALLOW_VOID_VALUED_PROPERTIES) |
| 28 | + .build(); |
| 29 | + |
| 30 | + public void testVoidBeanSerialization() throws Exception |
| 31 | + { |
| 32 | + // By default (2.x), not enabled: |
| 33 | + try { |
| 34 | + DEFAULT_MAPPER.writeValueAsString(new VoidBean()); |
| 35 | + fail("Should not pass"); |
| 36 | + } catch (InvalidDefinitionException e) { |
| 37 | + verifyException(e, "no properties discovered"); |
| 38 | + } |
| 39 | + |
| 40 | + // but when enabled |
| 41 | + assertEquals("{\"value\":null}", VOID_MAPPER.writeValueAsString(new VoidBean())); |
| 42 | + } |
| 43 | + |
| 44 | + public void testVoidBeanDeserialization() throws Exception { |
| 45 | + final String DOC = "{\"value\":null}"; |
| 46 | + VoidBean result; |
| 47 | + |
| 48 | + // By default (2.x), not enabled: |
| 49 | + try { |
| 50 | + result = DEFAULT_MAPPER.readValue(DOC, VoidBean.class); |
| 51 | + fail("Should not pass"); |
| 52 | + } catch (UnrecognizedPropertyException e) { |
| 53 | + verifyException(e, "Unrecognized field \"value\""); |
| 54 | + } |
| 55 | + |
| 56 | + // but when enabled |
| 57 | + result = VOID_MAPPER.readValue(DOC, VoidBean.class); |
| 58 | + assertNotNull(result); |
| 59 | + assertNull(result.getValue()); |
| 60 | + } |
| 61 | +} |
0 commit comments