|
| 1 | +package com.fasterxml.jackson.databind.records; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; |
| 4 | +import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
| 5 | +import com.fasterxml.jackson.annotation.PropertyAccessor; |
| 6 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | + |
| 9 | +public class RecordIgnoreNonAccessorGetterTest extends BaseMapTest { |
| 10 | + |
| 11 | + // [databind#3628] |
| 12 | + interface InterfaceWithGetter { |
| 13 | + |
| 14 | + String getId(); |
| 15 | + |
| 16 | + String getName(); |
| 17 | + } |
| 18 | + |
| 19 | + @JsonPropertyOrder({"id", "name", "count"}) // easier to assert when JSON field ordering is always the same |
| 20 | + record RecordWithInterfaceWithGetter(String name) implements InterfaceWithGetter { |
| 21 | + |
| 22 | + @Override |
| 23 | + public String getId() { |
| 24 | + return "ID:" + name; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public String getName() { |
| 29 | + return name; |
| 30 | + } |
| 31 | + |
| 32 | + // [databind#3895] |
| 33 | + public int getCount() { |
| 34 | + return 999; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 39 | + |
| 40 | + public void testSerializeIgnoreInterfaceGetter_WithoutUsingVisibilityConfig() throws Exception { |
| 41 | + String json = MAPPER.writeValueAsString(new RecordWithInterfaceWithGetter("Bob")); |
| 42 | + |
| 43 | + assertEquals("{\"id\":\"ID:Bob\",\"name\":\"Bob\",\"count\":999}", json); |
| 44 | + } |
| 45 | + |
| 46 | + public void testSerializeIgnoreInterfaceGetter_UsingVisibilityConfig() throws Exception { |
| 47 | + MAPPER.setVisibility(PropertyAccessor.GETTER, Visibility.NONE); |
| 48 | + MAPPER.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); |
| 49 | + |
| 50 | + String json = MAPPER.writeValueAsString(new RecordWithInterfaceWithGetter("Bob")); |
| 51 | + |
| 52 | + assertEquals("{\"name\":\"Bob\"}", json); |
| 53 | + } |
| 54 | +} |
0 commit comments