Skip to content

Commit 6777448

Browse files
committed
Only avoid Records fields detection for deserialization.
1 parent 8040e2a commit 6777448

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ protected void collectAll()
440440

441441
// 15-Jan-2023, tatu: [databind#3736] Let's avoid detecting fields of Records
442442
// altogether (unless we find a good reason to detect them)
443-
if (!isRecordType()) {
443+
// 17-Apr-2023: Need Records' fields for serialization for cases like [databind#3895] & [databind#3628]
444+
if (!isRecordType() || _forSerialization) {
444445
_addFields(props); // note: populates _fieldRenameMappings
445446
}
446447
_addMethods(props);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)