OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.64k stars 6.53k forks source link

Incorrect generation of Retrofit interfaces for file uploads #19693

Open LaurentTreguier opened 3 weeks ago

LaurentTreguier commented 3 weeks ago

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:

      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
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.

wing328 commented 3 days ago

@LaurentTreguier can you please file a PR when you've time? we'll review the fix accordingly.

thank you