Skip to content

fix NPE for unknown fields #2297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ protected Object handleUnknownProperties(DeserializationContext ctxt,
unknownTokens.writeEndObject();

// note: buffer does NOT have starting START_OBJECT
JsonParser bufferParser = unknownTokens.asParser();
JsonParser bufferParser = unknownTokens.asParser(ctxt);
while (bufferParser.nextToken() != JsonToken.END_OBJECT) {
String propName = bufferParser.currentName();
// Unknown: let's call handler method
Expand Down Expand Up @@ -1578,7 +1578,7 @@ protected Object handlePolymorphic(JsonParser p, DeserializationContext ctxt,
if (unknownTokens != null) {
// need to add END_OBJECT marker first
unknownTokens.writeEndObject();
JsonParser p2 = unknownTokens.asParser();
JsonParser p2 = unknownTokens.asParser(ctxt);
p2.nextToken(); // to get to first data field
bean = subDeser.deserialize(p2, ctxt, bean);
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.fasterxml.jackson.databind;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
Expand Down Expand Up @@ -293,4 +296,26 @@ public void testSchema() throws Exception
verifyException(e, "Cannot use FormatSchema");
}
}

public void testUnknownFields() throws Exception
{
ObjectMapper mapper = JsonMapper.builder().addHandler(new DeserializationProblemHandler(){
@Override
public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName) throws IOException {
p.readValueAsTree();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NPE is here without changes in BeanDeserializerBase

return true;
}
}).build();
A aObject = mapper.readValue("{\"unknownField\" : 1, \"knownField\": \"test\"}", A.class);

assertEquals("test", aObject.knownField);
}
private static class A{
private String knownField;

@JsonCreator
private A(@JsonProperty("knownField") String knownField) {
this.knownField = knownField;
}
}
}