Skip to content

Commit 20053ea

Browse files
bidorffOLcowtowncoder
authored andcommitted
Ignore the explicit names of READONLY properties when they are available (#2720)
1 parent bc97be3 commit 20053ea

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -792,10 +792,17 @@ protected void _removeUnwantedAccessor(Map<String, POJOPropertyBuilder> props)
792792

793793
while (it.hasNext()) {
794794
POJOPropertyBuilder prop = it.next();
795+
// [databind#2719]: ignore the explicit names when they are available
796+
Collection<PropertyName> names = prop.findExplicitNames();
797+
if (names.isEmpty()) {
798+
names = Collections.singleton(prop.getFullName());
799+
}
795800
// 26-Jan-2017, tatu: [databind#935]: need to denote removal of
796801
JsonProperty.Access acc = prop.removeNonVisible(inferMutators);
797802
if (acc == JsonProperty.Access.READ_ONLY) {
798-
_collectIgnorals(prop.getName());
803+
for (PropertyName explicitName : names) {
804+
_collectIgnorals(explicitName.getSimpleName());
805+
}
799806
}
800807
}
801808
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fasterxml.jackson.databind.deser;
2+
3+
import com.fasterxml.jackson.annotation.*;
4+
5+
import com.fasterxml.jackson.databind.*;
6+
7+
public class ReadOnlyDeser2719Test extends BaseMapTest
8+
{
9+
10+
// [databind#2719]
11+
static class UserWithReadOnly {
12+
@JsonProperty(value = "username", access = JsonProperty.Access.READ_ONLY)
13+
public String name;
14+
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
15+
public String password;
16+
public String login;
17+
}
18+
19+
/*
20+
/**********************************************************
21+
/* Test methods
22+
/**********************************************************
23+
*/
24+
25+
private final ObjectMapper MAPPER = newJsonMapper();
26+
27+
public void testFailOnIgnore() throws Exception
28+
{
29+
ObjectReader r = MAPPER.readerFor(UserWithReadOnly.class);
30+
31+
// First, fine to get 'login'
32+
UserWithReadOnly result = r.readValue(aposToQuotes("{'login':'foo'}"));
33+
assertEquals("foo", result.login);
34+
35+
// but not 'password'
36+
r = r.with(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
37+
try {
38+
r.readValue(aposToQuotes("{'login':'foo', 'password':'bar'}"));
39+
fail("Should fail");
40+
} catch (JsonMappingException e) {
41+
verifyException(e, "Ignored field");
42+
}
43+
44+
// or 'username'
45+
r = r.with(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
46+
try {
47+
r.readValue(aposToQuotes("{'login':'foo', 'username':'bar'}"));
48+
fail("Should fail");
49+
} catch (JsonMappingException e) {
50+
verifyException(e, "Ignored field");
51+
}
52+
53+
}
54+
55+
}

0 commit comments

Comments
 (0)