|
1 | 1 | package com.fasterxml.jackson.databind.deser;
|
2 | 2 |
|
3 | 3 | import java.beans.ConstructorProperties;
|
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
4 | 7 |
|
| 8 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
5 | 9 | import com.fasterxml.jackson.annotation.JsonProperty;
|
6 | 10 | import com.fasterxml.jackson.databind.*;
|
7 | 11 |
|
@@ -66,6 +70,41 @@ public Foo1345(String id, String name) {
|
66 | 70 | protected Foo1345() { }
|
67 | 71 | }
|
68 | 72 |
|
| 73 | + // [databind#1382] |
| 74 | + static class Foo1382 { |
| 75 | + @JsonProperty(access = JsonProperty.Access.READ_ONLY) |
| 76 | + private List<Long> list = new ArrayList<>(); |
| 77 | + |
| 78 | + List<Long> getList() { |
| 79 | + return list; |
| 80 | + } |
| 81 | + |
| 82 | + public Foo1382 setList(List<Long> list) { |
| 83 | + this.list = list; |
| 84 | + return this; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // [databind#1805] |
| 89 | + static class UserWithReadOnly1805 { |
| 90 | + public String name; |
| 91 | + |
| 92 | + @JsonProperty(access = JsonProperty.Access.READ_ONLY) |
| 93 | + public List<String> getRoles() { |
| 94 | + return Arrays.asList("admin", "monitor"); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // [databind#1805] |
| 99 | + @JsonIgnoreProperties(value={ "roles" }, allowGetters=true) |
| 100 | + static class UserAllowGetters1805 { |
| 101 | + public String name; |
| 102 | + |
| 103 | + public List<String> getRoles() { |
| 104 | + return Arrays.asList("admin", "monitor"); |
| 105 | + } |
| 106 | + } |
| 107 | + |
69 | 108 | /*
|
70 | 109 | /**********************************************************
|
71 | 110 | /* Test methods
|
@@ -93,11 +132,39 @@ public void testReadOnly935() throws Exception
|
93 | 132 | assertNotNull(result);
|
94 | 133 | }
|
95 | 134 |
|
| 135 | + // [databind#1345] |
96 | 136 | public void testReadOnly1345() throws Exception
|
97 | 137 | {
|
98 | 138 | Foo1345 result = MAPPER.readValue("{\"name\":\"test\"}", Foo1345.class);
|
99 | 139 | assertNotNull(result);
|
100 | 140 | assertEquals("test", result.name);
|
101 | 141 | assertNull(result.id);
|
102 | 142 | }
|
| 143 | + |
| 144 | + // [databind#1382] |
| 145 | + public void testReadOnly1382() throws Exception |
| 146 | + { |
| 147 | + String payload = "{\"list\":[1,2,3,4]}"; |
| 148 | + Foo1382 foo = MAPPER.readValue(payload, Foo1382.class); |
| 149 | + assertTrue("List should be empty", foo.getList().isEmpty()); |
| 150 | + } |
| 151 | + |
| 152 | + // [databind#1805] |
| 153 | + public void testViaReadOnly() throws Exception { |
| 154 | + UserWithReadOnly1805 user = new UserWithReadOnly1805(); |
| 155 | + user.name = "foo"; |
| 156 | + String json = MAPPER.writeValueAsString(user); |
| 157 | + UserWithReadOnly1805 result = MAPPER.readValue(json, UserWithReadOnly1805.class); |
| 158 | + assertNotNull(result); |
| 159 | + } |
| 160 | + |
| 161 | + // [databind#1805] |
| 162 | + public void testUsingAllowGetters() throws Exception { |
| 163 | + UserAllowGetters1805 user = new UserAllowGetters1805(); |
| 164 | + user.name = "foo"; |
| 165 | + String json = MAPPER.writeValueAsString(user); |
| 166 | + assertTrue(json.contains("roles")); |
| 167 | + UserAllowGetters1805 result = MAPPER.readValue(json, UserAllowGetters1805.class); |
| 168 | + assertNotNull(result); |
| 169 | + } |
103 | 170 | }
|
0 commit comments