Skip to content

Commit

Permalink
Only avoid Records fields detection for deserialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
yihtserns committed Apr 26, 2023
1 parent 8040e2a commit 6777448
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 6777448

Please sign in to comment.