Skip to content

Commit f7d68a9

Browse files
author
Constantinos Symeonides
committed
fix: Optional File field caused missing import error
1 parent d0a542b commit f7d68a9

File tree

6 files changed

+26
-8
lines changed

6 files changed

+26
-8
lines changed

end_to_end_tests/golden_record/my_test_api_client/models/body_upload_file_tests_upload_post.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import attr
55

6-
from ..types import UNSET, File, Unset
6+
from ..types import UNSET, File, FileJsonType, Unset
77

88
T = TypeVar("T", bound="BodyUploadFileTestsUploadPost")
99

@@ -13,11 +13,16 @@ class BodyUploadFileTestsUploadPost:
1313
""" """
1414

1515
some_file: File
16+
some_optional_file: Union[Unset, File] = UNSET
1617
some_string: Union[Unset, str] = "some_default_string"
1718

1819
def to_dict(self) -> Dict[str, Any]:
1920
some_file = self.some_file.to_tuple()
2021

22+
some_optional_file: Union[Unset, FileJsonType] = UNSET
23+
if not isinstance(self.some_optional_file, Unset):
24+
some_optional_file = self.some_optional_file.to_tuple()
25+
2126
some_string = self.some_string
2227

2328
field_dict: Dict[str, Any] = {}
@@ -26,6 +31,8 @@ def to_dict(self) -> Dict[str, Any]:
2631
"some_file": some_file,
2732
}
2833
)
34+
if some_optional_file is not UNSET:
35+
field_dict["some_optional_file"] = some_optional_file
2936
if some_string is not UNSET:
3037
field_dict["some_string"] = some_string
3138

@@ -36,10 +43,16 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
3643
d = src_dict.copy()
3744
some_file = File(payload=BytesIO(d.pop("some_file")))
3845

46+
some_optional_file: Union[Unset, File] = UNSET
47+
_some_optional_file = d.pop("some_optional_file", UNSET)
48+
if not isinstance(_some_optional_file, Unset):
49+
some_optional_file = File(payload=BytesIO(_some_optional_file))
50+
3951
some_string = d.pop("some_string", UNSET)
4052

4153
body_upload_file_tests_upload_post = cls(
4254
some_file=some_file,
55+
some_optional_file=some_optional_file,
4356
some_string=some_string,
4457
)
4558

end_to_end_tests/golden_record/my_test_api_client/types.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ class Response(Generic[T]):
4545
parsed: Optional[T]
4646

4747

48-
__all__ = ["File", "Response", "is_file"]
48+
__all__ = ["File", "Response", "is_file", "FileJsonType"]

end_to_end_tests/openapi.json

+5
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,11 @@
924924
"type": "string",
925925
"format": "binary"
926926
},
927+
"some_optional_file": {
928+
"title": "Some Optional File",
929+
"type": "string",
930+
"format": "binary"
931+
},
927932
"some_string": {
928933
"title": "Some String",
929934
"type": "string",

openapi_python_client/parser/properties/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class FileProperty(Property):
8383

8484
_type_string: ClassVar[str] = "File"
8585
# Return type of File.to_tuple()
86-
_json_type_string: ClassVar[str] = "Tuple[Optional[str], Union[BinaryIO, TextIO], Optional[str]]"
86+
_json_type_string: ClassVar[str] = "FileJsonType"
8787
template: ClassVar[str] = "file_property.py.jinja"
8888

8989
def get_imports(self, *, prefix: str) -> Set[str]:
@@ -95,7 +95,7 @@ def get_imports(self, *, prefix: str) -> Set[str]:
9595
back to the root of the generated client.
9696
"""
9797
imports = super().get_imports(prefix=prefix)
98-
imports.update({f"from {prefix}types import File", "from io import BytesIO"})
98+
imports.update({f"from {prefix}types import File, FileJsonType", "from io import BytesIO"})
9999
return imports
100100

101101

openapi_python_client/templates/types.py.jinja

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ class Response(Generic[T]):
4646
parsed: Optional[T]
4747

4848

49-
__all__ = ["File", "Response", "is_file"]
49+
__all__ = ["File", "Response", "is_file", "FileJsonType"]

tests/test_parser/test_properties/test_init.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,21 @@ def test_get_imports(self):
162162
p = FileProperty(name="test", required=True, default=None, nullable=False)
163163
assert p.get_imports(prefix=prefix) == {
164164
"from io import BytesIO",
165-
"from ...types import File",
165+
"from ...types import File, FileJsonType",
166166
}
167167

168168
p = FileProperty(name="test", required=False, default=None, nullable=False)
169169
assert p.get_imports(prefix=prefix) == {
170170
"from io import BytesIO",
171-
"from ...types import File",
171+
"from ...types import File, FileJsonType",
172172
"from typing import Union",
173173
"from ...types import UNSET, Unset",
174174
}
175175

176176
p = FileProperty(name="test", required=False, default=None, nullable=True)
177177
assert p.get_imports(prefix=prefix) == {
178178
"from io import BytesIO",
179-
"from ...types import File",
179+
"from ...types import File, FileJsonType",
180180
"from typing import Union",
181181
"from typing import Optional",
182182
"from ...types import UNSET, Unset",

0 commit comments

Comments
 (0)