Skip to content

Dont track unknown props in buffer if ignoreAllUnknown is true #3082

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 2 commits into from
Mar 30, 2021
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 @@ -506,12 +506,15 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
}
continue;
}
// Ok then, let's collect the whole field; name and value
if (unknown == null) {
unknown = new TokenBuffer(p, ctxt);

if(!_ignoreAllUnknown) {
// Ok then, let's collect the whole field; name and value
if (unknown == null) {
unknown = new TokenBuffer(p, ctxt);
}
unknown.writeFieldName(propName);
unknown.copyCurrentStructure(p);
}
unknown.writeFieldName(propName);
unknown.copyCurrentStructure(p);
}

// We hit END_OBJECT, so:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.fasterxml.jackson.databind.deser.filter;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

public class IgnoreUnknownPropertyUsingPropertyBasedTest extends BaseMapTest {

private final ObjectMapper MAPPER = newJsonMapper();

@JsonIgnoreProperties(ignoreUnknown = true)
static class IgnoreUnknownAnySetter {

int a, b;

@JsonCreator
public IgnoreUnknownAnySetter(@JsonProperty("a") int a, @JsonProperty("b") int b) {
this.a = a;
this.b = b;
}

Map<String, Object> props = new HashMap<>();

@JsonAnySetter
public void addProperty(String key, Object value) {
props.put(key, value);
}

@JsonAnyGetter
public Map<String, Object> getProperties() {
return props;
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
static class IgnoreUnknownUnwrapped {

int a, b;

@JsonCreator
public IgnoreUnknownUnwrapped(@JsonProperty("a") int a, @JsonProperty("b") int b) {
this.a = a;
this.b = b;
}

@JsonUnwrapped
UnwrappedChild child;

static class UnwrappedChild {
public int x, y;
}
}

public void testAnySetterWithFailOnUnknownDisabled() throws Exception {
IgnoreUnknownAnySetter value = MAPPER.readValue("{\"a\":1, \"b\":2, \"x\":3, \"y\": 4}", IgnoreUnknownAnySetter.class);
assertNotNull(value);
assertEquals(1, value.a);
assertEquals(2, value.b);
assertEquals(3, value.props.get("x"));
assertEquals(4, value.props.get("y"));
assertEquals(2, value.props.size());
}

public void testUnwrappedWithFailOnUnknownDisabled() throws Exception {
IgnoreUnknownUnwrapped value = MAPPER.readValue("{\"a\":1, \"b\": 2, \"x\":3, \"y\":4}", IgnoreUnknownUnwrapped.class);
assertNotNull(value);
assertEquals(1, value.a);
assertEquals(2, value.b);
assertEquals(3, value.child.x);
assertEquals(4, value.child.y);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ static class IgnoreUnknown {
public int a;
}

@JsonIgnoreProperties(ignoreUnknown=true)
static class IgnoreUnknownAnySetter {

Map<String, Object> props = new HashMap<>();

@JsonAnySetter
public void addProperty(String key, Object value) {
props.put(key, value);
}

@JsonAnyGetter
public Map<String, Object> getProperties() {
return props;
}
}

@JsonIgnoreProperties(ignoreUnknown=true)
static class IgnoreUnknownUnwrapped {

@JsonUnwrapped
UnwrappedChild child;

static class UnwrappedChild {
public int a, b;
}
}

@SuppressWarnings("serial")
@JsonIgnoreProperties({"a", "d"})
static class IgnoreMap extends HashMap<String,Object> { }
Expand Down Expand Up @@ -224,6 +251,21 @@ public void testClassWithIgnoreUnknown() throws Exception
assertEquals(-3, result.a);
}

public void testAnySetterWithFailOnUnknownDisabled() throws Exception
{
IgnoreUnknownAnySetter value = MAPPER.readValue("{\"x\":\"y\", \"a\":\"b\"}", IgnoreUnknownAnySetter.class);
assertNotNull(value);
assertEquals(2, value.props.size());
}

public void testUnwrappedWithFailOnUnknownDisabled() throws Exception
{
IgnoreUnknownUnwrapped value = MAPPER.readValue("{\"a\":1, \"b\":2}", IgnoreUnknownUnwrapped.class);
assertNotNull(value);
assertEquals(1, value.child.a);
assertEquals(2, value.child.b);
}

/**
* Test that verifies that use of {@link JsonIgnore} will add implicit
* skipping of matching properties.
Expand Down