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 ReadOnlyDeserFailOnUnknown2719Test extends BaseMapTest
8 {
9     // [databind#2719]
10     static class UserWithReadOnly {
11         @JsonProperty(value = "username", access = JsonProperty.Access.READ_ONLY)
12         public String name;
13         @JsonProperty(access = JsonProperty.Access.READ_ONLY)
14         public String password;
15         public String login;
16     }
17 
18     /*
19     /**********************************************************
20     /* Test methods
21     /**********************************************************
22      */
23 
24     private final ObjectMapper MAPPER = newJsonMapper();
25 
testFailOnIgnore()26     public void testFailOnIgnore() throws Exception
27     {
28         ObjectReader r = MAPPER.readerFor(UserWithReadOnly.class);
29 
30         // First, fine to get 'login'
31         UserWithReadOnly result = r.readValue(aposToQuotes("{'login':'foo'}"));
32         assertEquals("foo", result.login);
33 
34         // but not 'password'
35         r = r.with(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
36         try {
37             r.readValue(aposToQuotes("{'login':'foo', 'password':'bar'}"));
38             fail("Should fail");
39         } catch (JsonMappingException e) {
40             verifyException(e, "Ignored field");
41         }
42 
43         // or 'username'
44         r = r.with(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
45         try {
46             r.readValue(aposToQuotes("{'login':'foo', 'username':'bar'}"));
47             fail("Should fail");
48         } catch (JsonMappingException e) {
49             verifyException(e, "Ignored field");
50         }
51     }
52 }
53