NotLebedev opened a new issue, #3306:
URL: https://github.com/apache/fory/issues/3306

   ### Search before asking
   
   - [x] I had searched in the [issues](https://github.com/apache/fory/issues) 
and found no similar issues.
   
   
   ### Version
   
   Java, all versions up to commit 
[9891bde](https://github.com/apache/fory/commit/9891bdeb599b1b4d3b9960e67c3bf2f04c157f5a)
   
   ### Component(s)
   
   Java
   
   ### Minimal reproduce step
   
   Try to serialize and deserialize `EnumSet` containing the following test enum
   ```java
   enum TestEnum {
        A {
                @Override
                public void foo() {
                        System.out.println("A");
                }
        },
        B,
        C {
                @Override
                public void foo() {
                }
        },
        D;
   
        public void foo() {
                System.out.println("default");
        }
   }
   
   var result = fory.deserializeJavaObjectAndClass(
       fory.serializeJavaObjectAndClass(
           EnumSet.of(TestEnum.A, TestEnum.C, TestEnum.D)
       )
   );
   ```
   
   ### What did you expect to see?
   
   Serialization and deserialization to succeed and result to be the same 
`EnumSet.of(TestEnum.A, TestEnum.C, TestEnum.D)`
   
   ### What did you see instead?
   
   Exception `java.lang.ClassCastException: class TestEnum$1 not an enum`
   
   ### Anything Else?
   
   Exception happens in default EnumSetSerializer 
[here](https://github.com/apache/fory/blob/16a923085a33c36ce11f54fc0036dc370886932b/java/fory-core/src/main/java/org/apache/fory/serializer/collection/CollectionSerializers.java#L672).
   
   Problem is that `Class elemClass = 
fory.getClassResolver().readTypeInfo(buffer).getCls();` returns class of enum 
variant `TestEnum$1` which itself is not an enum (only base TestEnum is). To 
fix this we need to access  enclosing class like this:
   
   ```java
       @Override
       public EnumSet read(MemoryBuffer buffer) {
         Class elemClass = 
fory.getClassResolver().readTypeInfo(buffer).getCls();
         if (!elemClass.isEnum()) {
             elemClass = elemClass.getEnclosingClass();
         }
         EnumSet object = EnumSet.noneOf(elemClass);
         ...
       }
   ```
   
   ### Are you willing to submit a PR?
   
   - [x] I'm willing to submit a PR!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to