Title: Invalid python generated when using nested dictionaries · Issue #266 · openapi-generators/openapi-python-client · GitHub
Open Graph Title: Invalid python generated when using nested dictionaries · Issue #266 · openapi-generators/openapi-python-client
X Title: Invalid python generated when using nested dictionaries · Issue #266 · openapi-generators/openapi-python-client
Description: Describe the bug Using openapi-python-client on a valid FastAPI/pydantic API is generating invalid client library on 0.7.2 when there is a somewhat complex set of nested Dict and Union types. To Reproduce Put the following in a main.py f...
Open Graph Description: Describe the bug Using openapi-python-client on a valid FastAPI/pydantic API is generating invalid client library on 0.7.2 when there is a somewhat complex set of nested Dict and Union types. To Re...
X Description: Describe the bug Using openapi-python-client on a valid FastAPI/pydantic API is generating invalid client library on 0.7.2 when there is a somewhat complex set of nested Dict and Union types. To Re...
Opengraph URL: https://github.com/openapi-generators/openapi-python-client/issues/266
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Invalid python generated when using nested dictionaries","articleBody":"**Describe the bug**\r\nUsing `openapi-python-client` on a valid FastAPI/pydantic API is generating invalid client library on 0.7.2 when there is a somewhat complex set of nested `Dict` and `Union` types.\r\n\r\n**To Reproduce**\r\nPut the following in a main.py\r\n```\r\nfrom typing import Any, Dict, List, Union\r\nfrom fastapi import FastAPI\r\nfrom pydantic import (\r\n BaseModel,\r\n StrictBool,\r\n StrictInt,\r\n StrictFloat,\r\n StrictStr,\r\n)\r\n\r\napp = FastAPI()\r\n\r\nJSONValue = Union[\r\n Dict[str, Any], List[Any], StrictBool, StrictFloat, StrictInt, StrictStr, None\r\n]\r\nJSONDict = Dict[str, JSONValue]\r\n\r\nclass ItemMapResource(BaseModel):\r\n items: JSONDict\r\n\r\n@app.get(\"/\", response_model=ItemMapResource)\r\ndef read_item():\r\n return ItemMapResource(items={})\r\n```\r\n\r\nRun it like this:\r\n```\r\n uvicorn main:app --reload --port 4000\r\n```\r\n\r\nGenerate a client library like this\r\n```\r\nopenapi-python-client generate --url http://localhost:4000/openapi.json\r\n```\r\n\r\n**Expected behavior**\r\nShould generate valid python library\r\n\r\n**Actual behavior**\r\nGenerates code with broken indents which fails to execute:\r\n```\r\n@attr.s(auto_attribs=True)\r\nclass ItemMapResourceItems:\r\n \"\"\" \"\"\"\r\n additional_properties: Dict[str, Union[ItemMapResourceItemsAdditionalProperty, List[None], bool, float, int, str]] = attr.ib(init=False, factory=dict)\r\n\r\n\r\n def to_dict(self) -\u003e Dict[str, Any]:\r\n\r\n field_dict: Dict[str, Any] = {}\r\n for prop_name, prop in self.additional_properties.items():\r\n if isinstance(prop, ItemMapResourceItemsAdditionalProperty):\r\n field_dict[prop_name] = prop.to_dict()\r\n\r\n elif isinstance(prop, List[None]):\r\n field_dict[prop_name] = []\r\n for additional_property_item_data in prop:\r\n additional_property_item = None\r\n\r\n field_dict[prop_name].append(additional_property_item)\r\n\r\n\r\n\r\n\r\n elif isinstance(prop, bool):\r\n field_dict[prop_name] = prop\r\n elif isinstance(prop, float):\r\n field_dict[prop_name] = prop\r\n elif isinstance(prop, int):\r\n field_dict[prop_name] = prop\r\n else:\r\n field_dict[prop_name] = prop\r\n\r\n field_dict.update({\r\n })\r\n\r\n return field_dict\r\n\r\n```\r\nNote the line `if isinstance(prop, ItemMapResourceItemsAdditionalProperty):` is indented incorrectly.\r\n\r\n\r\n\r\n**OpenAPI Spec File**\r\n```\r\n{\"openapi\":\"3.0.2\",\"info\":{\"title\":\"FastAPI\",\"version\":\"0.1.0\"},\"paths\":{\"/\":{\"get\":{\"summary\":\"Read Item\",\"operationId\":\"read_item__get\",\"responses\":{\"200\":{\"description\":\"Successful Response\",\"content\":{\"application/json\":{\"schema\":{\"$ref\":\"#/components/schemas/ItemMapResource\"}}}}}}}},\"components\":{\"schemas\":{\"ItemMapResource\":{\"title\":\"ItemMapResource\",\"required\":[\"items\"],\"type\":\"object\",\"properties\":{\"items\":{\"title\":\"Items\",\"type\":\"object\",\"additionalProperties\":{\"anyOf\":[{\"type\":\"object\"},{\"type\":\"array\",\"items\":{}},{\"type\":\"boolean\"},{\"type\":\"number\"},{\"type\":\"integer\"},{\"type\":\"string\"}]}}}}}}}\r\n```\r\n\r\n**Desktop (please complete the following information):**\r\n - OS: Ubuntu 20.04\r\n - Python Version: 3.8.3\r\n - openapi-python-client version 0.7.2\r\n\r\n**Additional context**\r\nThis worked fine with 0.7.0, so I suspect a regression from #252 ","author":{"url":"https://github.com/joshzana","@type":"Person","name":"joshzana"},"datePublished":"2020-12-15T01:06:45.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/266/openapi-python-client/issues/266"}
| 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:3fe7db1c-d2b6-6bdc-7849-5664536723bf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D83C:684C3:38C839:4A7386:6980092D |
| html-safe-nonce | e23ca40de4503be3af80bc00baaa541b2003d34f42b469fcecde078cfdd241f4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEODNDOjY4NEMzOjM4QzgzOTo0QTczODY6Njk4MDA5MkQiLCJ2aXNpdG9yX2lkIjoiMTUzMTUzMTg3NTc3MDY5ODAyOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4a0f7d32fda019ef278eaf066a548da01f5c87aad6156a18e5aa74f0dda5a818 |
| hovercard-subject-tag | issue:767064858 |
| 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/266/issue_layout |
| twitter:image | https://opengraph.githubassets.com/466dc66c3fafc2f67b90bb0abfe9774375a9be886fe5bd8628ed090782d00a83/openapi-generators/openapi-python-client/issues/266 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/466dc66c3fafc2f67b90bb0abfe9774375a9be886fe5bd8628ed090782d00a83/openapi-generators/openapi-python-client/issues/266 |
| og:image:alt | Describe the bug Using openapi-python-client on a valid FastAPI/pydantic API is generating invalid client library on 0.7.2 when there is a somewhat complex set of nested Dict and Union types. To Re... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | joshzana |
| 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