Comments (11)
Oh I see the issue - your body is not marked as required: true
, so it will be skipped. The generator emits a warning, which you should see in Xcode/in build output. After you add the missing required key, it'll show up.
The specification explicitly states that a request multipart body must be required, and never optional. Can you check if that fixes your issue?
from swift-openapi-generator.
@erikhric It looks correct to me.
You should be able to initialize the Body with the multipartBody case. What error are you hitting?
from swift-openapi-generator.
You can create an HTTPBody value from data using let httpBody = HTTPBody(data)
.
from swift-openapi-generator.
@czechboy0 thank you! I'll be in Prague next month, can I buy you a beer? 🍺 😅
from swift-openapi-generator.
Hi @agattringer, I'd like to be able to reproduce it - can you provide the full operation, so including the responses
key, please?
from swift-openapi-generator.
Thank you very much for your quick answer!
required: true
actually fixes the issue.
from swift-openapi-generator.
Great! 🙏
from swift-openapi-generator.
Hey @agattringer, how did you initialize body for your multipart request? Can you check if my schema and generated code satisfies everything neccessary for upload? I'm going in circles and obiously missing something.
schema:
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
file:
type: string
description: 'The image file. Max size: 64 MB, Min dimensions: 640x640'
format: binary
required: true
code:
@frozen internal enum Body: Sendable, Hashable {
/// - Remark: Generated from `#/paths/accounts/{account}/content/basic/image/upload/POST/requestBody/multipartForm`.
@frozen internal enum multipartFormPayload: Sendable, Hashable {
/// - Remark: Generated from `#/paths/accounts/{account}/content/basic/image/upload/POST/requestBody/multipartForm/file`.
internal struct filePayload: Sendable, Hashable {
internal var body: OpenAPIRuntime.HTTPBody
/// Creates a new `filePayload`.
///
/// - Parameters:
/// - body:
internal init(body: OpenAPIRuntime.HTTPBody) {
self.body = body
}
}
case file(OpenAPIRuntime.MultipartPart<Operations.post_sol_accounts_sol__lcub_account_rcub__sol_content_sol_basic_sol_image_sol_upload.Input.Body.multipartFormPayload.filePayload>)
case undocumented(OpenAPIRuntime.MultipartRawPart)
}
/// - Remark: Generated from `#/paths/accounts/{account}/content/basic/image/upload/POST/requestBody/content/multipart\/form-data`.
case multipartForm(OpenAPIRuntime.MultipartBody<Operations.post_sol_accounts_sol__lcub_account_rcub__sol_content_sol_basic_sol_image_sol_upload.Input.Body.multipartFormPayload>)
}
from swift-openapi-generator.
Problem is obviously me, not the generator - I failed to find example for uploading an image. I don't know how to initialize Input for upload operation:
func uploadImage(accountId: Int, imageData: Data) {
//this gives error Initializer 'init(_:)' requires the types 'Operations.post_sol_accounts_sol__lcub_account_rcub__sol_content_sol_basic_sol_image_sol_upload.Input.Body.multipartFormPayload' and 'Data.Element' (aka 'UInt8') be equivalent
let multi: MultipartBody<Operations.post_sol_accounts_sol__lcub_account_rcub__sol_content_sol_basic_sol_image_sol_upload.Input.Body.multipartFormPayload> = .init(imageData)
// Cannot convert value of type 'MultipartBody<Operations.post_sol_accounts_sol__lcub_account_rcub__sol_content_sol_basic_sol_image_sol_upload.Input.Body.multipartFormPayload>.Type' to expected argument type 'MultipartBody<Operations.post_sol_accounts_sol__lcub_account_rcub__sol_content_sol_basic_sol_image_sol_upload.Input.Body.multipartFormPayload>'
let input: Operations.post_sol_accounts_sol__lcub_account_rcub__sol_content_sol_basic_sol_image_sol_upload
.Input = .init(path: .init(account: accountId),
body: .multipartForm(MultipartBody<Operations.post_sol_accounts_sol__lcub_account_rcub__sol_content_sol_basic_sol_image_sol_upload.Input.Body.multipartFormPayload>))
}
from swift-openapi-generator.
@erikhric check out the last example here: https://github.com/apple/swift-openapi-generator/blob/main/Examples/various-content-types-client-example/Sources/ContentTypesClient/ContentTypesClient.swift
A multipart body can be initialized using an array of parts. Each of the parts is named after the part name in the OpenAPI document. The part value can then be the image data itself.
from swift-openapi-generator.
I get Cannot convert value of type 'Data' to expected argument type 'HTTPBody'
error when passing image data.
If I convert it to string
let stringData = String(decoding: imageData, as: UTF8.self)
let multi: MultipartBody<Operations.post_sol_accounts_sol__lcub_account_rcub__sol_content_sol_basic_sol_image_sol_upload.Input.Body.multipartFormPayload> = [
.file(.init(payload: .init(body: stringData), filename: "image")) //Cannot convert value of type 'String' to expected argument type 'HTTPBody'
]
It only works directly with string in place
.file(.init(payload: .init(body: "this works, or at least it compiles"), filename: "image"))
from swift-openapi-generator.
Related Issues (20)
- Generated enums should conform to `CaseIterable` HOT 7
- Is it possible to use component refs in paths that accept multiple content types? HOT 8
- A way to group features or services to allow middleware to be applied to groups HOT 4
- Manual invocation of the generator and SPM warnings HOT 5
- Null property type with quotes not generated HOT 14
- Support for XML request body and response body HOT 7
- Support schemas.type: string, format: uri generates URL property not String HOT 3
- How to support OAuth2 Password flows? HOT 3
- allOf plus properties HOT 2
- OpenAPI shared components HOT 4
- bug/regression: latest yams results in incomplete generation of types HOT 10
- Logging and HTTPBody HOT 3
- Merging oneOf with many compatible enums of a single value HOT 10
- anyOf encoder does not include discriminator's `parameterName` leading to incorrect encoding HOT 2
- Is there a workaround to deal with slow compilation times for a very large openapi file? HOT 3
- Empty arrays are omitted from query params HOT 5
- Support for `multipart/mixed`? HOT 1
- When a delete path do not have responses content an error generated HOT 2
- Add ability to attach protocols to generated models HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swift-openapi-generator.