Skip to content

Commit

Permalink
Fixes bug in string case serialization not taking numbers into account (
Browse files Browse the repository at this point in the history
  • Loading branch information
matt035343 authored Jun 25, 2023
1 parent 8ffa594 commit 21d2ab2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions dataclasses_json/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ def _encode_overrides(kvs, overrides, encode_json=False):
letter_case = overrides[k].letter_case
original_key = k
k = letter_case(k) if letter_case is not None else k
if k in override_kvs:
raise ValueError(
f"Multiple fields map to the same JSON "
f"key after letter case encoding: {k}"
)

encoder = overrides[original_key].encoder
v = encoder(v) if encoder is not None else v
Expand Down
4 changes: 2 additions & 2 deletions dataclasses_json/stringcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def camelcase(string):
if not string:
return string
return (uplowcase(string[0], 'low')
+ re.sub(r"[\-_\.\s]([a-z])",
+ re.sub(r"[\-_\.\s]([a-z0-9])",
lambda matched: uplowcase(matched.group(1), 'up'),
string[1:]))

Expand All @@ -96,7 +96,7 @@ def snakecase(string):
if not string:
return string
return (uplowcase(string[0], 'low')
+ re.sub(r"[A-Z]",
+ re.sub(r"[A-Z0-9]",
lambda matched: '_' + uplowcase(matched.group(0), 'low'),
string[1:]))

Expand Down
37 changes: 37 additions & 0 deletions tests/test_letter_case.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from dataclasses import dataclass, field

import pytest

from dataclasses_json import LetterCase, dataclass_json, config


Expand Down Expand Up @@ -56,6 +58,31 @@ class CamelCasePersonWithOverride:
years_on_earth: int = field(metadata=config(field_name='age'))


@dataclass_json
@dataclass
class CamelCaseProtectedNamePerson:
_given_name_2: str = field(
metadata={'dataclasses_json': {
'letter_case': LetterCase.CAMEL
}}
)


@dataclass_json
@dataclass
class CamelCaseDuplicatedNameEncodingPerson:
given_name_1: str = field(
metadata={'dataclasses_json': {
'letter_case': LetterCase.CAMEL
}}
)
given_name1: str = field(
metadata={'dataclasses_json': {
'letter_case': LetterCase.CAMEL
}}
)


class TestLetterCase:
def test_camel_encode(self):
assert CamelCasePerson('Alice').to_json() == '{"givenName": "Alice"}'
Expand Down Expand Up @@ -107,3 +134,13 @@ def test_from_dict(self):

def test_to_dict(self):
assert {'givenName': 'Alice'} == CamelCasePerson('Alice').to_dict()

def test_protected_encode(self):
assert CamelCaseProtectedNamePerson('Alice').to_json() == '{"givenName2": "Alice"}'

def test_protected_decode(self):
assert CamelCaseProtectedNamePerson.from_json('{"givenName2": "Alice"}') == CamelCaseProtectedNamePerson('Alice')

def test_duplicated_encoding(self):
with pytest.raises(ValueError):
CamelCaseDuplicatedNameEncodingPerson('Alice', 'Bob').to_json()

0 comments on commit 21d2ab2

Please sign in to comment.