openapi-generators / openapi-generators/openapi-python-client

code generated for `date-time` fromat can not parse null response data

Open
#997 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2k
Forks
293
Avg merge
34m
Merged PRs (30d)
1

Description

Describe the bug
code generated for date-time fromat in models file like this for now:

_update_time = d.pop("updateTime", UNSET)
update_time: Union[Unset, datetime.datetime]
if isinstance(_update_time, Unset):
    update_time = UNSET
else:
    update_time = isoparse(_update_time)

if response data of updateTime field is null, the _update_time variable will be set to None, so isoparse function will rasie TypeError except.

self = <dateutil.parser.isoparser.isoparser object at 0x7f94bf7ae950>, dt_str = None

    def _parse_isodate_common(self, dt_str):
>       len_str = len(dt_str)
E       TypeError: object of type 'NoneType' has no len()

../../../../.local/lib/python3.10/site-packages/dateutil/parser/isoparser.py:213: TypeError

OpenAPI Spec File

{
    "updateTime": {
        "type": "string",
        "description": "updateTime",
        "format": "date-time"
    }
}

Desktop (please complete the following information):

  • OS: windows wsl ubuntu 22.0
  • Python Version: 3.10.0
  • openapi-python-client version 0.17.2

Additional context
my suggestion is, adding branch check value is None before call isoparse function, generate code like this:

  _update_time = d.pop("updateTime", UNSET)
  update_time: Union[Unset, datetime.datetime]
  if isinstance(_update_time, Unset):
      update_time = UNSET
  elif _update_time is None:
      update_time = None
  else:
      update_time = isoparse(_update_time)

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 by locating the generator logic that emits model deserialization for OpenAPI fields with the date-time format, then reproduce the issue using the provided nullable updateTime schema and response data. Add a regression test for null date-time values and verify that generated clients accept the response without raising TypeError.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.