openapi-generator
openapi-generator copied to clipboard
Incorrect generation of Retrofit interfaces for file uploads
Bug Report Checklist
- [x] Have you provided a full/minimal spec to reproduce the issue?
- [x] Have you validated the input using an OpenAPI validator (example)?
- [ ] Have you tested with the latest master to confirm the issue still exists?
- [x] Have you searched for related issues/PRs?
- [ ] What's the actual output vs expected output?
- [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
I have a project using the Kotlin generator and jvm-retrofit2
as the library. For file uploads, my OpenAPI schema has the following request body definition:
requestBody:
content:
application/octet-stream:
schema:
format: binary
type: string
required: true
The resulting interface uses java.io.File
as the body for the Retrofit interface. However, Retrofit doesn't support File
for file uploads out of the box, resulting in an exception at runtime.
openapi-generator version
7.8.0
OpenAPI declaration file content or url
https://github.com/fyreplace/fyreplace-android/blob/develop/app/src/main/assets/openapi.yaml
Generation Details
The code is generated using the official Gradle plugin.
Steps to reproduce
- Clone https://github.com/fyreplace/fyreplace-android
- Run
./gradlew openapiGenerate
inside the repository - Check the resulting
app/build/openapi/src/main/kotlin/app/fyreplace/api/UsersEndpointApi.kt
, and look forsetCurrentUserAvatar()
Related issues/PRs
Suggest a fix
A custom Converter for files can be created to handle files in request bodies. I have managed to make my uploads work with the following code:
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import retrofit2.Converter
import retrofit2.Retrofit
import java.io.File
import java.lang.reflect.Type
class FileConverterFactory : Converter.Factory() {
override fun requestBodyConverter(
type: Type,
parameterAnnotations: Array<out Annotation>,
methodAnnotations: Array<out Annotation>,
retrofit: Retrofit
) = when (type) {
File::class.java -> Converter<File, RequestBody> { it.asRequestBody(null) }
else -> null
}
companion object {
fun create() = FileConverterFactory()
}
}
The equivalent Java implementation would look something like this:
import androidx.annotation.Nullable;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.RequestBody;
import retrofit2.Converter;
import retrofit2.Retrofit;
public class FileConverterFactory extends Converter.Factory {
@Nullable
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
return type == File.class
? (File file) -> RequestBody.create(file, null)
: null;
}
public static FileConverterFactory create() {
return new FileConverterFactory();
}
}
Adding an instance of this FileConverterFactory
to the list of converter factories when creating the API client makes Retrofit perfectly happy to send File
objects in its requests.