Skip to content

Commit

Permalink
Fixes #145: use correct delegation for `GuavaOptionalDeserializer.get…
Browse files Browse the repository at this point in the history
…Empty()` (#146)
  • Loading branch information
cowtowncoder authored Feb 12, 2024
1 parent 3456419 commit b94e67d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public Optional<?> getNullValue(DeserializationContext ctxt) throws JsonMappingE

@Override
public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
return getEmptyValue(ctxt);
// 11-Feb-2024, tatu: [datatypes-collections#145] Should delegate
// to use "null value" (same as default of `ReferenceTypeDeserializer`)
return getNullValue(ctxt);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
package com.fasterxml.jackson.datatype.guava.optional;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;

import com.fasterxml.jackson.core.type.TypeReference;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.guava.ModuleTestBase;

import com.google.common.base.Optional;

public class OptionalFromEmptyTest extends ModuleTestBase
{
static class OptionalBeanWithEmpty {
protected Optional<String> _value;

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public OptionalBeanWithEmpty(@JsonProperty("value")
@JsonSetter(nulls = Nulls.AS_EMPTY) Optional<String> ref) {
_value = ref;
}
}

private final ObjectMapper MAPPER = mapperWithModule();

// [datatype-guava#48]
public void testDeserNull() throws Exception {
Optional<?> value = MAPPER.readValue("\"\"", new TypeReference<Optional<Integer>>() {});
assertEquals(false, value.isPresent());
}

// [datatypes-collections#145]
public void testDeserEmptyViaConstructor() throws Exception {
OptionalBeanWithEmpty bean = MAPPER.readValue("{}",
OptionalBeanWithEmpty.class);
assertNotNull(bean._value);
assertFalse(bean._value.isPresent());
}
}
4 changes: 4 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Active Maintainers:
=== Releases ===
------------------------------------------------------------------------

2.16.2 (not yet released)

#145: `GuavaOptionalDeserializer.getEmptyValue()` should not call itself recursively

2.16.1 (24-Dec-2023)

No changes since 2.16.0
Expand Down

0 comments on commit b94e67d

Please sign in to comment.