title: Axon query returning list with Axon Server
body: <p>We have an application where we're trying to move from using just the Axon Framework to also using the Axon Server. We're experiencing an issue where query responses, which are multiple instances of a type, are failing to be properly transported. The issue as detailed below does not occur when the server is not being used.</p>
  
  <p>We're sending a query and expecting a list of a specific type as the response.</p>
  
  <pre><code>queryGateway.query(new MyQuery(...), ResponseTypes.multipleInstancesOf(MyResponse.class));
  
  // elsewhere
  @QueryHandler
  public List&lt;MyResponse&gt; handle(MyQueryquery) {
    return List.of(new MyResponse(...));
  }
  </code></pre>
  
  <p>When run without the server this works fine. Of interest is that <code>MultipleInstancesResponseType#convert(Object)</code> is called once, and the parameter is the <code>List&lt;MyResponse&gt;</code> instance. No conversion is necessary here.</p>
  
  <p>When run with the server <code>MultipleInstancesResponseType#convert(Object)</code> is called twice. The first time, as above, the parameter is the expected <code>List&lt;MyResponse&gt;</code>, and again, no conversion is necessary. However, <code>MultipleInstancesResponseType#convert(Object)</code> is then called again, with the parameter being <code>List&lt;LinkedHashMap&gt;</code>. This requires conversion, which fails, because <code>List&lt;MyResponse&gt;</code> can't be converted to <code>List&lt;MyResponse&gt;</code>.</p>
  
  <p>I see both <code>convert</code> calls in my debugger because in my test the same JVM is both making and handling the query.</p>
  
  <p>I believe what is happening is that the query handler is returning the <code>List&lt;MyResponse&gt;</code>and the framework is calling <code>MultipleInstancesResponseType#convert(Object)</code> as per normal. The response is then serialized (gRPC?) and sent to the server, which then sends the response to the original caller. The original caller then tries to convert the payload, and fails. So, somewhere in the gRPC serialization/deserialization process, some type information has been lost.</p>
  
  <p>The net result is that the original caller gets an <code>IllegalArgumentException</code> stating</p>
  
  <blockquote>
    <p>Retrieved response [class java.util.ArrayList] is not convertible to a List of the expected response type [class MyResponse]</p>
  </blockquote>
  
  <p>Setting a breakpoint on the <code>GrpcPayloadSerializer</code> shows that the response payload, at the time it's serialized to be sent to the server, is correctly a <code>List&lt;MyResponse&gt;</code>.</p>
  
  <p>The <a href="https://docs.axoniq.io/reference-guide/implementing-domain-logic/query-handling/handling-queries#supported-multiple-instances-return-values" rel="nofollow noreferrer">documentation</a> seems to be saying clearly that <code>List&lt;of-something&gt;</code> is a supported query handler return type.</p>