openapi-generators / openapi-python-client

Generate modern Python clients from OpenAPI
MIT License
1.19k stars 189 forks source link

fix: Indent of generated code for non-required lists. Thanks @sfowl! #1050

Closed sfowl closed 1 month ago

sfowl commented 1 month ago

The _transform macro is called at three different places in this file, but one location was not similarly indented as the other two. This caused errors in the ruff post_hook like:

error: Failed to parse foo/models/foo_request.py:125:9: Expected an indented block after `if` statement

The generated code without the indent fix looks like:

        if not isinstance(self.foo, Unset):
        _temp_foo = []  # <-- incorrect indent
            for foo_item_data in self.foo:
                foo_item: Union[None, int]
                foo_item = foo_item_data
                _temp_foo.append(foo_item)
            foo = (None, json.dumps(_temp_foo).encode(), 'application/json')
sfowl commented 1 month ago

I believe this bug was introduced by https://github.com/openapi-generators/openapi-python-client/issues/926

dbanty commented 1 month ago

Can you provide a minimal OpenAPI schema which triggers this issue? That way we can make sure we're exercising that code path in tests to prevent this regression.

sfowl commented 1 month ago

I've modified the baseline spec in the end to end tests to cover this case, and regenerated the snapshots with pdm regen.