Title: Invalid code generated for nullable discriminated union · Issue #958 · openapi-generators/openapi-python-client · GitHub
Open Graph Title: Invalid code generated for nullable discriminated union · Issue #958 · openapi-generators/openapi-python-client
X Title: Invalid code generated for nullable discriminated union · Issue #958 · openapi-generators/openapi-python-client
Description: Describe the bug Given the schema below, using the generated code like: from test_client.models.demo import Demo from test_client.models.a import A Demo(example_union=A()).to_dict() fails with: Traceback (most recent call last): File "/U...
Open Graph Description: Describe the bug Given the schema below, using the generated code like: from test_client.models.demo import Demo from test_client.models.a import A Demo(example_union=A()).to_dict() fails with: Tra...
X Description: Describe the bug Given the schema below, using the generated code like: from test_client.models.demo import Demo from test_client.models.a import A Demo(example_union=A()).to_dict() fails with: Tra...
Opengraph URL: https://github.com/openapi-generators/openapi-python-client/issues/958
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Invalid code generated for nullable discriminated union","articleBody":"**Describe the bug**\r\n\r\nGiven the schema below, using the generated code like:\r\n\r\n```python\r\nfrom test_client.models.demo import Demo\r\nfrom test_client.models.a import A\r\n\r\nDemo(example_union=A()).to_dict()\r\n```\r\n\r\nfails with:\r\n\r\n```\r\nTraceback (most recent call last):\r\n File \"/Users/eric/Desktop/test/test.py\", line 4, in \u003cmodule\u003e\r\n Demo(example_union=A()).to_dict()\r\n File \"/Users/eric/Desktop/test/test_client/models/demo.py\", line 32, in to_dict\r\n elif isinstance(self.example_union, Union[\"A\", \"B\"]):\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"/Users/eric/.pyenv/versions/3.12.1/lib/python3.12/typing.py\", line 1564, in __instancecheck__\r\n return self.__subclasscheck__(type(obj))\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"/Users/eric/.pyenv/versions/3.12.1/lib/python3.12/typing.py\", line 1568, in __subclasscheck__\r\n if issubclass(cls, arg):\r\n ^^^^^^^^^^^^^^^^^^^^\r\nTypeError: issubclass() arg 2 must be a class, a tuple of classes, or a union\r\n```\r\n\r\n**OpenAPI Spec File**\r\n\r\n```yaml\r\nopenapi: 3.0.3\r\ninfo:\r\n title: Test\r\n version: 0.0.0\r\n description: test\r\npaths: {}\r\ncomponents:\r\n schemas:\r\n Demo:\r\n type: object\r\n properties:\r\n example_union:\r\n allOf:\r\n - $ref: '#/components/schemas/ExampleUnion'\r\n nullable: true # \u003c-- bug does not happen if this line is removed\r\n A:\r\n type: object\r\n properties:\r\n type:\r\n type: string\r\n B:\r\n type: object\r\n properties:\r\n type:\r\n type: string\r\n ExampleUnion:\r\n oneOf:\r\n - $ref: '#/components/schemas/A'\r\n - $ref: '#/components/schemas/B'\r\n discriminator:\r\n propertyName: type\r\n mapping:\r\n a: '#/components/schemas/A'\r\n b: '#/components/schemas/B'\r\n```\r\n\r\n\r\n**Desktop**\r\n - OS: macOS 14.3\r\n - Python Version: 3.12.1\r\n - openapi-python-client version: 0.17.2\r\n\r\n**Additional context**\r\n\r\nThe failing generated code is:\r\n\r\n```python\r\nisinstance(self.example_union, Union[\"A\", \"B\"])\r\n```\r\n\r\nUsing `instanceof` on a `Union` with quoted types is not allowed:\r\n\r\n```\r\n\u003e\u003e\u003e class A:\r\n... pass\r\n...\r\n\u003e\u003e\u003e class B:\r\n... pass\r\n...\r\n\u003e\u003e\u003e from typing import Union\r\n\u003e\u003e\u003e isinstance(None, Union[A, B])\r\nFalse\r\n\u003e\u003e\u003e isinstance(None, Union[\"A\", \"B\"])\r\nTraceback (most recent call last):\r\n File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\r\n File \"/Users/eric/.pyenv/versions/3.12.1/lib/python3.12/typing.py\", line 1564, in __instancecheck__\r\n return self.__subclasscheck__(type(obj))\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"/Users/eric/.pyenv/versions/3.12.1/lib/python3.12/typing.py\", line 1568, in __subclasscheck__\r\n if issubclass(cls, arg):\r\n ^^^^^^^^^^^^^^^^^^^^\r\nTypeError: issubclass() arg 2 must be a class, a tuple of classes, or a union\r\n```","author":{"url":"https://github.com/codebutler","@type":"Person","name":"codebutler"},"datePublished":"2024-02-09T18:53:43.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/958/openapi-python-client/issues/958"}
| 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:7f360f95-1a65-3503-39a6-9b937fe07847 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9976:10B547:1F7387:271CD4:697F8839 |
| html-safe-nonce | 8f235baf44e64345a1855f9fe5b770f957c697fbe466c3b67c5e01bbef7b53e4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OTc2OjEwQjU0NzoxRjczODc6MjcxQ0Q0OjY5N0Y4ODM5IiwidmlzaXRvcl9pZCI6IjQ1MzU2MTU5MTczODc2NDcwMzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6b7254072b0a6d5211585bcaed8ba94fc69677fb5c5e6134a4e272f4c6aa9b19 |
| hovercard-subject-tag | issue:2127688589 |
| 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/958/issue_layout |
| twitter:image | https://opengraph.githubassets.com/308caa4d0e3e190d5eb9e6aaf6eaf9332646f11f5060564adf587947cb8cf192/openapi-generators/openapi-python-client/issues/958 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/308caa4d0e3e190d5eb9e6aaf6eaf9332646f11f5060564adf587947cb8cf192/openapi-generators/openapi-python-client/issues/958 |
| og:image:alt | Describe the bug Given the schema below, using the generated code like: from test_client.models.demo import Demo from test_client.models.a import A Demo(example_union=A()).to_dict() fails with: Tra... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | codebutler |
| hostname | github.com |
| expected-hostname | github.com |
| None | 60279d4097367e16897439d16d6bbe4180663db828c666eeed2656988ffe59f6 |
| 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 | 7c85641c598ad130c74f7bcc27f58575cac69551 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width