openapi-generators / openapi-generators/openapi-python-client
String Binary File is just expecting BytesIO and not filename and mime-type
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2k
- Forks
- 293
- Avg merge
- 34m
- Merged PRs (30d)
- 1
Description
I have Java OpenAPI server which provides an upload endpoint (POST) that expect as schema a FileRequest ( schema = @Schema(implementation = FileRequest.class))
The FileRequest looks like:
@Value.Immutable
@JsonDeserialize(builder = com.mydata.request.FileRequest.Builder.class)
public abstract class FileRequest implements FileFields {
@Schema(type = "string", format = "binary", description = "File")
@JsonProperty
public abstract String file();
public static class Builder extends ImmutableFileRequest.Builder {
}
}
Looking at the generated code, the models looks good. This is the relevant part on types.py:
@attr.s(auto_attribs=True)
class File:
""" Contains information for file uploads """
payload: BinaryIO
file_name: Optional[str] = None
mime_type: Optional[str] = None
def to_tuple(self) -> FileJsonType:
""" Return a tuple representation that httpx will accept for multipart/form-data """
return self.file_name, self.payload, self.mime_type
T = TypeVar("T")
However, looking at the upload endpoint request, it doesn't expect the Optional values above:
@classmethod
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
d = src_dict.copy()
_file = d.pop("file", UNSET)
file: Union[Unset, File]
if isinstance(_file, Unset):
file = UNSET
else:
file = File(
payload = BytesIO(_file)
)
Notice that the File expects only the binary data, losing the filename and mimetype.
Expected behavior
I would expect that the generated code would look something like below, which I patched and am using currently:
else:
file = File(
payload = BytesIO(_file['payload']),
file_name=_file['filename']
)
Desktop (please complete the following information):
- OS: Linux, but irrelevant
- openapi-python-client version: 0.11.1
- Python 3.8.10
- openapi: 3.0.1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the generated types.py and the File.from_dict code shown in the issue, then trace how binary string schemas are represented during response or request parsing. Reproduce the supplied FileRequest upload case and verify that the resulting File preserves payload, filename, and MIME type rather than only the binary data.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100