Skip to content

YAML loading and serializing does not handle None properly #43694

Closed as not planned

Description

Description of Issue/Question

I am using import_yaml to load a YAML file that has some keys with empty values (Ex.: key:), then use file.serialize to write it to a YAML file again. But all keys that previously had no values, now have a None in place, which is a valid string, but not a valid YAML empty value.

This is causing problems because the program that expects an empty or decimal value is getting a string instead.

Setup

{% import_yaml "defaults.yaml" as defaults %} 

{{ sls }}~config:
  file.serialize:
    - name: /etc/config.yaml
    - show_changes: true
    - formatter: yaml
    - backup: minion
    - dataset: {{ defaults }}

Steps to Reproduce Issue

Can be verified using state.show_sls.

Versions Report

Salt Version:
           Salt: 2017.7.1
 
Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 2.4.2
      docker-py: Not Installed
          gitdb: 0.6.4
      gitpython: 1.0.1
          ioflo: Not Installed
         Jinja2: 2.8
        libgit2: Not Installed
        libnacl: Not Installed
       M2Crypto: Not Installed
           Mako: 1.0.3
   msgpack-pure: Not Installed
 msgpack-python: 0.4.6
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 2.7.12 (default, Nov 19 2016, 06:48:10)
   python-gnupg: Not Installed
         PyYAML: 3.11
          PyZMQ: 15.2.0
           RAET: Not Installed
          smmap: 0.9.0
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.1.4
 
System Versions:
           dist: Ubuntu 16.04 xenial
         locale: UTF-8
        machine: x86_64
        release: 4.4.0-96-generic
         system: Linux
        version: Ubuntu 16.04 xenial

Activity

  1. gtmanfred commented on Sep 22, 2017

    @gtmanfred
    Contributor

    PyYaml converts empty values to None when they are put in the yaml dictionary, and I am not sure how you would differentiate between having None or empty when dumping back to a file.

    I am going to mark this as a bug, but I would highly recommend using file.managed if you and values to remain empty instead of having None specified.

    Thanks,
    Daniel

  2. added
    bug
    Core
    severity-low
    P4
    on Sep 22, 2017
  3. added this to the Approved milestone on Sep 22, 2017
  4. gtmanfred commented on Sep 25, 2017

    @gtmanfred
    Contributor

    Ok, so it is an easy solution to solve.

    diff --git a/salt/serializers/yaml.py b/salt/serializers/yaml.py
    index 5ebc94b..51b0758 100644
    --- a/salt/serializers/yaml.py
    +++ b/salt/serializers/yaml.py
    @@ -98,7 +98,8 @@ Loader.add_multi_constructor(None, Loader.construct_undefined)
    
     class Dumper(BaseDumper):  # pylint: disable=W0232
         '''Overwrites Dumper as not for pollute legacy Dumper'''
    -    pass
    +    def represent_none(self, _):
    +        return self.represent_scalar('tag:yaml.org,2002:null', '')
    
     Dumper.add_multi_representer(type(None), Dumper.represent_none)
     Dumper.add_multi_representer(str, Dumper.represent_str)
    

    I am just not sure if it should be "solved" for everyone by default or not. It might make more sense to set it to 'null'.

    The other problem is if we change this behavior, Then any files dumped from this module won't be readable by salts yaml loader.

    @saltstack/team-core does anyone have any suggestions here?

    Thanks,
    Daniel

  5. norpol commented on Oct 3, 2017

    @norpol

    I have a similar problem. import_yaml ... wrongly escapes the content. So null, becomes None, \n becomes \\n, true becomes True.

    a: null
    b: "null"
    c: 'null
      null
      null'
    

    to

    {'a': None, 'b': 'null', 'c': 'null\\nnull\\nnull'}

    This is appears when I do a file.serialize or file.managed with either dataset or contents.
    I've managed to ?workaround? this problem by passing the import_yaml dict through |yaml in my jinja.

    Example:

     create-json:
       file.serialize:
         - name: /tmp/my.json
         - dataset: {{ myyamldict | yaml }}
         - formatter: json
    
  6. stale commented on Feb 7, 2019

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

    If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

  7. norpol commented on Feb 17, 2019

    @norpol

    I think this is still an issue.

  8. 3 remaining items

  9. garethgreenaway commented on Feb 18, 2019

    @garethgreenaway
    Contributor
  10. baby-gnu commented on Jun 7, 2019

    @baby-gnu
    Contributor

    I have the same issue when using ~ like

    template:
      pkg: ~
  11. terminalmage commented on Jun 7, 2019

    @terminalmage
    Contributor

    A tilde is a null value in YAML. Are you trying to get a literal tilde? If so you'll need to quote it.

    >>> import yaml
    >>> yaml.safe_load('foo: ~')
    {'foo': None}
    >>> yaml.safe_load('foo: "~"')
    {'foo': '~'}
    >>>
  12. baby-gnu commented on Jun 7, 2019

    @baby-gnu
    Contributor

    Hello @terminalmage, I want a None but today that's not what I got.

    Looks like I have an issue with my macros, I import_yaml and call a macro with the value:

    • The value loaded match the is none test
    • The value returned by my macro does not match is none.

    Sorry for the noise, I should have missed something.

  13. stale commented on Jan 8, 2020

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

    If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

  14. added
    Confirmed
    on Jan 8, 2020
  15. stale commented on Jan 8, 2020

    @stale

    Thank you for updating this issue. It is no longer marked as stale.

  16. removed
    P4
    on Jun 3, 2020
  17. dwoz commented on Oct 15, 2026

    @dwoz
    Contributor

    Thanks for the original report. This issue has been open for more than three years without recent activity and carries a severity label below severity-high. As part of a backlog cleanup, we're closing older low- and medium-severity issues so triage can focus on reports against currently supported releases.

    Closing isn't a judgement that the problem wasn't real — only that the codebase has moved through several releases since this was filed, and much of the surrounding code has changed.

    If you — or anyone else — can still reproduce this on the 3008.x LTS release, please leave a comment with a reproduction and a maintainer will reopen. The original reporter can also reopen directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ConfirmedCorebugseverity-lowstale

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions