|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 6 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 7 | +import com.fasterxml.jackson.databind.*; |
| 8 | + |
| 9 | +// [databind#2283]: ignore read-only Lists even if "getter-as-setter" enabled |
| 10 | +public class ReadOnlyList2283Test |
| 11 | + extends BaseMapTest |
| 12 | +{ |
| 13 | + static class RenamedToSameOnGetter { |
| 14 | + @JsonProperty(value = "list", access = JsonProperty.Access.READ_ONLY) |
| 15 | + List<Long> getList() { |
| 16 | + return Collections.emptyList(); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + static class RenamedToDifferentOnGetter { |
| 21 | + @JsonProperty(value = "renamedList", access = JsonProperty.Access.READ_ONLY) |
| 22 | + List<Long> getList() { |
| 23 | + return Collections.emptyList(); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + @JsonIgnoreProperties(value={ "renamedList" }, allowGetters=true) |
| 28 | + static class RenamedOnClass { |
| 29 | + @JsonProperty("renamedList") |
| 30 | + List<Long> getList() { |
| 31 | + return Collections.emptyList(); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /* |
| 36 | + /********************************************************** |
| 37 | + /* Test methods |
| 38 | + /********************************************************** |
| 39 | + */ |
| 40 | + |
| 41 | + private final ObjectMapper MAPPER = jsonMapperBuilder() |
| 42 | + .configure(MapperFeature.USE_GETTERS_AS_SETTERS, true).build(); |
| 43 | + |
| 44 | + public void testRenamedToSameOnGetter() throws Exception |
| 45 | + { |
| 46 | + assertEquals("{\"list\":[]}", |
| 47 | + MAPPER.writeValueAsString(new RenamedToSameOnGetter())); |
| 48 | + String payload = "{\"list\":[1,2,3,4]}"; |
| 49 | + RenamedToSameOnGetter foo = MAPPER.readValue(payload, RenamedToSameOnGetter.class); |
| 50 | + assertTrue("List should be empty", foo.getList().isEmpty()); |
| 51 | + } |
| 52 | + |
| 53 | + public void testRenamedToDifferentOnGetter() throws Exception |
| 54 | + { |
| 55 | + assertEquals("{\"renamedList\":[]}", |
| 56 | + MAPPER.writeValueAsString(new RenamedToDifferentOnGetter())); |
| 57 | + String payload = "{\"renamedList\":[1,2,3,4]}"; |
| 58 | + RenamedToDifferentOnGetter foo = MAPPER.readValue(payload, RenamedToDifferentOnGetter.class); |
| 59 | + assertTrue("List should be empty", foo.getList().isEmpty()); |
| 60 | + } |
| 61 | + |
| 62 | + public void testRenamedOnClass() throws Exception |
| 63 | + { |
| 64 | + assertEquals("{\"renamedList\":[]}", |
| 65 | + MAPPER.writeValueAsString(new RenamedOnClass())); |
| 66 | + String payload = "{\"renamedList\":[1,2,3,4]}"; |
| 67 | + RenamedOnClass foo = MAPPER.readValue(payload, RenamedOnClass.class); |
| 68 | + assertTrue("List should be empty", foo.getList().isEmpty()); |
| 69 | + } |
| 70 | +} |
0 commit comments