Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Generated map marshallers always use __md$2 variable when initialising BaseMarshallerDelegate #418

Closed
ryanemerson opened this issue Feb 17, 2025 · 0 comments · Fixed by #419
Assignees

Comments

@ryanemerson
Copy link
Contributor

Marshallers generated for classes with Map @ProtoField definitions always try to initialize the BaseMarshallerDelegate of the Map's value type to the _md$2 variable. Consequently, if there already exists a @ProtoField(2) definition for a type that also requires a BaseMarshallerDelegate, a ClassCastException will be thrown at runtime when the non-null _md$2 delegate is passed to a subsequent writeNestedMessage call.

Example definition:

   @ProtoField(2)
   BigInteger bigInteger;

   @ProtoField(3)
   Map<Integer, UUID> uuidMap;

   @ProtoFactory
   public MapOverlappingMarshallerDelegate(BigInteger bigInteger, Map<Integer, UUID> uuidMap) {
      this.bigInteger = bigInteger;
      this.uuidMap = uuidMap;
   }

Generated code:

   @Override
   public void write(org.infinispan.protostream.ProtobufTagMarshaller.WriteContext $1, org.infinispan.protostream.integrationtests.processor.marshaller.model.MapOverlappingMarshallerDelegate $2) throws java.io.IOException {
      org.infinispan.protostream.impl.TagWriterImpl $out = (org.infinispan.protostream.impl.TagWriterImpl) $1.getWriter();
      final org.infinispan.protostream.integrationtests.processor.marshaller.model.MapOverlappingMarshallerDelegate o = (org.infinispan.protostream.integrationtests.processor.marshaller.model.MapOverlappingMarshallerDelegate) $2;
      {
         final java.math.BigInteger __v$2 = (java.math.BigInteger) o.bigInteger;
         if (__v$2 != null) {
            if (__md$2 == null) __md$2 = ((org.infinispan.protostream.impl.SerializationContextImpl) $1.getSerializationContext()).getMarshallerDelegate(java.math.BigInteger.class);
            writeNestedMessage(__md$2, $out, 2, __v$2);
         }
      }
      {
         final java.util.Map<java.lang.Integer, java.util.UUID> __c$3 = o.uuidMap;
         if (__c$3 != null) 
            for (java.util.Iterator<java.util.Map.Entry<java.lang.Integer, java.util.UUID>> it = __c$3.entrySet().iterator(); it.hasNext(); ) {
               final java.util.Map.Entry<java.lang.Integer, java.util.UUID> __v$3 = it.next();
               try (NestedWriter $n = new NestedWriter($1, 3)) {
                  $out = $n.getWriter();
                  $out.writeInt32(1, __v$3.getKey().intValue());
                  {
                     if (__md$2 == null) __md$2 = ((org.infinispan.protostream.impl.SerializationContextImpl) $1.getSerializationContext()).getMarshallerDelegate(java.util.UUID.class);
                     writeNestedMessage(__md$2, $out, 2, __v$3.getValue());
                  }
               }
               $out = (org.infinispan.protostream.impl.TagWriterImpl) $1.getWriter();
            }
      }
   }

Exception:

java.lang.ClassCastException: class java.util.UUID cannot be cast to class java.math.BigInteger (java.util.UUID and java.math.BigInteger are in module java.base of loader 'bootstrap')
@ryanemerson ryanemerson self-assigned this Feb 17, 2025
ryanemerson added a commit to ryanemerson/protostream that referenced this issue Feb 17, 2025
ryanemerson added a commit to ryanemerson/protostream that referenced this issue Feb 17, 2025
ryanemerson added a commit to ryanemerson/protostream that referenced this issue Feb 18, 2025
@ryanemerson ryanemerson changed the title Generated marshallers always use __md$2 variable when initialising BaseMarshallerDelegate Generated map marshallers always use __md$2 variable when initialising BaseMarshallerDelegate Feb 18, 2025
tristantarrant pushed a commit that referenced this issue Feb 18, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
1 participant