Skip to content

Commit

Permalink
Special handling to support JsonIgnore for Records deserialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
yihtserns committed Jan 12, 2023
1 parent 4ab38d5 commit 2c15147
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
// regular property? needs buffering
SettableBeanProperty prop = _beanProperties.find(propName);
if (prop != null) {
if (prop.isIgnorable() && _beanType.isRecordType()) {
handleIgnoredProperty(p, ctxt, handledType(), propName);
continue;
}
try {
buffer.bufferProperty(prop, _deserializeWithErrorWrapping(p, ctxt, prop));
} catch (UnresolvedForwardReference reference) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,12 @@ protected void _removeUnwantedProperties(Map<String, POJOPropertyBuilder> props)
}
// Otherwise, check ignorals
if (prop.anyIgnorals()) {
if (_classDef.getType().isRecordType()) {
prop.removeIgnored();
_collectIgnorals(prop.getName());
continue;
}

// first: if one or more ignorals, and no explicit markers, remove the whole thing
// 16-May-2022, tatu: NOTE! As per [databind#3357] need to consider
// only explicit inclusion by accessors OTHER than ones with ignoral marker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.ClassUtil;
Expand Down Expand Up @@ -140,26 +139,9 @@ public void testSerializeJsonIgnoreRecord() throws Exception {
assertEquals("{\"id\":123}", json);
}

/**
* This test-case is just for documentation purpose:
* Because unlike JavaBean where the setter can be ignored, the Record's constructor argument must
* have value.
* <p/>
* You can make a constructor parameter optional by {@link JacksonInject}-ing a value, or by creating an alternative
* JsonCreator.mode=PROPERTIES constructor that excludes the ignored parameter.
*
* @see #testDeserializeConstructorInjectRecord()
* @see RecordCreatorsTest#testDeserializeWithAltCtor()
*/
public void testDeserializeJsonIgnoreRecord_WillFail() throws Exception {
try {
MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithIgnore.class);

fail("should not pass");
} catch (InvalidDefinitionException e) {
verifyException(e, "Argument #1 of constructor");
verifyException(e, "must have name when multiple-parameter constructor annotated as Creator");
}
public void testDeserializeJsonIgnoreRecord() throws Exception {
RecordWithIgnore value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithIgnore.class);
assertEquals(new RecordWithIgnore(123, null), value);
}

public void testSerializeJsonRename() throws Exception {
Expand Down

0 comments on commit 2c15147

Please sign in to comment.