diff --git a/doc/changelog.d/755.fixed.md b/doc/changelog.d/755.fixed.md new file mode 100644 index 00000000..8384f21b --- /dev/null +++ b/doc/changelog.d/755.fixed.md @@ -0,0 +1 @@ +Relax validation on enum values deserialization \ No newline at end of file diff --git a/src/ansys/openapi/common/_api_client.py b/src/ansys/openapi/common/_api_client.py index 01a87064..9c7fdd79 100644 --- a/src/ansys/openapi/common/_api_client.py +++ b/src/ansys/openapi/common/_api_client.py @@ -409,7 +409,6 @@ def __deserialize(self, data: SerializedType, klass_name: str) -> DeserializedTy klass = self.models[klass_name] if issubclass(klass, Enum): - assert isinstance(data, str) return klass(data) else: assert isinstance(data, (dict, str)) diff --git a/tests/models/__init__.py b/tests/models/__init__.py index ed06b55a..7a000964 100644 --- a/tests/models/__init__.py +++ b/tests/models/__init__.py @@ -22,5 +22,6 @@ from .example_base_model import ExampleBaseModel from .example_enum import ExampleEnum +from .example_int_enum import ExampleIntEnum from .example_model import ExampleModel from .example_model_with_enum import ExampleModelWithEnum diff --git a/tests/models/example_int_enum.py b/tests/models/example_int_enum.py new file mode 100644 index 00000000..187e0dc7 --- /dev/null +++ b/tests/models/example_int_enum.py @@ -0,0 +1,28 @@ +# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates. +# SPDX-License-Identifier: MIT +# +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from enum import Enum + + +class ExampleIntEnum(Enum): + _200 = 200 + _404 = 404 diff --git a/tests/test_api_client.py b/tests/test_api_client.py index a6957bc9..bbeed32e 100644 --- a/tests/test_api_client.py +++ b/tests/test_api_client.py @@ -401,6 +401,36 @@ def test_deserialize_enum(self): assert isinstance(serialized_enum, models.ExampleEnum) assert serialized_enum == models.ExampleEnum.GOOD + def test_deserialize_int_enum(self): + from . import models + + self._client.setup_client(models) + value = 200 + type_ref = "ExampleIntEnum" + serialized_enum = self._client._ApiClient__deserialize(value, type_ref) + assert isinstance(serialized_enum, models.ExampleIntEnum) + assert serialized_enum == models.ExampleIntEnum._200 + + @pytest.mark.parametrize( + ["value", "target_enum", "expected_error_msg"], + [ + ("200", "ExampleIntEnum", "'200' is not a valid ExampleIntEnum"), + (4.5, "ExampleIntEnum", "4.5 is not a valid ExampleIntEnum"), + (4, "ExampleIntEnum", "4 is not a valid ExampleIntEnum"), + ("SomeValue", "ExampleEnum", "'SomeValue' is not a valid ExampleEnum"), + (4.5, "ExampleEnum", "4.5 is not a valid ExampleEnum"), + (4, "ExampleEnum", "4 is not a valid ExampleEnum"), + ], + ) + def test_deserialize_enums_raises_helpful_message_on_wrong_value( + self, value, target_enum, expected_error_msg + ): + from . import models + + self._client.setup_client(models) + with pytest.raises(ValueError, match=expected_error_msg): + _ = self._client._ApiClient__deserialize(value, target_enum) + @pytest.mark.parametrize( ("data", "target_type"), (