1 package com.fasterxml.jackson.databind.deser; 2 3 import java.beans.ConstructorProperties; 4 import java.util.ArrayList; 5 import java.util.Arrays; 6 import java.util.List; 7 8 import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 9 import com.fasterxml.jackson.annotation.JsonProperty; 10 11 import com.fasterxml.jackson.databind.*; 12 13 public class ReadOrWriteOnlyTest extends BaseMapTest 14 { 15 // for [databind#935], verify read/write-only cases 16 static class ReadXWriteY { 17 @JsonProperty(access=JsonProperty.Access.READ_ONLY) 18 public int x = 1; 19 20 @JsonProperty(access=JsonProperty.Access.WRITE_ONLY) 21 public int y = 2; 22 setX(int x)23 public void setX(int x) { 24 throw new Error("Should NOT set x"); 25 } 26 getY()27 public int getY() { 28 throw new Error("Should NOT get y"); 29 } 30 } 31 32 public static class Pojo935 33 { 34 private String firstName = "Foo"; 35 private String lastName = "Bar"; 36 37 @JsonProperty(access = JsonProperty.Access.READ_ONLY) getFullName()38 public String getFullName() { 39 return firstName + " " + lastName; 40 } 41 getFirstName()42 public String getFirstName() { 43 return firstName; 44 } 45 setFirstName(String n)46 public void setFirstName(String n) { 47 firstName = n; 48 } 49 getLastName()50 public String getLastName() { 51 return lastName; 52 } 53 setLastName(String n)54 public void setLastName(String n) { 55 lastName = n; 56 } 57 } 58 59 // for [databind#1345], emulate way Lombok embellishes classes 60 static class Foo1345 { 61 @JsonProperty(access=JsonProperty.Access.READ_ONLY) 62 public String id; 63 public String name; 64 65 @ConstructorProperties({ "id", "name" }) Foo1345(String id, String name)66 public Foo1345(String id, String name) { 67 this.id = id; 68 this.name = name; 69 } 70 Foo1345()71 protected Foo1345() { } 72 } 73 74 // [databind#1382] 75 static class Foo1382 { 76 @JsonProperty(access = JsonProperty.Access.READ_ONLY) 77 private List<Long> list = new ArrayList<>(); 78 getList()79 List<Long> getList() { 80 return list; 81 } 82 setList(List<Long> list)83 public Foo1382 setList(List<Long> list) { 84 this.list = list; 85 return this; 86 } 87 } 88 89 // [databind#1805] 90 static class UserWithReadOnly1805 { 91 public String name; 92 93 @JsonProperty(access = JsonProperty.Access.READ_ONLY) getRoles()94 public List<String> getRoles() { 95 return Arrays.asList("admin", "monitor"); 96 } 97 } 98 99 // [databind#1805] 100 @JsonIgnoreProperties(value={ "roles" }, allowGetters=true) 101 static class UserAllowGetters1805 { 102 public String name; 103 getRoles()104 public List<String> getRoles() { 105 return Arrays.asList("admin", "monitor"); 106 } 107 } 108 109 // [databind#2779]: ignorable property renaming 110 static class Bean2779 { 111 String works; 112 113 @JsonProperty(value = "t", access = JsonProperty.Access.READ_ONLY) getDoesntWork()114 public String getDoesntWork() { 115 return "pleaseFixThisBug"; 116 } 117 getWorks()118 public String getWorks() { 119 return works; 120 } 121 setWorks(String works)122 public void setWorks(String works) { 123 this.works = works; 124 } 125 } 126 127 /* 128 /********************************************************** 129 /* Test methods 130 /********************************************************** 131 */ 132 133 private final ObjectMapper MAPPER = new ObjectMapper(); 134 135 // [databind#935] testReadOnlyAndWriteOnly()136 public void testReadOnlyAndWriteOnly() throws Exception 137 { 138 String json = MAPPER.writeValueAsString(new ReadXWriteY()); 139 assertEquals("{\"x\":1}", json); 140 141 ReadXWriteY result = MAPPER.readValue("{\"x\":5, \"y\":6}", ReadXWriteY.class); 142 assertNotNull(result); 143 assertEquals(1, result.x); 144 assertEquals(6, result.y); 145 } 146 testReadOnly935()147 public void testReadOnly935() throws Exception 148 { 149 String json = MAPPER.writeValueAsString(new Pojo935()); 150 Pojo935 result = MAPPER.readValue(json, Pojo935.class); 151 assertNotNull(result); 152 } 153 154 // [databind#1345] testReadOnly1345()155 public void testReadOnly1345() throws Exception 156 { 157 Foo1345 result = MAPPER.readValue("{\"name\":\"test\"}", Foo1345.class); 158 assertNotNull(result); 159 assertEquals("test", result.name); 160 assertNull(result.id); 161 } 162 163 // [databind#1382] testReadOnly1382()164 public void testReadOnly1382() throws Exception 165 { 166 String payload = "{\"list\":[1,2,3,4]}"; 167 Foo1382 foo = MAPPER.readValue(payload, Foo1382.class); 168 assertTrue("List should be empty", foo.getList().isEmpty()); 169 } 170 171 // [databind#1805] testViaReadOnly()172 public void testViaReadOnly() throws Exception { 173 UserWithReadOnly1805 user = new UserWithReadOnly1805(); 174 user.name = "foo"; 175 String json = MAPPER.writeValueAsString(user); 176 UserWithReadOnly1805 result = MAPPER.readValue(json, UserWithReadOnly1805.class); 177 assertNotNull(result); 178 } 179 180 // [databind#1805] testUsingAllowGetters()181 public void testUsingAllowGetters() throws Exception { 182 UserAllowGetters1805 user = new UserAllowGetters1805(); 183 user.name = "foo"; 184 String json = MAPPER.writeValueAsString(user); 185 assertTrue(json.contains("roles")); 186 UserAllowGetters1805 result = MAPPER.readValue(json, UserAllowGetters1805.class); 187 assertNotNull(result); 188 } 189 190 // [databind#2779]: ignorable property renaming testIssue2779()191 public void testIssue2779() throws Exception 192 { 193 Bean2779 bean = new Bean2779(); 194 bean.setWorks("works"); 195 196 String json = MAPPER.writeValueAsString(bean); 197 Bean2779 newBean = MAPPER.readValue(json, Bean2779.class); 198 assertNotNull(newBean); 199 } 200 } 201