julianhowarth commented on issue #2275:
URL: https://github.com/apache/fory/issues/2275#issuecomment-3259274782

   Sorry for the delay - I now have a reproducer that you can hopefully dig 
into.
   
   Here's a class for the code we get generated from Immutables:
   ```java
   import com.google.common.base.MoreObjects;
   import java.io.ObjectStreamException;
   import java.util.Objects;
   import javax.annotation.CheckReturnValue;
   import javax.annotation.Nullable;
   import javax.annotation.ParametersAreNonnullByDefault;
   import javax.annotation.concurrent.Immutable;
   
   @ParametersAreNonnullByDefault
   @CheckReturnValue
   @Immutable
   public final class Bar {
     private final String value;
     private final int hashCode;
   
     private Bar(String value) {
       this.value = (String) Objects.requireNonNull(value, "value");
       this.hashCode = this.computeHashCode();
     }
   
     private Bar(Bar original, String value) {
       this.value = value;
       this.hashCode = this.computeHashCode();
     }
   
     public String getValue() {
       return this.value;
     }
   
     public final Bar withValue(String value) {
       String newValue = (String) Objects.requireNonNull(value, "value");
       return this.value.equals(newValue) ? this : new Bar(this, newValue);
     }
   
     public boolean equals(@Nullable Object another) {
       if (this == another) {
         return true;
       } else {
         return another instanceof Bar && this.equalTo(0, (Bar) another);
       }
     }
   
     private boolean equalTo(int synthetic, Bar another) {
       return this.hashCode != another.hashCode ? false : 
this.value.equals(another.value);
     }
   
     public int hashCode() {
       return this.hashCode;
     }
   
     private int computeHashCode() {
       int h = 5381;
       h += (h << 5) + this.value.hashCode();
       return h;
     }
   
     public String toString() {
       return MoreObjects.toStringHelper("RealmName")
           .omitNullValues()
           .add("value", this.value)
           .toString();
     }
   
     public static Bar of(String value) {
       return new Bar(value);
     }
   
     private Object readResolve() throws ObjectStreamException {
       return new Bar(this, this.value);
     }
   }
   ```
   I then create a wrapper for it:
   ```java  
   record Foo(Bar bar) {}
   ```
   
   I write out a serialized file with:
   ```java
   void out() throws IOException {
       var fory = buildFory();
   
       var out = new File("out.fory");
       var outputStream = new BufferedOutputStream(new FileOutputStream(out));
   
       var item = new Foo(Bar.of("some-bar"));
   
       System.out.println(Instant.now() + " - writing data);
       outputStream.write(fory.serializeJavaObject(item));
       outputStream.flush();
       outputStream.close();
   
       System.out.println(Instant.now() + " - done");
   }
   
    private static Fory buildFory() {
       return Fory.builder()
           .withLanguage(Language.JAVA)
           .requireClassRegistration(false)
           .withCompatibleMode(CompatibleMode.COMPATIBLE)
           .build();
     }
   ```
   I then attempt to read it in with:
   ```java
   void in() throws IOException {
       var fory = buildFory();
   
       var in = new File("out.fory");
   
       System.out.println(Instant.now());
   
       var inputStream = new BufferedInputStream(new FileInputStream(in));
       var res = fory.deserializeJavaObject(inputStream.readAllBytes(), 
Foo.class);
   
       System.out.println(Instant.now() + " - loaded " + res);
   }
   ```
   and at this point I get the exception:
   ```
   java.lang.NullPointerException: Cannot read field "serializer" because 
"classInfo" is null
   
        at 
org.apache.fory.resolver.ClassResolver.getOrUpdateClassInfo(ClassResolver.java:1322)
        at 
org.apache.fory.resolver.ClassResolver.readClassInfoWithMetaShare(ClassResolver.java:1568)
        at 
org.apache.fory.resolver.ClassResolver.readClassInfo(ClassResolver.java:1879)
        at 
com.endeavorstreaming.vesper.content_engine.common.utils.SerializationTest_FooForyCodecMetaShared0_0.read(SerializationTest_FooForyCodecMetaShared0_0.java:52)
        at org.apache.fory.Fory.readDataInternal(Fory.java:1055)
        at org.apache.fory.Fory.deserializeJavaObject(Fory.java:1223)
        at org.apache.fory.Fory.deserializeJavaObject(Fory.java:1200)
        at ...SerializationTest.in(SerializationTest.java:84)
   ```
   Now what is interesting is if I try and do the read and write in a single 
operation, then all is fine - the following runs without problem:
   ```java
   void inOut() throws IOException {
   
       var fory = buildFory();
   
       var out = new File("out.fory");
       var outputStream = new BufferedOutputStream(new FileOutputStream(out));
   
       var item = new Foo(Bar.of("some-bar"));
   
       System.out.println(Instant.now() + " - writing data );
       outputStream.write(fory.serializeJavaObject(item));
       outputStream.flush();
       outputStream.close();
   
       System.out.println(Instant.now() + " - done");
   
       var in = new File("out.fory");
   
       System.out.println(Instant.now());
   
       var inputStream = new BufferedInputStream(new FileInputStream(in));
       var res = fory.deserializeJavaObject(inputStream.readAllBytes(), 
Foo.class);
   
       System.out.println(Instant.now() + " - loaded " + res.bar());
   ```
   Also, if I replace the `Bar` class (the immutable) with another record 
containing just a String, then again, everything works as expected.


-- 
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