Skip to content

Commit fdee4a0

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ 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+
if (!isRecordType() || _forSerialization) {
444444
_addFields(props); // note: populates _fieldRenameMappings
445445
}
446446
_addMethods(props);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 RecordIgnoreInterfaceGetter3628Test extends BaseMapTest {
10+
11+
interface InterfaceWithGetter {
12+
13+
String getId();
14+
15+
String getName();
16+
17+
int getCount();
18+
}
19+
20+
@JsonPropertyOrder({"id", "name", "count"}) // easier to assert when JSON field ordering is always the same
21+
record RecordWithInterfaceWithGetter(String name) implements InterfaceWithGetter {
22+
23+
@Override
24+
public String getId() {
25+
return "ID:" + name;
26+
}
27+
28+
@Override
29+
public String getName() {
30+
return name;
31+
}
32+
33+
@Override
34+
public int getCount() {
35+
return 999;
36+
}
37+
}
38+
39+
private final ObjectMapper MAPPER = newJsonMapper();
40+
41+
public void testSerializeIgnoreInterfaceGetter_WithoutUsingVisibilityConfig() throws Exception {
42+
String json = MAPPER.writeValueAsString(new RecordWithInterfaceWithGetter("Bob"));
43+
44+
assertEquals("{\"id\":\"ID:Bob\",\"name\":\"Bob\",\"count\":999}", json);
45+
}
46+
47+
public void testSerializeIgnoreInterfaceGetter_UsingVisibilityConfig() throws Exception {
48+
MAPPER.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
49+
MAPPER.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
50+
51+
String json = MAPPER.writeValueAsString(new RecordWithInterfaceWithGetter("Bob"));
52+
53+
assertEquals("{\"name\":\"Bob\"}", json);
54+
}
55+
}

0 commit comments

Comments
 (0)