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)
- HTTPTypes linker error if multiple targets use open API generator HOT 2
- Getting "any has no effect on concrete type 'Error'" HOT 6
- Need to be able to support multiple OpenAPI specs HOT 2
- Tutorial doc text color issue on light theme HOT 1
- Check for updated spec before generating HOT 2
- Warnings due to `@frozen` attribute on non-public enums with Xcode 16 Beta 3 HOT 5
- Support multiple contentTypes in encoding multipart/form-data HOT 7
- multipart/form-data request body schemas with additionalProperties are a bit confusing to use, also doesn't compile HOT 7
- Multipart generated enum case naming is inconsistent (other vs additionalProperties) HOT 3
- Add `consuming` Modifier to Middleware Intercept Method Parameters HOT 8
- Hyphen in path variable name results in static path variable name in url HOT 4
- The output formatting of JSON encoding/decoding should be more configurable HOT 1
- HTTPBody does not conform to Decodable? HOT 2
- [Support enumUnknownDefaultCase] Is there a way to add default case for generated enums? HOT 5
- Add option to set a status code for server error / runtime error on a case by case basis HOT 1
- Generator lacks necessary write permissions HOT 5
- Sharing schemas with main application HOT 12
- Error during build: Injecting stub binary into codeless framework HOT 4
- Optimising build time HOT 4
- Different Return Types Depending on the Request Made to the /chat/completions Endpoint in the OpenAI OpenAPI HOT 4
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.