Skip to content
Open
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
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-128.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: Deserialize enum variants case-insensitively, for compatibility with
the Java implementation
links:
- https://github.com/palantir/conjure-python-client/pull/128
5 changes: 3 additions & 2 deletions conjure_python_client/_serde/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ def decode_conjure_enum_type(cls, obj, conjure_type):
)
)

if obj in conjure_type.__members__:
return conjure_type[obj]
upper_value: str = obj.upper()
if upper_value in conjure_type.__members__:
return conjure_type[upper_value]

else:
return conjure_type["UNKNOWN"]
Expand Down
33 changes: 33 additions & 0 deletions test/example_service/product/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,36 @@ def __init__(self, value: Dict[str, str]) -> None:
@property
def value(self) -> Dict[str, str]:
return self._value


class EnumExample(ConjureEnumType):

ONE = 'ONE'
'''ONE'''
TWO = 'TWO'
'''TWO'''
ONE_HUNDRED = 'ONE_HUNDRED'
'''ONE_HUNDRED'''
UNKNOWN = 'UNKNOWN'
'''UNKNOWN'''

def __reduce_ex__(self, proto):
return self.__class__, (self.name,)


class EnumFieldExample(ConjureBeanType):

@classmethod
def _fields(cls) -> Dict[str, ConjureFieldDefinition]:
return {
'enum': ConjureFieldDefinition('enum', EnumExample)
}

__slots__: List[str] = ['_enum']

def __init__(self, enum: "EnumExample") -> None:
self._enum = enum

@property
def enum(self) -> "EnumExample":
return self._enum
12 changes: 11 additions & 1 deletion test/serde/test_decode_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import re
from conjure_python_client import ConjureDecoder
from test.example_service.product import CreateDatasetRequest
from test.example_service.product.datasets import ListExample, MapExample
from test.example_service.product.datasets import EnumExample, EnumFieldExample, ListExample, MapExample


def test_object_decodes_when_exact_fields_are_present():
Expand Down Expand Up @@ -54,6 +54,16 @@ def test_object_with_omitted_map_field_decodes():
assert decoded == MapExample({})


def test_object_with_enum_field_decodes():
decoded = ConjureDecoder().read_from_string('{"enum": "ONE"}', EnumFieldExample)
assert decoded == EnumFieldExample(EnumExample.ONE)


def test_object_with_enum_field_decodes_case_insensitive():
decoded = ConjureDecoder().read_from_string('{"enum": "one"}', EnumFieldExample)
assert decoded == EnumFieldExample(EnumExample.ONE)


def test_object_with_missing_field_should_throw_helpful_exception():
with pytest.raises(Exception) as excinfo:
ConjureDecoder().read_from_string(
Expand Down