Skip to content

[BUG] [PYTHON] Code generation of model for arrays #23508

@ilayu-blip

Description

@ilayu-blip
Description

Generated python sdk code does not handle WELL responses that have value of 'None'.
For the example spec I have provided below the generated code (of the from_dict) function has this:

"reactions": dict(
                (_k,
                        [UserUser.from_dict(_item) for _item in _v]
                        if _v is not None
                        else None
                )
                for _k, _v in obj.get("reactions", {}).items()
            ),

However, if the dict has something like {"reactions": None} then the code throws an exception ('None' does not '.items').
I think that the obj.get("reactions", {}) part was SUPPOSED to handle that (i.e get empty dict if empty).
I understand it that way because 'python-pydantic-v1' generation has something like this:

"{{{name}}}": {
                _k: [{{{items.items.dataType}}}.from_dict(_item) for _item in _v] if _v is not None else None
                for _k, _v in obj["{{{baseName}}}"].items()
            }
            if obj.get("{{{baseName}}}") is not None
            else None{{^-last}},{{/-last}}
openapi-generator version

7.21

OpenAPI declaration file content or url
reactions:
          type: object
          description: Reactions on that task.
          allOf:
          - $ref: '#/components/schemas/models.ReactionMap'
Generation Details

Generating different 'python sdks' has different results (for different template code)
I have generated the sdk for the "regular "'python' variant.

# 'python'
{{#items.isArray}}
            "{{{baseName}}}": dict(
                (_k,
                        [{{{items.items.dataType}}}.from_dict(_item) for _item in _v]
                        if _v is not None
                        else None
                )
                for _k, _v in obj.get("{{{baseName}}}", {}).items()
            ){{^-last}},{{/-last}}
            {{/items.isArray}}
# 'python-pydantic-vi'
{{#items.isArray}}
            "{{{name}}}": {
                _k: [{{{items.items.dataType}}}.from_dict(_item) for _item in _v] if _v is not None else None
                for _k, _v in obj["{{{baseName}}}"].items()
            }
            if obj.get("{{{baseName}}}") is not None
            else None{{^-last}},{{/-last}}
Suggest a fix

There are 2 possible fixes I can think of:

  1. Make the ACTUAL default object a dict (obj.get('name') or {}) (which I think was the REAL think the original author of that code meant)
  2. Pass None to the 'model_validate' function by check whether or not obj.get('name') is None or not (VERY similar to the code in the 'python-pydantic-v1' variant).

The fix should also make all the variants have consistent code (which currently it is not).

I'm willing to make a PR and fix this issue, given the fix we choose

Thanks in advance :)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions