|
15 | 15 | */
|
16 | 16 | package org.springframework.data.rest.webmvc.json;
|
17 | 17 |
|
18 |
| -import static org.hamcrest.Matchers.*; |
19 |
| -import static org.junit.Assert.*; |
20 |
| -import static org.mockito.Mockito.*; |
| 18 | +import static com.fasterxml.jackson.annotation.JsonProperty.Access.READ_ONLY; |
| 19 | +import static org.hamcrest.Matchers.contains; |
| 20 | +import static org.hamcrest.Matchers.instanceOf; |
| 21 | +import static org.hamcrest.Matchers.is; |
| 22 | +import static org.hamcrest.Matchers.notNullValue; |
| 23 | +import static org.hamcrest.Matchers.nullValue; |
| 24 | +import static org.hamcrest.Matchers.sameInstance; |
| 25 | +import static org.junit.Assert.assertThat; |
| 26 | +import static org.mockito.Mockito.mock; |
21 | 27 |
|
22 | 28 | import lombok.AllArgsConstructor;
|
23 | 29 | import lombok.EqualsAndHashCode;
|
|
58 | 64 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
59 | 65 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
60 | 66 | import com.fasterxml.jackson.annotation.JsonIgnore;
|
| 67 | +import com.fasterxml.jackson.annotation.JsonProperty; |
61 | 68 | import com.fasterxml.jackson.databind.JsonNode;
|
62 | 69 | import com.fasterxml.jackson.databind.ObjectMapper;
|
63 | 70 | import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
@@ -194,6 +201,22 @@ public void doesNotWipeIdAndVersionPropertyForPut() throws Exception {
|
194 | 201 | assertThat(result.version, is(1L));
|
195 | 202 | }
|
196 | 203 |
|
| 204 | + @Test // |
| 205 | + public void doesNotWipeReadOnlyJsonPropertyForPut() throws Exception { |
| 206 | + |
| 207 | + SampleUser sampleUser = new SampleUser("name", "password"); |
| 208 | + sampleUser.lastLogin = new Date(); |
| 209 | + |
| 210 | + ObjectMapper mapper = new ObjectMapper(); |
| 211 | + ObjectNode node = (ObjectNode) mapper.readTree("{ \"name\" : \"another\" }"); |
| 212 | + |
| 213 | + SampleUser result = reader.readPut(node, sampleUser, mapper); |
| 214 | + |
| 215 | + assertThat(result.name, is("another")); |
| 216 | + assertThat(result.password, notNullValue()); |
| 217 | + assertThat(result.lastLogin, notNullValue()); |
| 218 | + } |
| 219 | + |
197 | 220 | @Test // DATAREST-873
|
198 | 221 | public void doesNotApplyInputToReadOnlyFields() throws Exception {
|
199 | 222 |
|
@@ -510,6 +533,9 @@ static class SampleUser {
|
510 | 533 | @JsonIgnore String password;
|
511 | 534 | Map<String, SampleUser> relatedUsers;
|
512 | 535 |
|
| 536 | + @JsonProperty(access = READ_ONLY) |
| 537 | + private Date lastLogin; |
| 538 | + |
513 | 539 | public SampleUser(String name, String password) {
|
514 | 540 |
|
515 | 541 | this.name = name;
|
|
0 commit comments