Title: New behavior around optional nested objects breaks deserialization when explicit null is returned · Issue #343 · openapi-generators/openapi-python-client · GitHub
Open Graph Title: New behavior around optional nested objects breaks deserialization when explicit null is returned · Issue #343 · openapi-generators/openapi-python-client
X Title: New behavior around optional nested objects breaks deserialization when explicit null is returned · Issue #343 · openapi-generators/openapi-python-client
Description: Describe the bug We use FastAPI as an API server and generate a python client with open-api-client that is then used to run integration tests. Our team is using 0.7.3 but I was trying to update to 0.8.0. When doing so, we ran into the fo...
Open Graph Description: Describe the bug We use FastAPI as an API server and generate a python client with open-api-client that is then used to run integration tests. Our team is using 0.7.3 but I was trying to update to ...
X Description: Describe the bug We use FastAPI as an API server and generate a python client with open-api-client that is then used to run integration tests. Our team is using 0.7.3 but I was trying to update to ...
Opengraph URL: https://github.com/openapi-generators/openapi-python-client/issues/343
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"New behavior around optional nested objects breaks deserialization when explicit null is returned","articleBody":"**Describe the bug**\r\nWe use FastAPI as an API server and generate a python client with `open-api-client` that is then used to run integration tests. Our team is using 0.7.3 but I was trying to update to 0.8.0. When doing so, we ran into the following exception running our test suite:\r\n```\r\n/usr/local/lib/python3.8/site-packages/falkon_sdk/api/observations/get_observations.py:73: in _parse_response\r\n response_200 = ObservationResult.from_dict(response.json())\r\n/usr/local/lib/python3.8/site-packages/falkon_sdk/models/observation_result.py:104: in from_dict\r\n metric_change = ObservationFactWindowSummaryResource.from_dict(\r\n/usr/local/lib/python3.8/site-packages/falkon_sdk/models/observation_fact_window_summary_resource.py:156: in from_dict\r\n weighted_change_unfiltered = WeightedChange.from_dict(\r\n_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \r\n\r\ncls = \u003cclass 'falkon_sdk.models.weighted_change.WeightedChange'\u003e\r\nsrc_dict = None\r\n\r\n @classmethod\r\n def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -\u003e T:\r\n\u003e d = src_dict.copy()\r\nE AttributeError: 'NoneType' object has no attribute 'copy'\r\n```\r\n\r\nI was able to narrow down the problem to handling of `Optional[T]` objects within response models. Here is a minimal repro:\r\n\r\n**To Reproduce**\r\nSteps to reproduce the behavior:\r\n1. Make a main.py file with the following contents:\r\n```\r\nfrom typing import Optional\r\nfrom fastapi import FastAPI\r\nfrom pydantic import BaseModel\r\n\r\napp = FastAPI()\r\n\r\nclass ItemResource(BaseModel):\r\n id: int\r\n\r\nclass ResourceWithOptional(BaseModel):\r\n item: Optional[ItemResource]\r\n\r\n@app.get(\"/\", response_model=ResourceWithOptional)\r\ndef read_item():\r\n return ResourceWithOptional(item=None)\r\n```\r\n2. Start it up in uvicorn\r\n```\r\nuvicorn main:app \u0026\r\n```\r\n2. Curling that endpoint returns this:\r\n```\r\ncurl -s http://localhost:8000/\r\nINFO: 127.0.0.1:32770 - \"GET / HTTP/1.1\" 200 OK\r\n{\"item\":null}\r\n```\r\n\r\n3. Generate a client for this:\r\n```\r\nopenapi-python-client generate --url http://localhost:8000/openapi.json\r\n```\r\n\r\n4. Look at the generated `resource_with_optional.py` in the `from_dict` method:\r\n```\r\n item: Union[ItemResource, Unset] = UNSET\r\n _item = d.pop(\"item\", UNSET)\r\n if not isinstance(_item, Unset):\r\n item = ItemResource.from_dict(_item)\r\n```\r\n\r\n**Expected behavior**\r\nI expect that this allows for an explicit `null` to be returned, as was the case in openapi-python-client==0.7.3, where the generated code looks like this:\r\n```\r\n item: Union[ItemResource, Unset] = UNSET\r\n _item = d.pop(\"item\", UNSET)\r\n if _item is not None and not isinstance(_item, Unset):\r\n item = ItemResource.from_dict(cast(Dict[str, Any], _item))\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/ResourceWithOptional\"}}}}}}}},\"components\":{\"schemas\":{\"ItemResource\":{\"title\":\"ItemResource\",\"required\":[\"id\"],\"type\":\"object\",\"properties\":{\"id\":{\"title\":\"Id\",\"type\":\"integer\"}}},\"ResourceWithOptional\":{\"title\":\"ResourceWithOptional\",\"type\":\"object\",\"properties\":{\"item\":{\"$ref\":\"#/components/schemas/ItemResource\"}}}}}}\r\n```\r\n\r\n**Desktop (please complete the following information):**\r\n - OS: Ubuntu Linux 20.04\r\n - Python Version: 3.8.6\r\n - openapi-python-client version 0.8.0\r\n\r\n**Additional context**\r\nFastAPI has support for excluding nulls from responses with `response_model_exclude_none` but this means changing how our API works to suit the sdk generator, which I would prefer not to have to do.\r\nThe new behavior I was referring to seems to have come in with #334 \r\n","author":{"url":"https://github.com/joshzana","@type":"Person","name":"joshzana"},"datePublished":"2021-03-03T05:48:49.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/343/openapi-python-client/issues/343"}
| 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:b9b548ac-e39e-eeb3-7dde-b76fbf3cbdb0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E592:3836D9:7A2B697:A29AC38:6980C089 |
| html-safe-nonce | 0e43a134ed805a2a286956fd1f96c07a033d4e94b31d446df0e30c8ce97d461e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNTkyOjM4MzZEOTo3QTJCNjk3OkEyOUFDMzg6Njk4MEMwODkiLCJ2aXNpdG9yX2lkIjoiODExNDE1MDcxNjkwMzc1MzA1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 589cfac623f34201d458d18d9dfb4d92f2e5ba318a3da4008921e42c8de80acf |
| hovercard-subject-tag | issue:820767495 |
| 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/343/issue_layout |
| twitter:image | https://opengraph.githubassets.com/15fe8c53089cee88dc2245234cf031d1333e2fdd040611185299dd22baf1a3d1/openapi-generators/openapi-python-client/issues/343 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/15fe8c53089cee88dc2245234cf031d1333e2fdd040611185299dd22baf1a3d1/openapi-generators/openapi-python-client/issues/343 |
| og:image:alt | Describe the bug We use FastAPI as an API server and generate a python client with open-api-client that is then used to run integration tests. Our team is using 0.7.3 but I was trying to update to ... |
| 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 | fb936832e40deb3175641d9cfedc8432372f91b0bc899576222953095fc7879e |
| 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 | 3e9b7987a76a2b9d08066d02bb64947a61943332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width