Skip to content

[bug][python][pydantic]Generated from_dict() incorrectly calls List[...] as a function for nested arrays #21309

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

Open
balazser opened this issue May 20, 2025 · 0 comments

Comments

@balazser
Copy link

Description

openapi-generator produces broken Python code when generating models with nested arrays, such as in a MultiPolygon structure.

The generated model looks like this:

class MultiPolygon(BaseModel):
    bbox: Optional[Bbox] = None
    coordinates: List[List[Annotated[List[LineStringCoordinatesInner], Field(min_length=4)]]]
    type: StrictStr

But in the generated from_dict() method, it incorrectly tries to call List[...] as a function:

"coordinates": [
    [List[LineStringCoordinatesInner].from_dict(_inner_item) for _inner_item in _item]
    for _item in obj["coordinates"]
]

Model Class

class RoofLayerFeature(geojson_pydantic.Feature):
    pass

Generated DTO class

class MultiPolygon(BaseModel):
    """
    MultiPolygon Model
    """ # noqa: E501
    bbox: Optional[Bbox] = None
    coordinates: List[List[Annotated[List[LineStringCoordinatesInner], Field(min_length=4)]]]
    type: StrictStr
    __properties: ClassVar[List[str]] = ["bbox", "coordinates", "type"]

    # ...

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of MultiPolygon from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "bbox": Bbox.from_dict(obj["bbox"]) if obj.get("bbox") is not None else None,
            "coordinates": [
                    [List[LineStringCoordinatesInner].from_dict(_inner_item) for _inner_item in _item]
                    for _item in obj["coordinates"]
                ] if obj.get("coordinates") is not None else None,
            "type": obj.get("type")
        })
        return _obj

This leads to a runtime error since List[...] is not callable.
openapi-generator version

@openapitools/[email protected]

Steps to Reproduce

Generate a Python client using a schema that includes a deeply nested array (like MultiPolygon) and try to use the generated from_dict() method. It will fail due to misuse of List[...] as a constructor.

Currently I'm using fastapi + https://github.com/developmentseed/geojson-pydantic

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

1 participant