Skip to content

Commit c006ea0

Browse files
dbantydtkav
andauthored
fix: import Optional when properties are nullable or not required (#181)
Co-authored-by: Daniel Grossmann-Kavanagh <[email protected]>
1 parent 30edc56 commit c006ea0

File tree

5 files changed

+7
-5
lines changed

5 files changed

+7
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## 0.5.5 - Unreleased
99
### Fixes
1010
- Improved trailing comma handling in endpoint generation (#178 & #179). Thanks @dtkav!
11+
- `Optional` is now properly imported for `nullable` fields (#177 & #180). Thanks @dtkav!
1112

1213

1314
## 0.5.4 - 2020-08-29

end_to_end_tests/fastapi_app/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Dict, List, Union
77

88
from fastapi import APIRouter, Body, FastAPI, File, Header, Query, UploadFile
9-
from pydantic import BaseModel
9+
from pydantic import BaseModel, Field
1010
from starlette.responses import FileResponse
1111

1212
app = FastAPI(title="My Test API", description="An API for testing openapi-python-client",)
@@ -44,7 +44,7 @@ class AModel(BaseModel):
4444

4545
an_enum_value: AnEnum
4646
nested_list_of_enums: List[List[DifferentEnum]] = []
47-
some_dict: Dict[str, str]
47+
some_dict: Dict[str, str] = Field(..., nullable=True)
4848
aCamelDateTime: Union[datetime, date]
4949
a_date: date
5050

end_to_end_tests/fastapi_app/openapi.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,8 @@
493493
"type": "object",
494494
"additionalProperties": {
495495
"type": "string"
496-
}
496+
},
497+
"nullable": true
497498
},
498499
"aCamelDateTime": {
499500
"title": "Acameldatetime",

end_to_end_tests/golden-master/my_test_api_client/models/a_model.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class AModel:
1313
""" A Model for testing all the ways custom objects can be used """
1414

1515
an_enum_value: AnEnum
16-
some_dict: Dict[Any, Any]
16+
some_dict: Optional[Dict[Any, Any]]
1717
a_camel_date_time: Union[datetime.datetime, datetime.date]
1818
a_date: datetime.date
1919
nested_list_of_enums: Optional[List[List[DifferentEnum]]] = field(

openapi_python_client/parser/properties.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def get_imports(self, *, prefix: str) -> Set[str]:
6363
Args:
6464
prefix: A prefix to put before any relative (local) module names.
6565
"""
66-
if not self.required:
66+
if self.nullable or not self.required:
6767
return {"from typing import Optional"}
6868
return set()
6969

0 commit comments

Comments
 (0)