Skip to content

Commit 75f0777

Browse files
authored
Ignore the explicit names of READONLY properties when they are available (#2720)
1 parent a061742 commit 75f0777

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

756756
while (it.hasNext()) {
757757
POJOPropertyBuilder prop = it.next();
758+
// [databind#2719]: ignore the explicit names when they are available
759+
Collection<PropertyName> names = prop.findExplicitNames();
760+
if (names.isEmpty()) {
761+
names = Collections.singleton(prop.getFullName());
762+
}
758763
// 26-Jan-2017, tatu: [databind#935]: need to denote removal of
759764
JsonProperty.Access acc = prop.removeNonVisible(inferMutators);
760765
if (acc == JsonProperty.Access.READ_ONLY) {
761-
_collectIgnorals(prop.getName());
766+
for (PropertyName explicitName : names) {
767+
_collectIgnorals(explicitName.getSimpleName());
768+
}
762769
}
763770
}
764771
}
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)