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 use custom_encoder if Config.json_encoders is not defined. #714

Closed
matrixise opened this issue Nov 19, 2019 · 6 comments

Comments

@matrixise
Copy link
Contributor

Describe the bug

With factory_boy and faker, I wanted to define a Factory, here is the code...

from datetime import datetime

import factory
import faker.utils.datetime_safe
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel


class MyModel(BaseModel):
    dt_field: datetime


class MyModelFactory(factory.Factory):
    dt_field = factory.Faker("date_time")  # should return a datetime

    class Meta:
        model = MyModel


inst = MyModelFactory.build()

assert isinstance(inst, MyModel)
assert isinstance(inst.dt_field, faker.utils.datetime_safe.datetime)
assert isinstance(inst.dt_field, datetime)

inst_encoded = jsonable_encoder(inst)
assert isinstance(inst_encoded["dt_field"], dict)
assert inst_encoded["dt_field"] != inst.dt_field.isoformat()

inst_encoded = jsonable_encoder(
    inst, custom_encoder={faker.utils.datetime_safe.datetime: lambda o: o.isoformat()}
)
assert isinstance(inst_encoded["dt_field"], str)
assert inst_encoded["dt_field"] == inst.dt_field.isoformat()

To Reproduce
Use my script

Expected behavior
custom_encoder is not used...

Environment:

  • OS: Linux
  • FastAPI Version [e.g. 0.3.0], get it with:
import fastapi
print(fastapi.__version__)

FastAPI: 0.42.0

  • Python version, get it with:
python --version

Python: 3.8.0

Additional context
Add any other context about the problem here.

@matrixise matrixise added the bug Something isn't working label Nov 19, 2019
@chbndrhnns
Copy link

Can we assume this issue is fixed with the merged changed in 0.47.1?

@tiangolo
Copy link
Owner

Thanks for the report @matrixise , I think this should be fixed in the latest FastAPI versions.

Could you confirm if it works for you and if so, close the issue?

@matrixise
Copy link
Contributor Author

I will test on tomorrow and will keep you in touch.

@tiangolo
Copy link
Owner

Did you confirm if it works? May we close this issue?

@github-actions
Copy link
Contributor

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

@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 use custom_encoder if Config.json_encoders is not defined. jsonable_encoder does not use custom_encoder if Config.json_encoders is not defined. 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 #8008 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

3 participants