Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

jsonable_encoder does not take the Config into account for Enums #196

Closed
ebreton opened this issue May 1, 2019 · 5 comments
Closed

jsonable_encoder does not take the Config into account for Enums #196

ebreton opened this issue May 1, 2019 · 5 comments

Comments

@ebreton
Copy link

ebreton commented May 1, 2019

Describe the bug

When calling jsonable_encoder on a pydantic object that makes use of an Enum for a field, the JSON response contains the Enum.field.value, whereas the default behavior should be to use Enum.field.name (as per the Config.use_enum_values which is False by default)

in the code, file encoders.py, around line 26:

def jsonable_encoder
...
    if isinstance(obj, Enum):
        return obj.value

To Reproduce
Steps to reproduce the behavior:

  1. Create a file with
from enum import Enum
from pydantic import BaseModel

class AuditType(Enum):
    rank = "RankValue"
    count = "CountValue"

class AuditBase(BaseModel):
    type: AuditType = None
  1. Add a test which calls jsonable_encoder on an instance
from fastapi.encoders import jsonable_encoder
obj_in = AuditBase(type=AuditType.count)
print(jsonable_encoder(obj_in))
  1. See error, i.e {'type': 'CountValue'} instead of {'type': 'count'}

Expected behavior

The expected behavior should be to check the value of the Config.use_enum_values and return the name or the value appropriately

Environment:

  • FastAPI Version 0.18.0
  • Python version 3.7.2
@tiangolo
Copy link
Owner

Thanks for the report. I'll check it.

@tiangolo
Copy link
Owner

I'm delaying this until pydantic/pydantic#520 is resolved.

It will change how responses are encoded from Pydantic models in a much cleaner way. And it will solve several issues at the same time (most probably including this).

@tiangolo
Copy link
Owner

Pydantic has a recent/new ORM mode.

I just finished a deep integration of it into FastAPI. It is released in version 0.30.0 🎉

Now, if you use a response_model, Pydantic will take whatever is returned by your path operation function and use it directly. Without being passed through jsonable_encoder.

If you use ORM object (SQLAlchemy, Peewee, etc) you should use Pydantic's orm_mode = True to tell Pydantic to extract the data from the ORM objects. The new docs are here: https://fastapi.tiangolo.com/tutorial/sql-databases/

Also, in case I'm misunderstanding the question, specific to the configs set in Pydantic models, I'm pretty sure the jsonable_encoder was updated at some point to use the configs from the model, maybe your specific issue was solved by that change in a previous version?

@tiangolo
Copy link
Owner

tiangolo commented Aug 4, 2019

I assume this was solved in recent versions, so I'll close this issue.

But feel free to add more comments or create new issues.

@tiangolo tiangolo closed this as completed Aug 4, 2019
@tiangolo tiangolo added question Question or problem answered reviewed and removed bug Something isn't working labels Feb 22, 2023
@tiangolo tiangolo changed the title [BUG] jsonable_encoder does not take the Config into account for Enums jsonable_encoder does not take the Config into account for Enums Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
@github-actions
Copy link
Contributor

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8282 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

2 participants