Skip to content
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

Incorrect generation of Retrofit interfaces for file uploads #19693

Open
3 of 6 tasks
LaurentTreguier opened this issue Sep 26, 2024 · 0 comments
Open
3 of 6 tasks

Incorrect generation of Retrofit interfaces for file uploads #19693

LaurentTreguier opened this issue Sep 26, 2024 · 0 comments

Comments

@LaurentTreguier
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • 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
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant