question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] jsonable_encoder does not use custom_encoder if Config.json_encoders is not defined.

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
chbndrhnnscommented, Jan 27, 2020

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

0reactions
github-actions[bot]commented, Apr 23, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

override default encoders for jsonable_encoder in FastAPI
Right now I am using custom encoder within json.dumps like this: class TestEncoder(JSONEncoder): def default(self, obj): if isinstance(obj, ...
Read more >
json — JSON encoder and decoder — Python 3.11.1 ...
To use a custom JSONDecoder subclass, specify it with the cls kwarg; otherwise JSONDecoder is used. Additional keyword arguments will be passed to...
Read more >
JSON Compatible Encoder - FastAPI
You can use jsonable_encoder for that. It receives an object, like a Pydantic model, and returns a JSON compatible version: Python 3.6 and...
Read more >
Python JSON Serialize Set - PYnative
You are here because when you try to dump or encode Python set into JSON, you received an error, TypeError: Object of type...
Read more >
Overriding default JSON encoding of floating point values?
Using JSONEncoder /JSONSerialization, floating point values will be ... bad request with an error message telling me that it's not a float if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found