Skip to content

[Java] Custom enum as Map key fails with XLANG despite registration #2104

Open
@LouisLou2

Description

@LouisLou2

Search before asking

  • I had searched in the issues and found no similar issues.

Version

0.10.0

Component(s)

Java

Minimal reproduce step

package org.apache.fury;

import org.apache.fury.config.Language;

import java.util.HashMap;
import java.util.Map;

public class Main {
  enum Kind {
    X, Y, Z
  }

  public static class SomeClass {
    Map<Kind, Integer> map = new HashMap<>();
    private Kind kind;
  }

  public static void main(String[] args) {
    Fury fury = Fury.builder()
            .withLanguage(Language.XLANG)
            .withRefTracking(true)
            .ignoreBasicTypesRef(true)
            .ignoreTimeRef(true)
            .ignoreStringRef(false)
            .build();

    fury.register(Kind.class, "MyKind");
    fury.register(SomeClass.class, "MySomeClass");

    SomeClass someClass = new SomeClass();
    byte[] bytes = fury.serialize(someClass);
    Object obj = fury.deserialize(bytes);
    System.out.println(obj);
  }
}

What did you expect to see?

I expected the serialization and deserialization to complete successfully since I explicitly registered both Kind enum class and SomeClass using fury.register().

What did you see instead?

I received the following exception indicating that the class is not registered, despite having registered it:

Exception in thread "main" org.apache.fury.exception.InsecureException: class org.apache.fury.Main$Kind is not registered, please check whether it's the type you want to serialize or a **vulnerability**. If safe, you should invoke `Fury#register` to register class, which will have better performance by skipping classname serialization. If your env is 100% secure, you can also avoid this exception by disabling class registration check using `FuryBuilder#requireClassRegistration(false)`
	at org.apache.fury.resolver.ClassResolver.createSerializer(ClassResolver.java:1235)
	at org.apache.fury.resolver.ClassResolver.getOrUpdateClassInfo(ClassResolver.java:1197)
	at org.apache.fury.resolver.ClassResolver.getSerializer(ClassResolver.java:806)
	at org.apache.fury.type.GenericType.getSerializer(GenericType.java:187)
	at org.apache.fury.serializer.collection.AbstractMapSerializer.xwriteElements(AbstractMapSerializer.java:469)
	at org.apache.fury.serializer.collection.AbstractMapSerializer.xwrite(AbstractMapSerializer.java:169)
	at org.apache.fury.Fury.xwriteRef(Fury.java:561)
	at org.apache.fury.serializer.StructSerializer.xwrite(StructSerializer.java:166)
	at org.apache.fury.Fury.xwriteRef(Fury.java:551)
	at org.apache.fury.Fury.serialize(Fury.java:325)
	at org.apache.fury.Fury.serialize(Fury.java:276)
	at org.apache.fury.Main.main(Main.java:35)

Anything Else?

I found that the error occurs in AbstractMapSerializer.java at line 469 where it's trying to get the serializer for the map key:

Serializer keySerializer = keyGenericType.getSerializer(fury.getClassResolver());

I've verified that when using standard Java types (like String) as Map keys with the same configuration, it works correctly. The issue seems specific to using custom enum types as Map keys in XLANG mode, despite proper registration.

I've searched through existing issues but couldn't find any addressing this specific problem. Is there a limitation with custom enum types as Map keys in XLANG mode, or am I missing something in my registration process?

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions