Context data merges instead of overwritting with dicts #9553

Closed
opened 2025-12-29 20:51:22 +01:00 by adam · 1 comment
Owner

Originally created by @tehungtseng1 on GitHub (Apr 29, 2024).

Deployment Type

Self-hosted

NetBox Version

v3.7.2

Python Version

3.11

Steps to Reproduce

Create two config contexts with one lower weight and one heigher weight.
Use dict as your data

lower_weight:

{
    "the_dict_to_check": {
        "low_weight_key": "low_weight_data"
    },
    "the_list_to_check": [
        "lower_weight_data"
    ],
    "the_variable_to_check": "lower_weight_data"
}

higher_weight:

{
    "the_dict_to_check": {
        "higher_weight_key": "higher_weight_data"
    },
    "the_list_to_check": [
        "higher_weight_data"
    ],
    "the_variable_to_check": "higher_weight_data"
}

Expected Behavior

https://docs.netbox.dev/en/stable/features/context-data/

https://github.com/netbox-community/netbox/issues/5698#issuecomment-773519600

Should overwrite the variable instead of merge. This works as intended in a list

{
    "the_dict_to_check": {
        "higher_weight_key": "higher_weight_data",
    },
    "the_list_to_check": [
        "higher_weight_data"
    ],
    "the_variable_to_check": "higher_weight_data"
}

Observed Behavior

Seems to be this line;
https://github.com/netbox-community/netbox/blob/v3.7.2/netbox/utilities/utils.py#L275
https://github.com/netbox-community/netbox/blob/master/netbox/utilities/utils.py#L276

{
    "the_dict_to_check": {
        "higher_weight_key": "higher_weight_data",
        "low_weight_key": "low_weight_data"
    },
    "the_list_to_check": [
        "higher_weight_data"
    ],
    "the_variable_to_check": "higher_weight_data"
}

Originally created by @tehungtseng1 on GitHub (Apr 29, 2024). ### Deployment Type Self-hosted ### NetBox Version v3.7.2 ### Python Version 3.11 ### Steps to Reproduce Create two config contexts with one lower weight and one heigher weight. Use dict as your data **lower_weight:** ``` { "the_dict_to_check": { "low_weight_key": "low_weight_data" }, "the_list_to_check": [ "lower_weight_data" ], "the_variable_to_check": "lower_weight_data" } ``` **higher_weight:** ``` { "the_dict_to_check": { "higher_weight_key": "higher_weight_data" }, "the_list_to_check": [ "higher_weight_data" ], "the_variable_to_check": "higher_weight_data" } ``` ### Expected Behavior https://docs.netbox.dev/en/stable/features/context-data/ https://github.com/netbox-community/netbox/issues/5698#issuecomment-773519600 Should overwrite the variable instead of merge. This works as intended in a list ``` { "the_dict_to_check": { "higher_weight_key": "higher_weight_data", }, "the_list_to_check": [ "higher_weight_data" ], "the_variable_to_check": "higher_weight_data" } ``` ### Observed Behavior Seems to be this line; https://github.com/netbox-community/netbox/blob/v3.7.2/netbox/utilities/utils.py#L275 https://github.com/netbox-community/netbox/blob/master/netbox/utilities/utils.py#L276 ``` { "the_dict_to_check": { "higher_weight_key": "higher_weight_data", "low_weight_key": "low_weight_data" }, "the_list_to_check": [ "higher_weight_data" ], "the_variable_to_check": "higher_weight_data" } ```
adam closed this issue 2025-12-29 20:51:22 +01:00
Author
Owner

@jeremystretch commented on GitHub (Apr 29, 2024):

This is working as intended. There's even an example in the previous issue to which you linked confirming that this is expected behavior.

Individual keys mapping to discrete values (strings, numbers, lists) are overwritten whereas dictionaries are merged. This is true at all levels of the data hierarchy. If you want to overwrite a lower-weight item, you need to reuse the same key when defining a higher-weight item.

@jeremystretch commented on GitHub (Apr 29, 2024): This is working as intended. There's even an example in the previous issue to which you linked confirming that this is expected behavior. Individual keys mapping to discrete values (strings, numbers, lists) are overwritten whereas dictionaries are merged. This is true at all levels of the data hierarchy. If you want to overwrite a lower-weight item, you need to reuse the same key when defining a higher-weight item.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9553