From e78b8d69644fc5ff005104b787e1cec1d625b315 Mon Sep 17 00:00:00 2001 From: Jonathan Brownell Date: Wed, 8 Oct 2014 15:17:16 -0700 Subject: [PATCH] Treat null values in JSON as being absent rather than 'None' The current method of serialization for keys containing null values in JSON input files is that they become a part of the metadata tree, with a value expressed as the string 'None' in output (when invoked with --key) or when inserted into templates. The use of 'None' creates awkward ambiguity (no way for downstream code logic to tell if the request JSON key originally contained the string "None" or if it was actually null), and also throws conversion errors when requested in any type other than 'raw'. This patch proposes that keys with null values be handled as though they were absent from the metadata completely. (If such a key is requested in command-line invokations using "--key" and "--key-default", the provided default will be printed.) Change-Id: I9288dffb13d781e0c4dd9e5be0b483c090ec2d7c --- os_apply_config/apply_config.py | 2 ++ os_apply_config/tests/test_apply_config.py | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/os_apply_config/apply_config.py b/os_apply_config/apply_config.py index fa609ea..b99ebe6 100755 --- a/os_apply_config/apply_config.py +++ b/os_apply_config/apply_config.py @@ -158,6 +158,8 @@ def print_key( for key in keys: try: config = config[key] + if config is None: + raise TypeError() except (KeyError, TypeError): if default is not None: print(str(default)) diff --git a/os_apply_config/tests/test_apply_config.py b/os_apply_config/tests/test_apply_config.py index 16319b5..45efa43 100644 --- a/os_apply_config/tests/test_apply_config.py +++ b/os_apply_config/tests/test_apply_config.py @@ -39,6 +39,7 @@ TEMPLATE_PATHS = [ CONFIG = { "x": "foo", "y": False, + "z": None, "database": { "url": "sqlite:///blah" } @@ -114,6 +115,14 @@ class TestRunOSConfigApplier(testtools.TestCase): self.stdout.read().strip()) self.assertEqual('', self.logger.output) + def test_print_null_key(self): + self.assertEqual(0, apply_config.main( + ['os-apply-config.py', '--metadata', self.path, '--key', + 'z', '--type', 'raw', '--key-default', ''])) + self.stdout.seek(0) + self.assertEqual('', self.stdout.read().strip()) + self.assertEqual('', self.logger.output) + def test_print_key_missing(self): self.assertEqual(1, apply_config.main( ['os-apply-config.py', '--metadata', self.path, '--key',