From dd5e6d046f2b43c35f58a17919eb2ed9197dc0ec Mon Sep 17 00:00:00 2001 From: Bidorff David Date: Wed, 13 May 2020 15:38:47 +0200 Subject: [PATCH] Ignore the explicit names of READONLY properties when they are available --- .../introspect/POJOPropertiesCollector.java | 9 ++- .../databind/deser/ReadOnlyDeser2719Test.java | 55 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/fasterxml/jackson/databind/deser/ReadOnlyDeser2719Test.java diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java index 28a805dabb..754421af27 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java @@ -755,10 +755,17 @@ protected void _removeUnwantedAccessor(Map props) while (it.hasNext()) { POJOPropertyBuilder prop = it.next(); + // [databind#2719]: ignore the explicit names when they are available + Collection 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()); + } } } } diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/ReadOnlyDeser2719Test.java b/src/test/java/com/fasterxml/jackson/databind/deser/ReadOnlyDeser2719Test.java new file mode 100644 index 0000000000..18c432a3b8 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/databind/deser/ReadOnlyDeser2719Test.java @@ -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"); + } + + } + +}