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
Support multipart/form-data as opt-in feature #172
Conversation
1f9e77f
to
31469ad
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great! Thanks for the contribution 🚀 I just added some notes about naming of the property and docs, but I am looking forward to shipping this as part of 0.2.0 🙌
Docs/ConfigOptions.md
Outdated
@@ -103,6 +103,7 @@ Below you can find the complete documentation for all available options. | |||
- [entities](#renameentities) | |||
- [operations](#renameoperations) | |||
- [collectionElements](#renamecollectionelements) | |||
- [useDataForMultipartFormDataRequestBody](#usedataformultipartformdatarequestbody) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the option should have been a part of paths
🤔
I noticed that the option only applies to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ainame! These changes look good to me 🚀
Is there anything else that needs doing to support this or is it ready to merge?
No. The blocker was only me figuring out how to encode an |
Background
I want to add multipart/form-data support to CreateAPI. The typical reason people need
multipart/form-data
is the lack of native support for binary data in JSON format. Let’s say you have an endpoint to post not only a comment but also a photo, you would need to send both data in a payload and want to usemultipart/form-data
. Looking at the codebase, currently, CreateAPI seems to postpone supporting the format. When an endpoint supports onlymultipart/form-data
in the request body, it becomes Data type. That leaves CreateAPI unusable for such an endpoint.CreateAPI/Sources/CreateAPI/Generator/Generator+Paths.swift
Line 7 in 2084450
CreateAPI/Sources/CreateAPI/Generator/Generator+Schemas.swift
Line 6 in d9a7f7d
I can see why it wasn't supported initially because https://github.com/kean/Get doesn't support request body serialization other than
JSONEncoder
.What I changed
I added a new option called
useStructuredMultipartFormDataRequest
to have CreateAPI generate a structured request body, just like normalapplication/json
based requests. The default value isfalse
. Nothing is affected by existing users here. Previously it was limited to just a Data but now it’s easier to compose data with a documented structure. The idea is that once this was implemented, the responsibility is down toCreateAPI
users to handle encoding request body. You would need something likeMultipartFormDataEncoder
instead ofJSONEncoder
in their HTTP clients.This is an example of what a
multipart/form-data
request looks like when the option is enabled.31469ad#diff-b304b36e06a0c0a28709263be23a00d9ab31e810f924da2a1468e1109e277227
And also, I changed the default type for binary data from
String
toData
. Despite the JSON format doesn’t support binary data,JSONEncoder
andJSONDecoder
does supportData
type using Base64 encoding/decoding.Data
would still work with existingJSONEncoder
andJSONDecoder
.This part would be a breaking change. Given the version is still
0.1.1
and theoretically, CreateAPI users have never utilised binary data in their request body just yet (sincemultipart/form-data
support isn't in place!), I hope this is an acceptable change🙏Another thought on using
Data
for binary field. In amultipart/form-data
request, we normally also need to specify a MIME type.Data
type alone can do nothing to identify the type. I think we can solve that by leveraging customstruct
type having bothmimeType
anddata
properties or assuming MIME type from magic bytes, like https://github.com/sendyhalim/Swime.