Repository: cookiecutter/cookiecutter

Issue:
Adding value to nested dicts broken after 2.2.0
Tested with cookiecutter version: 2.5.0 and main

Prior to Version 2.2.0 one could have a `cookiecutter.json` with a dict as value like so:
```json
{
  "mainkey": {
    "subkey1": "value1"
    "subkey2": "value2"
   }
}
```
and then define an YAML to pass in with `--config-file ` like so:
```YAML
default_context:
    mainkey:
        subkey1: value1
        subkey2: value2
        subkey3: value3
        subkey4: value4
```
And after this, in the template with `cookiecutter.mainkey` one got all `subkey1` to `subkey4`.

The feature https://github.com/cookiecutter/cookiecutter/pull/1692 here broke this (even if there were already some other fixes to this).

After 2.2.0 all we got are `subkey1` and `subkey2`.


Discussion:

Resolving patch:
diff --git a/cookiecutter/generate.py b/cookiecutter/generate.py
--- a/cookiecutter/generate.py
+++ b/cookiecutter/generate.py
@@ -55,8 +55,11 @@ def apply_overwrites_to_context(
     """Modify the given context in place based on the overwrite_context."""
     for variable, overwrite in overwrite_context.items():
         if variable not in context:
-            # Do not include variables which are not used in the template
-            continue
+            if not in_dictionary_variable:
+                # We are dealing with a new variable on first level, ignore
+                continue
+            # We are dealing with a new dictionary variable in a deeper level
+            context[variable] = overwrite
 
         context_value = context[variable]
         if isinstance(context_value, list):


Test patch:
diff --git a/tests/test-generate-context/nested_dict_additional.json b/tests/test-generate-context/nested_dict_additional.json
new file mode 100644
--- /dev/null
+++ b/tests/test-generate-context/nested_dict_additional.json
@@ -0,0 +1,9 @@
+{
+  "mainkey1": "mainvalue1",
+  "mainkey2": {
+    "subkey1": "subvalue1",
+    "subkey2": {
+      "subsubkey1": "subsubvalue1"
+    }
+  }
+}
diff --git a/tests/test_generate_context.py b/tests/test_generate_context.py
--- a/tests/test_generate_context.py
+++ b/tests/test_generate_context.py
@@ -1,4 +1,5 @@
 """Verify generate context behaviour and context overwrite priorities."""
+
 import os
 import re
 from collections import OrderedDict
@@ -290,3 +291,74 @@ def test_apply_overwrites_in_nested_dict():
     )
 
     assert generated_context == expected_context
+
+
+def test_apply_overwrite_context_as_in_nested_dict_with_additional_values():
+    """Verify nested dict in default content settings are correctly added.
+
+    The `apply_overwrites_to_context` function should add the extra values to the dict.
+    """
+    expected = OrderedDict({"key1": "value1", "key2": "value2"})
+    context = OrderedDict({"key1": "value1"})
+    overwrite_context = OrderedDict({"key2": "value2"})
+    generate.apply_overwrites_to_context(
+        context,
+        overwrite_context,
+        in_dictionary_variable=True,
+    )
+    assert context == expected
+
+
+def test_apply_overwrites_in_nested_dict_additional_values():
+    """Verify nested dict in default content settings are correctly added."""
+    expected_context = {
+        'nested_dict_additional': OrderedDict(
+            [
+                ('mainkey1', 'mainvalue1'),
+                (
+                    'mainkey2',
+                    OrderedDict(
+                        [
+                            ('subkey1', 'subvalue1'),
+                            (
+                                'subkey2',
+                                OrderedDict(
+                                    [
+                                        ('subsubkey1', 'subsubvalue1'),
+                                        ('subsubkey2', 'subsubvalue2_default'),
+                                        ('subsubkey3', 'subsubvalue3_extra'),
+                                    ]
+                                ),
+                            ),
+                            ('subkey4', 'subvalue4_default'),
+                            ('subkey5', 'subvalue5_extra'),
+                        ]
+                    ),
+                ),
+            ]
+        )
+    }
+
+    generated_context = generate.generate_context(
+        context_file='tests/test-generate-context/nested_dict_additional.json',
+        default_context={
+            'not_in_template': 'foobar',
+            'mainkey2': {
+                'subkey2': {
+                    'subsubkey2': 'subsubvalue2_default',
+                },
+                'subkey4': 'subvalue4_default',
+            },
+        },
+        extra_context={
+            'also_not_in_template': 'foobar2',
+            'mainkey2': {
+                'subkey2': {
+                    'subsubkey3': 'subsubvalue3_extra',
+                },
+                'subkey5': 'subvalue5_extra',
+            },
+        },
+    )
+
+    assert generated_context == expected_context