Skip to content

Commit

Permalink
fix(java): fix enum abstract field serialization (#1765)
Browse files Browse the repository at this point in the history
## What does this PR do?

 fix enum abstract field serialization

## Related issues

Closes #1764 

## Does this PR introduce any user-facing change?

<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fury/issues/new/choose) describing the
need to do so and update the document if necessary.
-->

- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?


## Benchmark

<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
  • Loading branch information
chaokunyang authored Jul 26, 2024
1 parent 4f24bd4 commit 40697ed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ public Class<? extends Serializer> getSerializerClass(Class<?> cls) {
}

public Class<? extends Serializer> getSerializerClass(Class<?> cls, boolean codegen) {
if (ReflectionUtils.isAbstract(cls) || cls.isInterface()) {
if (!cls.isEnum() && (ReflectionUtils.isAbstract(cls) || cls.isInterface())) {
throw new UnsupportedOperationException(
String.format("Class %s doesn't support serialization.", cls));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@

import static org.testng.Assert.*;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.fury.Fury;
import org.apache.fury.FuryTestBase;
import org.apache.fury.codegen.JaninoUtils;
import org.apache.fury.config.CompatibleMode;
import org.apache.fury.config.FuryBuilder;
import org.apache.fury.config.Language;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -60,15 +63,13 @@ public void testEnumSerialization(boolean referenceTracking, Language language)
.requireClassRegistration(false);
Fury fury1 = builder.build();
Fury fury2 = builder.build();
assertEquals(EnumSerializerTest.EnumFoo.A, serDe(fury1, fury2, EnumSerializerTest.EnumFoo.A));
assertEquals(EnumSerializerTest.EnumFoo.B, serDe(fury1, fury2, EnumSerializerTest.EnumFoo.B));
assertEquals(
EnumSerializerTest.EnumSubClass.A, serDe(fury1, fury2, EnumSerializerTest.EnumSubClass.A));
assertEquals(
EnumSerializerTest.EnumSubClass.B, serDe(fury1, fury2, EnumSerializerTest.EnumSubClass.B));
assertEquals(EnumFoo.A, serDe(fury1, fury2, EnumFoo.A));
assertEquals(EnumFoo.B, serDe(fury1, fury2, EnumFoo.B));
assertEquals(EnumSubClass.A, serDe(fury1, fury2, EnumSubClass.A));
assertEquals(EnumSubClass.B, serDe(fury1, fury2, EnumSubClass.B));
}

@Test()
@Test
public void testEnumSerializationUnexistentEnumValueAsNull() {
String enumCode2 = "enum TestEnum2 {" + " A;" + "}";
String enumCode1 = "enum TestEnum2 {" + " A, B" + "}";
Expand All @@ -93,4 +94,26 @@ public void testEnumSerializationUnexistentEnumValueAsNull() {
byte[] bytes = furySerialization.serialize(cls1.getEnumConstants()[1]);
Object data = furyDeserialize.deserialize(bytes);
}

@Data
@AllArgsConstructor
static class EnumSubclassFieldTest {
EnumSubClass subEnum;
}

@Test(dataProvider = "enableCodegen")
public void testEnumSubclassField(boolean enableCodegen) {
serDeCheck(
builder().withCodegen(enableCodegen).build(), new EnumSubclassFieldTest(EnumSubClass.B));
}

@Test(dataProvider = "scopedMetaShare")
public void testEnumSubclassFieldCompatible(boolean scopedMetaShare) {
serDeCheck(
builder()
.withScopedMetaShare(scopedMetaShare)
.withCompatibleMode(CompatibleMode.COMPATIBLE)
.build(),
new EnumSubclassFieldTest(EnumSubClass.B));
}
}

0 comments on commit 40697ed

Please sign in to comment.