From 0fdaa73e5b1b8062305423a41913160c6ebc2e4d Mon Sep 17 00:00:00 2001 From: Anca Lita <27920906+ancalita@users.noreply.github.com> Date: Wed, 11 Oct 2023 12:15:04 +0100 Subject: [PATCH] implement changes --- rasa/shared/core/domain.py | 8 ++++++++ rasa/shared/core/slot_mappings.py | 2 +- rasa/shared/utils/schemas/domain.yml | 2 +- tests/shared/core/test_domain.py | 19 +++++++++++++++++++ tests/shared/core/test_slot_mappings.py | 13 ------------- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/rasa/shared/core/domain.py b/rasa/shared/core/domain.py index 33d6e59c5487..62132cd2d5bf 100644 --- a/rasa/shared/core/domain.py +++ b/rasa/shared/core/domain.py @@ -39,6 +39,7 @@ ) from rasa.shared.core.constants import ( ACTION_SHOULD_SEND_DOMAIN, + SLOT_MAPPINGS, SlotMappingType, MAPPING_TYPE, MAPPING_CONDITIONS, @@ -490,6 +491,13 @@ def collect_slots(slot_dict: Dict[Text, Any]) -> List[Slot]: slot_type = slot_dict[slot_name].pop("type", None) slot_class = Slot.resolve_by_type(slot_type) + if SLOT_MAPPINGS not in slot_dict[slot_name]: + logger.warning( + f"Slot '{slot_name}' has no mappings defined. " + f"We will continue with an empty list of mappings." + ) + slot_dict[slot_name][SLOT_MAPPINGS] = [] + slot = slot_class(slot_name, **slot_dict[slot_name]) slots.append(slot) return slots diff --git a/rasa/shared/core/slot_mappings.py b/rasa/shared/core/slot_mappings.py index 44d19a4cd5c7..57130cccb61d 100644 --- a/rasa/shared/core/slot_mappings.py +++ b/rasa/shared/core/slot_mappings.py @@ -229,7 +229,7 @@ def validate_slot_mappings(domain_slots: Dict[Text, Any]) -> None: ) for slot_name, properties in domain_slots.items(): - mappings = properties.get(SLOT_MAPPINGS) + mappings = properties.get(SLOT_MAPPINGS, []) for slot_mapping in mappings: SlotMapping.validate(slot_mapping, slot_name) diff --git a/rasa/shared/utils/schemas/domain.yml b/rasa/shared/utils/schemas/domain.yml index bd615c9b9161..134512b25596 100644 --- a/rasa/shared/utils/schemas/domain.yml +++ b/rasa/shared/utils/schemas/domain.yml @@ -77,7 +77,7 @@ mapping: required: False mappings: type: "seq" - required: True + required: False allowempty: False sequence: - type: "map" diff --git a/tests/shared/core/test_domain.py b/tests/shared/core/test_domain.py index 5e1d1e4ced66..08e8164ef81f 100644 --- a/tests/shared/core/test_domain.py +++ b/tests/shared/core/test_domain.py @@ -1,5 +1,6 @@ import copy import json +import logging import re import textwrap from pathlib import Path @@ -7,6 +8,7 @@ from typing import Dict, List, Text, Any, Union, Set, Optional import pytest +from pytest import LogCaptureFixture from pytest import WarningsRecorder from rasa.shared.exceptions import YamlSyntaxException, YamlException @@ -2352,3 +2354,20 @@ def test_merge_yaml_domains_loads_actions_which_explicitly_need_domain(): def test_domain_responses_with_ids_are_loaded(domain_yaml, expected) -> None: domain = Domain.from_yaml(domain_yaml) assert domain.responses == expected + + +def test_domain_with_slots_without_mappings(caplog: LogCaptureFixture) -> None: + domain_yaml = """ + slots: + slot_without_mappings: + type: text + """ + with caplog.at_level(logging.WARN): + domain = Domain.from_yaml(domain_yaml) + + assert isinstance(domain.slots[0].mappings, list) + assert len(domain.slots[0].mappings) == 0 + assert ( + "Slot 'slot_without_mappings' has no mappings defined. " + "We will continue with an empty list of mappings." + ) in caplog.text diff --git a/tests/shared/core/test_slot_mappings.py b/tests/shared/core/test_slot_mappings.py index 7e67f388b102..07be20e270e2 100644 --- a/tests/shared/core/test_slot_mappings.py +++ b/tests/shared/core/test_slot_mappings.py @@ -103,19 +103,6 @@ def test_slot_mappings_ignored_intents_during_active_loop(): ) -def test_missing_slot_mappings_raises(): - with pytest.raises(YamlValidationException): - Domain.from_yaml( - f""" - version: "{LATEST_TRAINING_DATA_FORMAT_VERSION}" - slots: - some_slot: - type: text - influence_conversation: False - """ - ) - - def test_slot_mappings_invalid_type_raises(): with pytest.raises(YamlValidationException): Domain.from_yaml(