Skip to content

Commit

Permalink
[configModel] Create path when it is partial (RedHatProductSecurity#150)
Browse files Browse the repository at this point in the history
This kind of YAML was causing trouble:

```yaml
a:
```

When reading this, it is converted to `{"a": None}`, but configModel
would refuse to go down that path to set a value (unless we overwrite).

This change fixes that: in this condition, config.set() no longer
consider setting `a.b` as overwriting `None` to a dictionary

This fixes the ZAP template configuration, where the following happened:

```yaml
general:
  container:
```

the code would refuse to set `container.parameters.executable` because
container existed but points to `None`

This resulted in `zap.sh` not being set
  • Loading branch information
cedricbu authored Oct 20, 2023
1 parent 8b8c410 commit da05a93
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion configmodel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ def set(self, path, value, overwrite=True):
walk = walk[key]
continue
tmp = walk[key]
# case 2: not a "dictionary" type: warn and overwrite (if True)
# case 2: the value is None (path partially exists): initialize a dictionary
if tmp is None:
walk[key] = {}
tmp = walk[key]
# case 3: not a "dictionary" type: warn and overwrite (if True)
if not isinstance(tmp, dict):
logging.warning(
f"RapidastConfigModel.set: Incompatible {path} at {tmp}"
Expand Down
13 changes: 13 additions & 0 deletions tests/configmodel/test_configmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def generate_some_nested_config():
"key4": "value4",
"nested": {"morenested": {"key3": "nestedvalue"}},
"nothing": None,
"falsekey": False,
}


Expand Down Expand Up @@ -87,6 +88,18 @@ def test_configmodel_set(some_nested_config):
myconf.set("nested.morenested", "mynewval", overwrite=True)
assert myconf.get("nested.morenested") == "mynewval"

# deep sets
# `set` should rewrite a `None` value even on `overwrite=False`
myconf.set("nothing.some.more.nested", "newval", overwrite=False)
assert myconf.get("nothing.some.more.nested") == "newval"
# but should not rewrite a value set to False value
myconf.set("falsekey.some.more.nested", "newval", overwrite=False)
assert not myconf.exists("falsekey.some.more.nested")
# unless we overwrite
myconf.set("falsekey.some.more.nested", "newval", overwrite=True)
assert myconf.exists("falsekey.some.more.nested")
assert myconf.get("falsekey.some.more.nested") == "newval"


def test_configmodel_move(some_nested_config):
myconf = RapidastConfigModel(some_nested_config)
Expand Down

0 comments on commit da05a93

Please sign in to comment.