|
| 1 | +package com.fasterxml.jackson.databind.tofix; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 9 | +import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected; |
| 10 | + |
| 11 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 12 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 13 | + |
| 14 | +public class RecordWithReadOnly5049Test extends DatabindTestUtil |
| 15 | +{ |
| 16 | + record ReadOnly5049Record( |
| 17 | + @JsonProperty(value = "a", access = JsonProperty.Access.READ_ONLY) String a, |
| 18 | + @JsonProperty(value = "b", access = JsonProperty.Access.READ_ONLY) String b) { |
| 19 | + } |
| 20 | + |
| 21 | + static class ReadOnly5049Pojo |
| 22 | + { |
| 23 | + protected String a, b; |
| 24 | + |
| 25 | + ReadOnly5049Pojo( |
| 26 | + @JsonProperty(value = "a", access = JsonProperty.Access.READ_ONLY) String a, |
| 27 | + @JsonProperty(value = "b", access = JsonProperty.Access.READ_ONLY) String b) { |
| 28 | + this.a = a; |
| 29 | + this.b = b; |
| 30 | + } |
| 31 | + |
| 32 | + public String getA() { return a; } |
| 33 | + public String getB() { return b; } |
| 34 | + } |
| 35 | + |
| 36 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 37 | + |
| 38 | + @Test |
| 39 | + void testRoundtripPOJO() throws Exception |
| 40 | + { |
| 41 | + String json = MAPPER.writeValueAsString(new ReadOnly5049Pojo("hello", "world")); |
| 42 | + //System.err.println("JSON/pojo: "+json); |
| 43 | + ReadOnly5049Pojo pojo = MAPPER.readerFor(ReadOnly5049Pojo.class).readValue(json); |
| 44 | + assertNotNull(pojo); |
| 45 | + assertNull(pojo.a); |
| 46 | + assertNull(pojo.b); |
| 47 | + } |
| 48 | + |
| 49 | + @JacksonTestFailureExpected |
| 50 | + @Test |
| 51 | + void testRoundtripRecord() throws Exception |
| 52 | + { |
| 53 | + // json = MAPPER.writeValueAsString(new ReadOnly5049Record("hello", "world")); |
| 54 | + String json = "{\"a\":\"hello\",\"b\":\"world\"}"; |
| 55 | + ReadOnly5049Record record = MAPPER.readValue(json, ReadOnly5049Record.class); |
| 56 | + assertNotNull(record); |
| 57 | + assertNull(record.a()); |
| 58 | + assertNull(record.b()); |
| 59 | + } |
| 60 | +} |
0 commit comments