Skip to content

Ignore the explicit names of READONLY properties when they are available #2720

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
May 17, 2020
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 @@ -755,10 +755,17 @@ protected void _removeUnwantedAccessor(Map<String, POJOPropertyBuilder> props)

while (it.hasNext()) {
POJOPropertyBuilder prop = it.next();
// [databind#2719]: ignore the explicit names when they are available
Collection<PropertyName> names = prop.findExplicitNames();
if (names.isEmpty()) {
names = Collections.singleton(prop.getFullName());
}
// 26-Jan-2017, tatu: [databind#935]: need to denote removal of
JsonProperty.Access acc = prop.removeNonVisible(inferMutators);
if (acc == JsonProperty.Access.READ_ONLY) {
_collectIgnorals(prop.getName());
for (PropertyName explicitName : names) {
_collectIgnorals(explicitName.getSimpleName());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.fasterxml.jackson.databind.deser;

import com.fasterxml.jackson.annotation.*;

import com.fasterxml.jackson.databind.*;

public class ReadOnlyDeser2719Test extends BaseMapTest
{

// [databind#2719]
static class UserWithReadOnly {
@JsonProperty(value = "username", access = JsonProperty.Access.READ_ONLY)
public String name;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public String password;
public String login;
}

/*
/**********************************************************
/* Test methods
/**********************************************************
*/

private final ObjectMapper MAPPER = newJsonMapper();

public void testFailOnIgnore() throws Exception
{
ObjectReader r = MAPPER.readerFor(UserWithReadOnly.class);

// First, fine to get 'login'
UserWithReadOnly result = r.readValue(aposToQuotes("{'login':'foo'}"));
assertEquals("foo", result.login);

// but not 'password'
r = r.with(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
try {
r.readValue(aposToQuotes("{'login':'foo', 'password':'bar'}"));
fail("Should fail");
} catch (JsonMappingException e) {
verifyException(e, "Ignored field");
}

// or 'username'
r = r.with(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
try {
r.readValue(aposToQuotes("{'login':'foo', 'username':'bar'}"));
fail("Should fail");
} catch (JsonMappingException e) {
verifyException(e, "Ignored field");
}

}

}