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

json_encoders not respected for enum.Enum in V2 #6989

Closed
1 task done
tobni opened this issue Aug 2, 2023 · 2 comments · Fixed by #7029
Closed
1 task done

json_encoders not respected for enum.Enum in V2 #6989

tobni opened this issue Aug 2, 2023 · 2 comments · Fixed by #7029
Assignees
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable

Comments

@tobni
Copy link
Contributor

tobni commented Aug 2, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

We are using json_encoders={enum.Enum: lambda val: val.name} in pydantic V1. In V2, this setting seems to be ignored when migrating to V2.

Example Code

import enum

import pydantic
from pydantic import ConfigDict


class MyEnum(enum.Enum):
    A = 'a'
    B = 'b'

class A(pydantic.BaseModel):
    a: MyEnum
    model_config = ConfigDict(json_encoders={enum.Enum: lambda val: val.name})


print(A(a = MyEnum.A).model_dump_json()) # {"a":"a"}, expected {"a":"A"}

Python, Pydantic & OS Version

pydantic version: 2.1.1
        pydantic-core version: 2.4.0
          pydantic-core build: profile=release pgo=true mimalloc=true
                 install path: .../lib/python3.10/site-packages/pydantic
               python version: 3.10.2 (main, Mar  3 2022, 12:03:29) [GCC 9.3.0]
                     platform: Linux-5.14.0-1054-oem-x86_64-with-glibc2.35
     optional deps. installed: ['typing-extensions']

Selected Assignee: @samuelcolvin

@tobni tobni added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Aug 2, 2023
@samuelcolvin samuelcolvin assigned adriangb and unassigned samuelcolvin Aug 7, 2023
@samuelcolvin
Copy link
Member

@adriangb please can you have a look at this since you worked on json_encoders.

@adriangb
Copy link
Member

adriangb commented Aug 7, 2023

It's likely a bug stemming form the complexity in GenerateSchema. I think we can fix it, but it may not be trivial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants