Skip to content

Commit

Permalink
Empty string is a valid JSON-key
Browse files Browse the repository at this point in the history
Signed-off-by: Sergey Tsaplin <me@sergeytsaplin.com>
  • Loading branch information
SergeyTsaplin committed Jul 19, 2022
1 parent 50bc758 commit 04a0fc7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pydantic/fields.py
Expand Up @@ -148,7 +148,7 @@ def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
self.default = default
self.default_factory = kwargs.pop('default_factory', None)
self.alias = kwargs.pop('alias', None)
self.alias_priority = kwargs.pop('alias_priority', 2 if self.alias else None)
self.alias_priority = kwargs.pop('alias_priority', 2 if self.alias is not None else None)
self.title = kwargs.pop('title', None)
self.description = kwargs.pop('description', None)
self.exclude = kwargs.pop('exclude', None)
Expand Down Expand Up @@ -395,7 +395,7 @@ def __init__(

self.name: str = name
self.has_alias: bool = bool(alias)
self.alias: str = alias or name
self.alias: str = alias if alias is not None else name
self.type_: Any = convert_generics(type_)
self.outer_type_: Any = type_
self.class_validators = class_validators or {}
Expand Down
10 changes: 10 additions & 0 deletions tests/test_aliases.py
Expand Up @@ -335,3 +335,13 @@ def alias_generator(x):
'd_config_parent',
'e_generator_child',
]


def test_empty_string_alias():
class Model(BaseModel):
empty_string_key: int = Field(alias='')

data = {'': 123}
m = Model(**data)
assert m.empty_string_key == 123
assert m.dict(by_alias=True) == data

0 comments on commit 04a0fc7

Please sign in to comment.