Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An empty string is a valid JSON attribute name #4253

Closed
SergeyTsaplin opened this issue Jul 19, 2022 · 0 comments · Fixed by #4252
Closed

An empty string is a valid JSON attribute name #4253

SergeyTsaplin opened this issue Jul 19, 2022 · 0 comments · Fixed by #4252
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@SergeyTsaplin
Copy link
Contributor

Checks

  • [ x] I added a descriptive title to this issue
  • [ x] I have searched (google, github) for similar issues and couldn't find anything
  • [ x] I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.9.0
            pydantic compiled: False
                 install path: /Users/s_tsaplin/projects/github.com/sergeytsaplin/pydantic/pydantic
               python version: 3.9.13 (main, May 19 2022, 14:10:54)  [Clang 13.1.6 (clang-1316.0.21.2)]
                     platform: macOS-12.4-x86_64-i386-64bit
     optional deps. installed: ['devtools', 'dotenv', 'email-validator', 'typing-extensions']
from pydantic import BaseModel, Field

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

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

Current behavior:

The code causes the exception:

Traceback (most recent call last):
  File "<masked>/.pyenv/versions/3.10.1/lib/python3.10/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Model
empty_string_key
  field required (type=value_error.missing)

Expected result:

The code should print:

{'': 123}

Links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant