Skip to content

Invalid Code is generated for lists inside a union #756

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

Closed
diesieben07 opened this issue May 4, 2023 · 1 comment · Fixed by #930
Closed

Invalid Code is generated for lists inside a union #756

diesieben07 opened this issue May 4, 2023 · 1 comment · Fixed by #930
Labels
🐞bug Something isn't working

Comments

@diesieben07
Copy link

Describe the bug
When a list occurs inside a union, this call causes the list template to initialize the value with UNSET instead of []. The list template then tries to call append on UNSET, which of course does not work.

OpenAPI Spec File
A link to your openapi.json which produces this issue.

Desktop (please complete the following information):

  • OS: Ubuntu 22.04.2
  • Python Version: 2.10.8
  • openapi-python-client version 0.14.0

Additional context
The above OpneAPI spec file generates the following code, for reference:

def _parse_response(
    *, client: Client, response: httpx.Response
) -> Optional[Union["GetExampleResponse200Type0", List["GetExampleResponse200Type1Item"]]]:
    if response.status_code == HTTPStatus.OK:

        def _parse_response_200(
            data: object,
        ) -> Union["GetExampleResponse200Type0", List["GetExampleResponse200Type1Item"]]:
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                response_200_type_0 = GetExampleResponse200Type0.from_dict(data)

                return response_200_type_0
            except:  # noqa: E722
                pass
            if not isinstance(data, list):
                raise TypeError()
            response_200_type_1 = UNSET # PROBLEM IS HERE
            _response_200_type_1 = data
            for response_200_type_1_item_data in _response_200_type_1:
                response_200_type_1_item = GetExampleResponse200Type1Item.from_dict(response_200_type_1_item_data)

                response_200_type_1.append(response_200_type_1_item)

            return response_200_type_1

        response_200 = _parse_response_200(response.json())

        return response_200
    if client.raise_on_unexpected_status:
        raise errors.UnexpectedStatus(response.status_code, response.content)
    else:
        return None
@diesieben07 diesieben07 added the 🐞bug Something isn't working label May 4, 2023
@kgutwin
Copy link
Contributor

kgutwin commented Jan 3, 2024

It looks like I also identified this in #928 and there is a proposed fix in #929.

dbanty added a commit that referenced this issue Jan 4, 2024
This PR was created by Knope. Merging it will create a new release

### Features

#### Export `Unset` types from generated `types.py` (#927)

#### Generate properties for some boolean enums

If a schema has both `type = "boolean"` and `enum` defined, a normal
boolean property will now be created.
Previously, the generator would error. 

Note that the generate code _will not_ correctly limit the values to the
enum values. To work around this, use the
OpenAPI 3.1 `const` instead of `enum` to generate Python `Literal`
types.

Thanks for reporting #922 @macmoritz!

### Fixes

#### Do not stop generation for invalid enum values

This generator only supports `enum` values that are strings or integers.
Previously, this was handled at the parsing level, which would cause the
generator to fail if there were any unsupported values in the document.
Now, the generator will correctly keep going, skipping only endpoints
which contained unsupported values.

Thanks for reporting #922 @macmoritz!

#### Fix lists within unions

Fixes #756 and #928. Arrays within unions (which, as of 0.17 includes
nullable arrays) would generate invalid code.

Thanks @kgutwin and @diesieben07!

#### Simplify type checks for non-required unions

Co-authored-by: GitHub <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants