Title: Invalid Code is generated for lists inside a union · Issue #756 · openapi-generators/openapi-python-client · GitHub
Open Graph Title: Invalid Code is generated for lists inside a union · Issue #756 · openapi-generators/openapi-python-client
X Title: Invalid Code is generated for lists inside a union · Issue #756 · openapi-generators/openapi-python-client
Description: 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 Fi...
Open Graph Description: 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 ...
X Description: 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 ...
Opengraph URL: https://github.com/openapi-generators/openapi-python-client/issues/756
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Invalid Code is generated for lists inside a union","articleBody":"**Describe the bug**\r\nWhen a list occurs inside a union, [this call](https://github.com/openapi-generators/openapi-python-client/blob/508a841eea69da5f8e868ee20d785b16426212d4/openapi_python_client/templates/property_templates/union_property.py.jinja#L31) 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.\r\n\r\n**OpenAPI Spec File**\r\n[A link to your openapi.json which produces this issue.](https://gist.github.com/diesieben07/267dd33e21a206ce4bc9a8906bba259e)\r\n\r\n**Desktop (please complete the following information):**\r\n - OS: Ubuntu 22.04.2\r\n - Python Version: 2.10.8\r\n - openapi-python-client version 0.14.0\r\n\r\n**Additional context**\r\nThe above OpneAPI spec file generates the following code, for reference:\r\n```python\r\ndef _parse_response(\r\n *, client: Client, response: httpx.Response\r\n) -\u003e Optional[Union[\"GetExampleResponse200Type0\", List[\"GetExampleResponse200Type1Item\"]]]:\r\n if response.status_code == HTTPStatus.OK:\r\n\r\n def _parse_response_200(\r\n data: object,\r\n ) -\u003e Union[\"GetExampleResponse200Type0\", List[\"GetExampleResponse200Type1Item\"]]:\r\n try:\r\n if not isinstance(data, dict):\r\n raise TypeError()\r\n response_200_type_0 = GetExampleResponse200Type0.from_dict(data)\r\n\r\n return response_200_type_0\r\n except: # noqa: E722\r\n pass\r\n if not isinstance(data, list):\r\n raise TypeError()\r\n response_200_type_1 = UNSET # PROBLEM IS HERE\r\n _response_200_type_1 = data\r\n for response_200_type_1_item_data in _response_200_type_1:\r\n response_200_type_1_item = GetExampleResponse200Type1Item.from_dict(response_200_type_1_item_data)\r\n\r\n response_200_type_1.append(response_200_type_1_item)\r\n\r\n return response_200_type_1\r\n\r\n response_200 = _parse_response_200(response.json())\r\n\r\n return response_200\r\n if client.raise_on_unexpected_status:\r\n raise errors.UnexpectedStatus(response.status_code, response.content)\r\n else:\r\n return None\r\n```\r\n","author":{"url":"https://github.com/diesieben07","@type":"Person","name":"diesieben07"},"datePublished":"2023-05-04T12:02:59.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/756/openapi-python-client/issues/756"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:943e99c9-a523-22d1-f32c-6aed00c0bac9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B3C2:BCA9F:2D2E86A:3C7FE38:69807ED4 |
| html-safe-nonce | 86bc575570a9155993453250ff2bb199761e9e05ea428b5442b57327a14a7f94 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCM0MyOkJDQTlGOjJEMkU4NkE6M0M3RkUzODo2OTgwN0VENCIsInZpc2l0b3JfaWQiOiI4Mjc1MzYyNzUzNjk1MjE5NDEyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 50fc9e7b0577b2149bef0be557bbe4b9f1729289f0a963dd75c0c75e3d7c90c0 |
| hovercard-subject-tag | issue:1695875013 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/openapi-generators/openapi-python-client/756/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bd4c1726005406678a76ce8c4d69fd5aab2f4a1d6b0a6b291f59289356d94530/openapi-generators/openapi-python-client/issues/756 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bd4c1726005406678a76ce8c4d69fd5aab2f4a1d6b0a6b291f59289356d94530/openapi-generators/openapi-python-client/issues/756 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | diesieben07 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4590f1c00c56e5b3a3460b81e4236454a157a2159793b09a2ddee090670e75fb |
| turbo-cache-control | no-preview |
| go-import | github.com/openapi-generators/openapi-python-client git https://github.com/openapi-generators/openapi-python-client.git |
| octolytics-dimension-user_id | 84925606 |
| octolytics-dimension-user_login | openapi-generators |
| octolytics-dimension-repository_id | 240776275 |
| octolytics-dimension-repository_nwo | openapi-generators/openapi-python-client |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 240776275 |
| octolytics-dimension-repository_network_root_nwo | openapi-generators/openapi-python-client |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 242fda17fe105562f9eb7fe91a7a0dca07c527b3 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width