javajenkinsyamljenkins-pipelinesnakeyaml

Why Jenkins 'readYaml' pipeline utility step is ignoring duplicate keys in YAML?


I am reading a YAML file using the Jenkins pipeline utility step readYaml:

key1:
  subkey1: val1
key2:
  subkey2: val2
key2:
  subkey2: val3

I noticed that the step is ignoring the duplicate keys in the YAML and throws no exception. When I looked into the source code I could not see any relevant configuration.

This plugin is using the snakeyaml library. By default the snakeyaml library allows duplicate keys as per the LoaderOptions class.

If the Jenkins pipeline utility step is not changing the LoaderOptions config then why the duplicate key is being ignored?


Solution

  • The YAML specification does not allow multiple keys.

    The snakeyaml loader option allowDuplicateKeys=true means it will silently ignore any duplicate keys and not throw an exception.

    See: https://github.com/snakeyaml/snakeyaml/blob/c98ffba9cd065d1ead94c9ec580d8b5a5966c9d3/src/main/java/org/yaml/snakeyaml/constructor/SafeConstructor.java#L124