Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure Domain.fromDict(domain) doesn't change its input domain #5794

Merged
merged 4 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/5794.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Creating a ``Domain`` using ``Domain.fromDict`` can no longer alter the input dictionary.
chkoss marked this conversation as resolved.
Show resolved Hide resolved
Previously, there could be problems when the input dictionary was re-used for other
things after creating the ``Domain`` from it.
4 changes: 4 additions & 0 deletions rasa/core/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ def collect_slots(slot_dict: Dict[Text, Any]) -> List[Slot]:
# it is super important to sort the slots here!!!
# otherwise state ordering is not consistent
slots = []
# make a copy to not alter the input dictionary
chkoss marked this conversation as resolved.
Show resolved Hide resolved
slot_dict = copy.deepcopy(slot_dict)
for slot_name in sorted(slot_dict):
slot_class = Slot.resolve_by_type(slot_dict[slot_name].get("type"))
if "type" in slot_dict[slot_name]:
Expand Down Expand Up @@ -332,6 +334,8 @@ def collect_intent_properties(
Returns:
The intent properties to be stored in the domain.
"""
# make a copy to not alter the input argument
intents = copy.deepcopy(intents)
intent_properties = {}
duplicates = set()
for intent in intents:
Expand Down
29 changes: 27 additions & 2 deletions tests/core/test_domain.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import json
from pathlib import Path

Expand Down Expand Up @@ -680,8 +681,6 @@ def test_clean_domain_deprecated_templates():


def test_add_knowledge_base_slots(default_domain):
import copy

# don't modify default domain as it is used in other tests
test_domain = copy.deepcopy(default_domain)

Expand Down Expand Up @@ -802,3 +801,29 @@ def test_domain_utterance_actions_deprecated_templates():
old_domain = Domain.from_yaml(old_yaml)
new_domain = Domain.from_yaml(new_yaml)
assert hash(old_domain) == hash(new_domain)


def test_domain_from_dict_does_not_change_input():
wochinge marked this conversation as resolved.
Show resolved Hide resolved
input_before = {
"intents": [
{"greet": {USE_ENTITIES_KEY: ["name"]}},
{"default": {IGNORE_ENTITIES_KEY: ["unrelated_recognized_entity"]}},
{"goodbye": {USE_ENTITIES_KEY: None}},
{"thank": {USE_ENTITIES_KEY: False}},
{"ask": {USE_ENTITIES_KEY: True}},
{"why": {USE_ENTITIES_KEY: []}},
"pure_intent",
],
"entities": ["name", "unrelated_recognized_entity", "other"],
"slots": {"name": {"type": "text"}},
"responses": {
"utter_greet": [{"text": "hey there {name}!"}],
"utter_goodbye": [{"text": "goodbye 😢"}, {"text": "bye bye 😢"}],
"utter_default": [{"text": "default message"}],
},
}

input_after = copy.deepcopy(input_before)
Domain.from_dict(input_after)

assert input_after == input_before