title: MapStruct - Map Generic List to Non-Generic List containing different types
body: <p>I am attempting to use MapStruct to convert between 2 internal models. These models are generated from the same specification and represent a large tree-like structure that is constantly being added to. The goal of using MapStruct is to have an efficient, low (non-generated) code way of performing this conversion that stays up to date with additions to the specification. As an example, my models would look like:</p>
  <pre class="lang-java prettyprint-override"><code>package com.mycompany.models.speca;
  
  public class ModelSpecA {
      private String name;
      private int biggestNumberFound;
      private com.mycompany.models.speca.InternalModel internalModel;
      private List&lt;com.mycompany.models.speca.InternalModel&gt; internalModelList;
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>package com.mycompany.models.specb;
  
  public class ModelSpecB {
      private String name;
      private int biggestNumberFound;
      private com.mycompany.models.specb.InternalModel internalModel;
      private List internalModelList;
  }
  </code></pre>
  <p>with all of the expected getters and setters and no-arg constructors.
  MapStruct is able to generate code for the mapping extremely easily with the code looking like:</p>
  <pre class="lang-java prettyprint-override"><code>interface ModelSpecMapper {
      ModelSpecB map(ModelSpecA source);
  }
  </code></pre>
  <p>From unit testing and inspecting the generated code, the mapping is accurate and complete except in one regard: the mapping of the <code>internalModelList</code> member in each class. The generated code looks like the following:</p>
  <pre><code>...
  if (sourceInternalModelList != null) {
      specBTarget.setInternalModelList( specASource.getInternalModelList() );
  }
  ...
  </code></pre>
  <p>I.e. It is mapping from the generic List&lt;com.mycompany.models.speca.InternalModel&gt; to the non-generic List without doing model conversion. This passes at compile time and runtime in unit tests, but will cause errors in later code when we expect to be able to cast to the SpecB version of the model.</p>
  <p>So far, I've investigated if it is possible to force a mapping of the parameterized type in the source to its corresponding type without using expensive reflection operations, which would eliminate the gains from using MapStruct as a solution. This is my first experience with MapStruct, so there may be an obvious solution I am simply unaware of. Adding an explicit mapping is infeasible as I need this to be forward compatible with future additions to the model including new Lists.</p>
  <p>TLDR; How do I use MapStruct to convert the contents of a generic List to a non-generic List? E.g. <code>List&lt;com.mycompany.a.ComplexModel&gt; --&gt; List</code> whose members are of type <code>com.mycompany.b.ComplexModel</code>.</p>