-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Swift5: Binary / File type generated incorrectly as URL in responses #9308
Comments
@4brunu I've created an issue to discuss the best solution for this. Then we can create a PR based on this. That is interesting. I agree this is a breaking change, but nobody will use URL for binary responses in their app. It simply can't work. The URL object is completely useless currently. In our app we "fixed" it by the type-mapping during generation. Or did I miss something and there is a working use-case for the current implementation? |
@jenswet-el thanks for creating this issue. I have a binary type in swagger file that I consume, that it's used for file upload, and it's working fine with the URL.
|
@4brunu ahhh, you use it as upload. I am talking about download. There it is useless according to our app devs.
Generates: open class func getImage(apiResponseQueue: DispatchQueue = OpenAPIClientAPI.apiResponseQueue, completion: @escaping ((_ data: URL?, _ error: Error?) -> Void)) {
getImageWithRequestBuilder().execute(apiResponseQueue) { result -> Void in
switch result {
case let .success(response):
completion(response.body, nil)
case let .failure(error):
completion(nil, error)
}
}
} Which maps the binary body into the URL if I am correct? I don't do Swift (I do the backend), but I got this feedback from our app devs. |
Oh, I see. |
Could you please give us a sample project for this? I never used URLSession nor AlamoFire, but writing back-end Swift in Vapor, a request's body at the end gets converted to app.post() { request -> Response in
let file = Data(base64Encoded: base64)!
PetAPI.uploadFile(petId: 99, file: file).whenComplete { result in
switch result {
case .success(let uploadFile):
switch uploadFile {
case .http0(let value, _), .http200(let value, _):
print(value.code, value.type, value.message)
}
case .failure(let error):
print(error)
}
}
return Response(status: .ok)
} |
Sure, here you have a sample project that show's the API usage. Lines 65 to 89 in 5d94628
And here you have the URLSession implementation for file upload Lines 424 to 566 in 5d94628
|
@4brunu URLSessionImplementations.swift translates the URLs into IMO, this should be the job of the user to grab the raw data from URLs and not the client library. We could have helpers to assist with that but not to make that the default behavior. CC @jenswet-el @wing328 Lines 491 to 524 in 5d94628
|
|
I have looked into multiple generators, but the only conclusion that I got is that there is no uniformity between them. So maybe we can go forward with this change, and create a feature flag to avoid the breaking change. what do you think of this? c#
java
java play framework
Ruby
Kotlin
Dart
TypeScript
|
@aymanbagabas do you want to create a PR with this change? Or do you want me to do it? |
* [swift5] Map file and binary to Data Fixes: #9308 * Update docs
* [swift5] Map file and binary to Data Fixes: OpenAPITools#9308 * Update docs
After looking at this issue once more, I found the following code. Lines 196 to 229 in 163d13e
Looking at this, the binary download should have always worked. |
You can use HTTP Cats API, https://http.cat/500.jpg for instance |
Do they have a swagger/openapi file? |
No, but it's a simple API. Here's one:
|
Thanks. |
Now that #9595 got merged, the type
That being said, now that the Also if there is any generator library, for example #9583, that needs the |
After some investigation, the test issue when changing the type binary from
URL
toData
, is related to the code in ModelUtils line 659.openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
Lines 2059 to 2062 in 3894aa4
openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java
Lines 643 to 660 in 3894aa4
The binary type will be considered as file type.
To add some interesting bits to the discussion
The Kotlin Client generator, considers binary as BiteArray (or Data in Swift) and file as java.io.File (or URL in Swift)
openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java
Lines 159 to 165 in 3894aa4
The Java Client generator, considers binary as byte[], and file as java.io.File.
Since this is a big change on how files/binary is treated in the Swift Generator that is also a breaking change, and doing things differently from the other generators worries me a bit, so I just would like to be sure that this is the right move, but let's continue this discussion on the new PR..
Originally posted by @4brunu in #9206 (comment)
The text was updated successfully, but these errors were encountered: