title: merging yaml across multiple spring profiles
body: <p>I offer one solution at the end of this post but what is the preferred way to do this? My use case is to read yaml across multiple profiles and merge to a single configuration that maps to a java class. </p>
  
  <p>A sample project describing this issue is at <a href="https://github.com/balamuru/yaml-pojo-merge" rel="nofollow noreferrer">https://github.com/balamuru/yaml-pojo-merge</a></p>
  
  <p>eg. Consider the following POJO</p>
  
  <pre><code>public class Credentials {
      private String user;
      private String password;
  }
  </code></pre>
  
  <p>This can be composed into a collection in several ways</p>
  
  <p>a collection of Credential objects
  direct descriptive instances of Credential objects</p>
  
  <pre><code>public class AllCredentials {
      private List&lt;Credentials&gt; credentials; //won't merge (will override instead) because spring doesn't know which original Credential list item to over-ride when an override value is supplied in the profile (for instance , if only one password is supplied, spring doesn't know whether to update list item 1 or 2 etc)
  }
  public class FamilyCredentials {
      private Credentials husband; //the corresponding yaml is determinate - and spring can determine exactly what field to update
      private Credentials wife;
  } 
  </code></pre>
  
  <p>The corresponding representations in the default and profile specific YML files follows</p>
  
  <pre><code>com.foo.bar.allcreds:
    credentials:
      - user: bill
        password: bi123
      - user: hilary
        password: h123
  
  com.foo.bar.famcreds:
    husband:
      user: bill
      password: bi123
    wife:
      user: hilary
      password: h123
  
  ---
  spring.profiles: prod
  com.foo.bar.allcreds:
    credentials:
      - password: monic@
      - password: my_em@ilz
  
  com.foo.bar.famcreds:
    husband:
      password: monic@
    wife:
      password: my_em@ilz    
  </code></pre>
  
  <p>It is observed that in the case of List, Spring cannot determine which credential instance to update the password for (even if the user name is suppllied) since it is provided a list of items, and there is no guarantee that all items are provided, or (even in order for that matter)</p>
  
  <p>OTOH, the FamilyCredentials YAML resolves perfectly since the paths to the password fields are well formed and determinate.</p>
  
  <p>The solution, if dealing with unbounded data structures, is to employ something that allows us to qualify the segment being modified i.e. a hashmap. Here is a data structure that will work (the keys "mr_prez" and "first_lady" enable precise resolution of the qualified yaml properties that are updated).</p>
  
  <p>The test cases in this project bear this out.</p>
  
  <pre><code>public class AllCredentialsMap {
      private Map&lt;String, Credentials&gt; credentials;
  and the corresponding yaml segments
  
  com.foo.bar.allcredsmap:
    credentials:
      mr_prez:
        user: bill
        password: bi123
      first_lady:
        user: hilary
        password: h123
  
  ---
  spring.profiles: prod
  
  com.foo.bar.allcredsmap:
    credentials:
      mr_prez:
        password: monic@
      first_lady:
        password: my_em@ilz  
  </code></pre>