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.dumps in swagger_info will fail on datetime serialization #94

Open
nmatare opened this issue Nov 10, 2020 · 1 comment
Open

json.dumps in swagger_info will fail on datetime serialization #94

nmatare opened this issue Nov 10, 2020 · 1 comment

Comments

@nmatare
Copy link

nmatare commented Nov 10, 2020

If the swagger_info attribute contains a datetime object in an example definition, the default JSON serializer will fail for datetime types.

ex)

TypeError: datetime.datetime(2021, 11, 14, 1, 34, 26, tzinfo=datetime.timezone(datetime.timedelta(0, 720))) is not JSON serializable

https://swagger.io/docs/specification/data-models/data-types/#string

swagger_info = json.dumps(swagger_info)

Proposing a PR to either:

  1. allow the caller to pass in a string, a pre-serialized spec;
  2. allow the caller to specify a user-defined serializer function; or
  3. something else.
@iTaybb
Copy link

iTaybb commented Sep 20, 2022

In my case, changing all the json.dumps calls and adding default=str as an argument solved the issue.

Since this project seems unmaintained at the moment, I've added the following lines to my Dockerfile to patch the library for this issue:

LIB_FOLDER=$(python -c 'import aiohttp_swagger; print(aiohttp_swagger.__path__[0])')
sed -i 's/json.dumps(swagger)/json.dumps(swagger, default=str)/g' $LIB_FOLDER/helpers/builders.py
sed -i 's/json.dumps(swagger_info)/json.dumps(swagger_info, default=str)/g' $LIB_FOLDER/__init__.py

Or via monkey patching:

aiohttp_swagger.json.dumps = partial(aiohttp_swagger.json.dumps, default=str)
aiohttp_swagger.helpers.builders.json.dumps = partial(aiohttp_swagger.helpers.builders.json.dumps, default=str)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants