Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#851 Ensure that CompatibleFieldSerializer sets fields with default values to null #854

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ public T read (Kryo kryo, Input input, Class<? extends T> type) {
continue;
}
if (registration == null) {
try {
if (cachedField != null && cachedField.canBeNull) {
cachedField.field.set(object, null);
}
} catch (IllegalAccessException e) {
}
if (chunked) inputChunked.nextChunk();
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,20 @@ void testClassWithGenericField () {
roundTrip(9, new ClassWithGenericField<>(1));
}

// https://github.com/EsotericSoftware/kryo/issues/851
@Test
void testClassWithDefaultValueField () {
CompatibleFieldSerializer.CompatibleFieldSerializerConfig config = new CompatibleFieldSerializer.CompatibleFieldSerializerConfig();
config.setChunkedEncoding(true);
config.setReadUnknownFieldData(true);
kryo.setDefaultSerializer(new CompatibleFieldSerializerFactory(config));
kryo.register(ClassWithDefaultValueField.class);

final ClassWithDefaultValueField o = new ClassWithDefaultValueField();
o.setValue(null);
roundTrip(10, o);
}

public static class TestClass {
public String text = "something";
public int moo = 120;
Expand Down Expand Up @@ -809,4 +823,29 @@ public boolean equals (Object o) {
return Objects.equals(value, that.value);
}
}

public static class ClassWithDefaultValueField {

private Integer value = 10;

public Integer getValue () {
return value;
}

public void setValue (Integer value) {
this.value = value;
}

@Override
public boolean equals (Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ClassWithDefaultValueField that = (ClassWithDefaultValueField)o;
return Objects.equals(value, that.value);
}
}
}