From 85249959bf48ed8f3b9d083a85cc49e3514ddded Mon Sep 17 00:00:00 2001 From: Ricardo Mariano Date: Thu, 18 Nov 2021 01:15:44 +1100 Subject: [PATCH] fix: do not require List to be bound when using TypedClientFactory stand-alone (#2218) When an app does not use TypedHttpClientModule and tries to use TypedClientFactory, it asks for lists of client interceptors. These are usually bound to an empty list (new multibinder) in TypedHttpClientModule, so without that module bound it fails. --- .../kotlin/misk/client/TypedHttpClientModule.kt | 14 ++++++++------ .../test/kotlin/misk/client/TypedHttpClientTest.kt | 12 ++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/misk/src/main/kotlin/misk/client/TypedHttpClientModule.kt b/misk/src/main/kotlin/misk/client/TypedHttpClientModule.kt index 087a8ddd15c..fb35e98017a 100644 --- a/misk/src/main/kotlin/misk/client/TypedHttpClientModule.kt +++ b/misk/src/main/kotlin/misk/client/TypedHttpClientModule.kt @@ -144,13 +144,15 @@ class TypedClientFactory @Inject constructor() { // Use Providers for the interceptors so Guice can properly detect cycles when apps inject // an HTTP Client in an Interceptor. // https://gist.github.com/ryanhall07/e3eac6d2d47b72a4c37bce87219d7ced - @Inject - private lateinit var clientNetworkInterceptorFactories: - Provider> + // Also, these are optional because the end user may not have used TypedHttpClientModule at all + // and thus may not have had multibindings provided here. + @Inject(optional = true) + private val clientNetworkInterceptorFactories: + Provider> = Provider { emptyList() } - @Inject - private lateinit var clientApplicationInterceptorFactories: - Provider> + @Inject(optional = true) + private val clientApplicationInterceptorFactories: + Provider> = Provider { emptyList() } @Inject private lateinit var clientMetricsInterceptorFactory: ClientMetricsInterceptor.Factory diff --git a/misk/src/test/kotlin/misk/client/TypedHttpClientTest.kt b/misk/src/test/kotlin/misk/client/TypedHttpClientTest.kt index cac00e9abfa..8b6d8538041 100644 --- a/misk/src/test/kotlin/misk/client/TypedHttpClientTest.kt +++ b/misk/src/test/kotlin/misk/client/TypedHttpClientTest.kt @@ -67,6 +67,18 @@ internal class TypedHttpClientTest { fun buildDynamicClients() { val typedClientFactory = clientInjector.getInstance(TypedClientFactory::class.java) + testBuildAndUseDynamicClient(typedClientFactory) + } + + @Test + fun buildDynamicClientWithoutHavingBuiltAnyOtherClients() { + val injector = Guice.createInjector(MiskTestingServiceModule()) + val typedClientFactory = injector.getInstance(TypedClientFactory::class.java) + + testBuildAndUseDynamicClient(typedClientFactory) + } + + private fun testBuildAndUseDynamicClient(typedClientFactory: TypedClientFactory) { val dinoClient = typedClientFactory.build( HttpClientEndpointConfig(jetty.httpServerUrl.toString()), "dynamicDino"