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

ScalarsConverterFactory is applied regardless Content-Type #4213

Open
snedbalek-paylocity opened this issue Aug 20, 2024 · 0 comments
Open

Comments

@snedbalek-paylocity
Copy link

By the documentation of ScalarsConverterFactory, I'd expect the converter to be applied only on text/plain:

A {@linkplain Converter.Factory converter} for strings and both primitives and their boxed types to {@code text/plain} bodies.

But that is ignored and it consumes even application/json as shown in this test case:

import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.GET

class RetrofitBug {
    @JvmField
    @Rule
    val server: MockWebServer = MockWebServer()

    internal interface Service {
        @GET("/path")
        fun foo(): Call<String>
    }

    @Test
    @Throws(Exception::class)
    fun test() {
        val retrofit = Retrofit.Builder()
            .baseUrl(server.url("/"))
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(MoshiConverterFactory.create())
            .build()
        val example = retrofit.create(
            Service::class.java
        )

        server.enqueue(
            MockResponse()
                .addHeader("Content-Type", "application/json")
                .setBody("\"Hi\"")
        )

        val call = example.foo()
        val response = call.execute()
        Assert.assertEquals("application/json", response.headers()["Content-Type"])
        Assert.assertEquals("Hi", response.body())
    }
}

The shared case won't fail if the factories are reversed:

            .addConverterFactory(MoshiConverterFactory.create())
            .addConverterFactory(ScalarsConverterFactory.create())

But that has implications on body serialization.
Is there a way how to restrict converter only to certain content type? Is this a bug? Should the docu be updated?

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

No branches or pull requests

1 participant