From 329944abbd4a9754b086d5271e2670125bf0d389 Mon Sep 17 00:00:00 2001 From: Sim Yih Tsern Date: Thu, 4 May 2023 06:15:46 +0800 Subject: [PATCH] Only avoid Records fields detection for deserialization. (#3894) --- .../introspect/POJOPropertiesCollector.java | 3 +- .../RecordIgnoreNonAccessorGetterTest.java | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordIgnoreNonAccessorGetterTest.java diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java index fcffc61e1b..fabeb15cbb 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java @@ -440,7 +440,8 @@ protected void collectAll() // 15-Jan-2023, tatu: [databind#3736] Let's avoid detecting fields of Records // altogether (unless we find a good reason to detect them) - if (!isRecordType()) { + // 17-Apr-2023: Need Records' fields for serialization for cases like [databind#3895] & [databind#3628] + if (!isRecordType() || _forSerialization) { _addFields(props); // note: populates _fieldRenameMappings } _addMethods(props); diff --git a/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordIgnoreNonAccessorGetterTest.java b/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordIgnoreNonAccessorGetterTest.java new file mode 100644 index 0000000000..af5077c211 --- /dev/null +++ b/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordIgnoreNonAccessorGetterTest.java @@ -0,0 +1,54 @@ +package com.fasterxml.jackson.databind.records; + +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.BaseMapTest; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class RecordIgnoreNonAccessorGetterTest extends BaseMapTest { + + // [databind#3628] + interface InterfaceWithGetter { + + String getId(); + + String getName(); + } + + @JsonPropertyOrder({"id", "name", "count"}) // easier to assert when JSON field ordering is always the same + record RecordWithInterfaceWithGetter(String name) implements InterfaceWithGetter { + + @Override + public String getId() { + return "ID:" + name; + } + + @Override + public String getName() { + return name; + } + + // [databind#3895] + public int getCount() { + return 999; + } + } + + private final ObjectMapper MAPPER = newJsonMapper(); + + public void testSerializeIgnoreInterfaceGetter_WithoutUsingVisibilityConfig() throws Exception { + String json = MAPPER.writeValueAsString(new RecordWithInterfaceWithGetter("Bob")); + + assertEquals("{\"id\":\"ID:Bob\",\"name\":\"Bob\",\"count\":999}", json); + } + + public void testSerializeIgnoreInterfaceGetter_UsingVisibilityConfig() throws Exception { + MAPPER.setVisibility(PropertyAccessor.GETTER, Visibility.NONE); + MAPPER.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); + + String json = MAPPER.writeValueAsString(new RecordWithInterfaceWithGetter("Bob")); + + assertEquals("{\"name\":\"Bob\"}", json); + } +}