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

Demarshalling doesn't work for collections of generic objects #394

Closed
Thomas-TeS opened this issue Jan 3, 2025 · 3 comments · Fixed by #401
Closed

Demarshalling doesn't work for collections of generic objects #394

Thomas-TeS opened this issue Jan 3, 2025 · 3 comments · Fixed by #401
Assignees

Comments

@Thomas-TeS
Copy link

I've found a problem with the protostream marshallers. The following class generates a compiler error in its marshaller:

public class DTO {
  // other fields

  private Set<Pair<String, String>> restrictions;

  // other getters

  @ProtoField(number = 12, collectionImplementation = HashSet.class)
  public Set<Pair<String, String>> getRestrictions() {
     return restrictions;
  }
}

Marshaller method (Version 5.x):

public void write(WriteContext $1,  DTO $2) {
      // writing of other fields is ok
      {
         final java.util.Collection<Pair> __c$13 = o.getRestrictions(); /* <= this does not work because Pair is not parameterized 
                                                           with <String, String> while the method o.getRestrictions() returns a Set<Pair<String, String>> */
         if (__c$13 != null) 
            for (java.util.Iterator<Pair> it = __c$13.iterator(); it.hasNext(); ) {
               final Pair __v$13 = it.next();
               {
                  if (__md$13 == null) __md$13 = ((org.infinispan.protostream.impl.SerializationContextImpl) $1.getSerializationContext()).getMarshallerDelegate(Pair.class);
                  writeNestedMessage(__md$13, $out, 13, __v$13);
               }
            }
      }
}

This only seems to pose a problem since Protostream 5.x because in version 4.x it worked because the generated write-method did not use a parameterized collection. Instead it contained a loop over all elements of the collection like this:
Marshaller method (Version 4.x):

{
            final java.util.Collection __c$13 = o.getRestrictions();
            if (__c$13 != null) for (java.util.Iterator it = __c$13.iterator(); it.hasNext(); ) {
                final Pair __v$13 = (Pair) it.next();
                {
                    if (__md$13 == null)
                        __md$13 = ((org.infinispan.protostream.impl.SerializationContextImpl) $1.getSerializationContext()).getMarshallerDelegate(Pair.class);
                    writeNestedMessage(__md$13, $1, 13, __v$13);
                }
            }
        }
@tristantarrant tristantarrant self-assigned this Jan 7, 2025
@tristantarrant
Copy link
Member

I'm working on adding better support for parameterized types. I hope to have something in the next week

@tristantarrant
Copy link
Member

@Thomas-TeS how are you annotating the Pair class ?

@Thomas-TeS
Copy link
Author

Hi @tristantarrant, the class Pair itself does not contain annotations. We wrote an adapter for it:

@ProtoAdapter(Pair.class)
public class PairAdapter {
	
	@ProtoFactory
	public Pair<?, ?> create(WrappedMessage left, WrappedMessage right) {
		return new Pair<>(left.getValue(), right.getValue());
	}
	
	@ProtoField(number = 1)
	public WrappedMessage getLeft(Pair<?, ?> pair) {
		return new WrappedMessage(pair.getLeft());
	}
	
	@ProtoField(number = 2)
	public WrappedMessage getRight(Pair<?, ?> pair) {
		return new WrappedMessage(pair.getRight());
	}
}

tristantarrant added a commit to tristantarrant/protostream that referenced this issue Jan 10, 2025
* A temporary workaround for the issue. Full support for generic
  parameter types will need to be added to type mirroring
ryanemerson pushed a commit that referenced this issue Jan 13, 2025
* A temporary workaround for the issue. Full support for generic
  parameter types will need to be added to type mirroring
tristantarrant added a commit to tristantarrant/protostream that referenced this issue Jan 13, 2025
* A temporary workaround for the issue. Full support for generic
  parameter types will need to be added to type mirroring
ryanemerson pushed a commit that referenced this issue Jan 13, 2025
* A temporary workaround for the issue. Full support for generic
  parameter types will need to be added to type mirroring
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants