Skip to content
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
1 change: 1 addition & 0 deletions doc/changelog.d/755.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Relax validation on enum values deserialization
1 change: 0 additions & 1 deletion src/ansys/openapi/common/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions tests/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions tests/models/example_int_enum.py
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
(
Expand Down