Skip to content

Commit

Permalink
Merge pull request #5794 from RasaHQ/domain-from-dict
Browse files Browse the repository at this point in the history
make sure Domain.fromDict(domain) doesn't change its input domain
  • Loading branch information
chkoss authored May 14, 2020
2 parents 79cb399 + 1c7aadf commit 911acc0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
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.
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
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():
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

0 comments on commit 911acc0

Please sign in to comment.