OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
[ ] [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:
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.
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.
Bug Report Checklist
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:The resulting interface uses
java.io.File
as the body for the Retrofit interface. However, Retrofit doesn't supportFile
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
./gradlew openapiGenerate
inside the repositoryapp/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:
The equivalent Java implementation would look something like this:
Adding an instance of this
FileConverterFactory
to the list of converter factories when creating the API client makes Retrofit perfectly happy to sendFile
objects in its requests.