swagger-api / swagger-api/swagger-codegen

[Python] Bug when working with FakeDatetime from freezegun

Open
#10,293 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Swagger code generation for datetime fails in the context of fake datetime substituted by freezgun.

Description
Swagger-codegen version

3.0.20

Swagger declaration file content or url

https://api.cifrum.io/assets/swagger.json

Command line used for generation
$ java -jar ~/apps/swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
   -i https://api.cifrum.io/assets/swagger.json \
   -l python \
   -o ./lib/api_swagger_client/
Steps to reproduce
  1. Create a test inside the context of freezgun
  2. Try to fetch a model with a datetime field
  3. It should fail with the stack trace ending with:
  File "/lib-python/lib/api_swagger_client/swagger_client/api/infos_api.py", line 126, in mutru_info_with_http_info
    collection_formats=collection_formats)
  File "/lib-python/lib/api_swagger_client/swagger_client/api_client.py", line 321, in call_api
    _preload_content, _request_timeout)
  File "/lib-python/lib/api_swagger_client/swagger_client/api_client.py", line 160, in __call_api
    return_data = self.deserialize(response_data, response_type)
  File "/lib-python/lib/api_swagger_client/swagger_client/api_client.py", line 232, in deserialize
    return self.__deserialize(data, response_type)
  File "/lib-python/lib/api_swagger_client/swagger_client/api_client.py", line 271, in __deserialize
    return self.__deserialize_model(data, klass)
  File "/lib-python/lib/api_swagger_client/swagger_client/api_client.py", line 614, in __deserialize_model
    kwargs[attr] = self.__deserialize(value, attr_type)
  File "/lib-python/lib/api_swagger_client/swagger_client/api_client.py", line 271, in __deserialize
    return self.__deserialize_model(data, klass)
  File "/lib-python/lib/api_swagger_client/swagger_client/api_client.py", line 604, in __deserialize_model
    if not klass.swagger_types and not self.__hasattr(klass, 'get_real_child_model'):
AttributeError: type object 'datetime.datetime' has no attribute 'swagger_types'

It fails because api_client.py:268 check klass == datetime.datetime fails since datetime.datetime is actually <class 'freezegun.api.FakeDatetime'>.

Suggest a fix/enhancement

Hopefully FakeDatetime overrides issubclass. And I suggest the fix as follows:

        elif issubclass(klass, datetime.date):
            return self.__deserialize_date(data)
        elif issubclass(klass, datetime.datetime):
            return self.__deserialize_datatime(data)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in the generated Python client's api_client.py around line 268 and follow the __deserialize and __deserialize_model calls shown in the stack trace. Reproduce the failure with freezegun and a model containing a datetime field; done means FakeDatetime values deserialize without the swagger_types AttributeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.