From 0b2fea2575652b083f7795721f258029369a6a08 Mon Sep 17 00:00:00 2001 From: Thomas Heigl Date: Fri, 3 Sep 2021 13:13:39 +0200 Subject: [PATCH] #851 Ensure that `CompatibleFieldSerializer` sets fields with default values to `null` --- .../CompatibleFieldSerializer.java | 11 ++++++ .../CompatibleFieldSerializerTest.java | 39 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java b/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java index 3c58e2d3d..a57a79998 100644 --- a/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java +++ b/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java @@ -142,6 +142,7 @@ public T read (Kryo kryo, Input input, Class type) { continue; } if (registration == null) { + setFieldToNull(cachedField, object); if (chunked) inputChunked.nextChunk(); continue; } @@ -242,6 +243,16 @@ else if (compare > 0) return fields; } + private void setFieldToNull (CachedField field, Object obj) { + if (field instanceof ReflectField && field.getCanBeNull()) { + try { + ((ReflectField)field).set(obj, null); + } catch (IllegalAccessException e) { + throw new KryoException("Error accessing field: " + field, e); + } + } + } + public CompatibleFieldSerializerConfig getCompatibleFieldSerializerConfig () { return config; } diff --git a/test/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializerTest.java b/test/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializerTest.java index 528bf3f14..48f3e684c 100644 --- a/test/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializerTest.java +++ b/test/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializerTest.java @@ -515,6 +515,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; @@ -811,4 +825,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); + } + } }