Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append to array in overridden YAML file (Spring)?

I have a JAR which contains an application.yml. That YAM file contains an array, e.g.:

things:
  - name: one
    color: red

I need to adjust this array at runtime by adding an additional application.yml file in the same directory as the JAR. However, I'm not sure how to append to the array, because the follow config seems to replace the config in the built-in YAML file in the JAR:

things:
  - name: red
    color: blue

In the end, during runtime, I need this:

things:
  - name: one
    color: red
  - name: two
    color: blue
like image 487
Josh M. Avatar asked Aug 11 '19 17:08

Josh M.


1 Answers

There looks like a similar question was asked. Doesn't sound like it's supported. Looking through docs, I've found is some older docs show it's not supported.

When a collection is specified in multiple profiles, the one with highest priority is used (and only that one):

foo:
  list:
    - name: my name
      description: my description
    - name: another name
      description: another description
---
spring:
  profiles: dev
foo:
  list:
     - name: my another name

In the example above, considering that the dev profile is active, FooProperties.list will contain one MyPojo entry (with name “my another name” and description null).

A newer 2.6.0 snapshot doc mentions the same but seems to encourage you name your keys as their own objects. You might not have access to the beans bringing them in but it might be good to structure others in this way if you can.

Consider the following configuration:

my:
  map:
    key1:
      name: "my name 1"
      description: "my description 1"
---
spring:
  config:
    activate:
      on-profile: "dev"
my:
  map:
    key1:
      name: "dev name 1"
    key2:
      name: "dev name 2"
      description: "dev description 2"

If the dev profile is not active, MyProperties.map contains one entry with key key1 (with a name of my name 1 and a description of my description 1). If the dev profile is enabled, however, map contains two entries with keys key1 (with a name of dev name 1 and a description of my description 1) and key2 (with a name of dev name 2 and a description of dev description 2).

like image 66
Grant Lindsey Avatar answered Nov 05 '22 01:11

Grant Lindsey