|
| 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