saltstack / saltstack/salt

file.serialize JSON w/merge_if_exists fails if top-level is not a dict

Open
#41,209 9 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug Platform severity-medium State-Module
Dominant language
Python
Stars
15.7k
Forks
5.6k
Avg merge
2d 44m
Merged PRs (30d)
80

Description

Description of Issue/Question

file.serialize has the merge_if_exists option.
When serializing to JSON, it apparently assumes the top level to be a dict.
While this is of course extremely common, it's not always true, and when it's not, the serializer crashes.

Steps to Reproduce Issue

SLS:

my-json-list:
  file.serialize:
    - name: /tmp/list.json
    - formatter: json
    - merge_if_exists: True
    - dataset:
        - tea
        - pot
# cat /tmp/list.json
[  "tea", "kettle" ]
          ID: my-json-list
    Function: file.serialize
        Name: /tmp/list.json
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1746, in call
                  **cdata['kwargs'])
                File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1704, in wrapper
                  return f(*args, **kwargs)
                File "/usr/lib/python2.7/dist-packages/salt/states/file.py", line 5144, in serialize
                  merged_data = salt.utils.dictupdate.merge_recurse(existing_data, dataset)
                File "/usr/lib/python2.7/dist-packages/salt/utils/dictupdate.py", line 82, in merge_recurse
                  return update(copied, obj_b, merge_lists=merge_lists)
                File "/usr/lib/python2.7/dist-packages/salt/utils/dictupdate.py", line 35, in update
                  raise TypeError('Cannot update using non-dict types in dictupdate.update()')
              TypeError: Cannot update using non-dict types in dictupdate.update()
     Started: 11:32:11.537613
    Duration: 3.339 ms
     Changes:   
Versions Report
Salt Version:
           Salt: 2016.11.4
 
Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: 2.4.2
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
          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: Not Installed
        timelib: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.1.4
 
System Versions:
           dist: Ubuntu 16.04 xenial
        machine: x86_64
        release: 4.10.0-20-generic
         system: Linux
        version: Ubuntu 16.04 xenial

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with salt/states/file.py at serialize and trace the merge_if_exists path into salt/utils/dictupdate.py, especially merge_recurse and update. Reproduce the issue with the provided SLS and confirm the expected behavior for a JSON list when an existing file is present; done means the non-dict case no longer crashes and relevant tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devops, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.