Skip to content

Commit

Permalink
fix(java): NonExistentEnum on mode serializeEnumByName (#1904)
Browse files Browse the repository at this point in the history

## What does this PR do?
Handle NonExistentEnum on mode serializeEnumByName by returning UNKNOWN.
since there are no relevancy anymore by using enum ordinal.

## Related issues

<!--
Is there any related issue? Please attach here.

- #xxxx0
- #xxxx1
- #xxxx2
-->

## 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
orisgarno authored Oct 24, 2024
1 parent 3fb0797 commit 39b919e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.fury.resolver.ClassInfoHolder;
import org.apache.fury.resolver.ClassResolver;
import org.apache.fury.resolver.MetaContext;
import org.apache.fury.resolver.MetaStringResolver;
import org.apache.fury.resolver.RefResolver;
import org.apache.fury.serializer.NonexistentClass.NonexistentEnum;
import org.apache.fury.type.Descriptor;
Expand Down Expand Up @@ -226,14 +227,21 @@ public Object read(MemoryBuffer buffer) {

public static final class NonexistentEnumClassSerializer extends Serializer {
private final NonexistentEnum[] enumConstants;
private final MetaStringResolver metaStringResolver;

public NonexistentEnumClassSerializer(Fury fury) {
super(fury, NonexistentEnum.class);
metaStringResolver = fury.getMetaStringResolver();
enumConstants = NonexistentEnum.class.getEnumConstants();
}

@Override
public Object read(MemoryBuffer buffer) {
if (fury.getConfig().serializeEnumByName()) {
metaStringResolver.readMetaStringBytes(buffer);
return NonexistentEnum.UNKNOWN;
}

int ordinal = buffer.readVarUint32Small7();
if (ordinal >= enumConstants.length) {
ordinal = enumConstants.length - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ public void testNonexistentEnum(boolean scopedMetaShare) {
assertEquals(o, NonexistentClass.NonexistentEnum.V1);
}

@Test(dataProvider = "scopedMetaShare")
public void testNonexistentEnum_AsString(boolean scopedMetaShare) {
Fury fury =
furyBuilder(scopedMetaShare)
.withDeserializeNonexistentClass(true)
.serializeEnumByName(true)
.build();
String enumCode = ("enum TestEnum {" + " A, B" + "}");
Class<?> cls = JaninoUtils.compileClass(getClass().getClassLoader(), "", "TestEnum", enumCode);
Object c = cls.getEnumConstants()[1];
assertEquals(c.toString(), "B");
byte[] bytes = fury.serialize(c);
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
Fury fury2 =
furyBuilder(scopedMetaShare)
.withDeserializeNonexistentClass(true)
.serializeEnumByName(true)
.build();
Object o = fury2.deserialize(bytes);
assertEquals(o, NonexistentClass.NonexistentEnum.UNKNOWN);
}

@Test(dataProvider = "scopedMetaShare")
public void testNonexistentEnumAndArrayField(boolean scopedMetaShare) throws Exception {
String enumStructCode1 =
Expand Down

0 comments on commit 39b919e

Please sign in to comment.