Skip to content

fix: import Optional when properties are nullable or not required #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## 0.5.5 - Unreleased
### Fixes
- Improved trailing comma handling in endpoint generation (#178 & #179). Thanks @dtkav!
- `Optional` is now properly imported for `nullable` fields (#177 & #180). Thanks @dtkav!


## 0.5.4 - 2020-08-29
Expand Down
4 changes: 2 additions & 2 deletions end_to_end_tests/fastapi_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Dict, List, Union

from fastapi import APIRouter, Body, FastAPI, File, Header, Query, UploadFile
from pydantic import BaseModel
from pydantic import BaseModel, Field
from starlette.responses import FileResponse

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

an_enum_value: AnEnum
nested_list_of_enums: List[List[DifferentEnum]] = []
some_dict: Dict[str, str]
some_dict: Dict[str, str] = Field(..., nullable=True)
aCamelDateTime: Union[datetime, date]
a_date: date

Expand Down
3 changes: 2 additions & 1 deletion end_to_end_tests/fastapi_app/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,8 @@
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"nullable": true
},
"aCamelDateTime": {
"title": "Acameldatetime",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AModel:
""" A Model for testing all the ways custom objects can be used """

an_enum_value: AnEnum
some_dict: Dict[Any, Any]
some_dict: Optional[Dict[Any, Any]]
a_camel_date_time: Union[datetime.datetime, datetime.date]
a_date: datetime.date
nested_list_of_enums: Optional[List[List[DifferentEnum]]] = field(
Expand Down
2 changes: 1 addition & 1 deletion openapi_python_client/parser/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def get_imports(self, *, prefix: str) -> Set[str]:
Args:
prefix: A prefix to put before any relative (local) module names.
"""
if not self.required:
if self.nullable or not self.required:
return {"from typing import Optional"}
return set()

Expand Down