Skip to content

Commit

Permalink
fix(java17): add Jackson converter to RestAdapters to avoid Gson (#1174)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
mattgogerly and mergify[bot] committed Sep 20, 2023
1 parent 0e30827 commit 9d053bb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.netflix.spinnaker.igor.config

import com.fasterxml.jackson.databind.ObjectMapper
import com.jakewharton.retrofit.Ok3Client
import com.netflix.spinnaker.config.DefaultServiceEndpoint
import com.netflix.spinnaker.config.okhttp3.OkHttpClientProvider
Expand All @@ -30,6 +31,7 @@ import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import retrofit.Endpoints
import retrofit.RestAdapter
import retrofit.converter.JacksonConverter

@Configuration
@ConditionalOnProperty(['services.clouddriver.base-url', 'docker-registry.enabled'])
Expand All @@ -42,9 +44,12 @@ class DockerRegistryConfig {
}

@Bean
ClouddriverService dockerRegistryProxyService(OkHttpClientProvider clientProvider,
IgorConfigurationProperties igorConfigurationProperties,
RestAdapter.LogLevel retrofitLogLevel) {
ClouddriverService dockerRegistryProxyService(
OkHttpClientProvider clientProvider,
IgorConfigurationProperties igorConfigurationProperties,
RestAdapter.LogLevel retrofitLogLevel,
ObjectMapper objectMapper
) {
def address = igorConfigurationProperties.services.clouddriver.baseUrl ?: 'none'
if (address == 'none') {
null
Expand All @@ -54,6 +59,7 @@ class DockerRegistryConfig {
.setEndpoint(Endpoints.newFixedEndpoint(address))
.setClient(new Ok3Client(clientProvider.getClient(new DefaultServiceEndpoint("clouddriver", address))))
.setLogLevel(retrofitLogLevel)
.setConverter(new JacksonConverter(objectMapper))
.setLog(new Slf4jRetrofitLogger(ClouddriverService))
.build()
.create(ClouddriverService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.netflix.spinnaker.igor.config

import com.fasterxml.jackson.databind.ObjectMapper
import com.jakewharton.retrofit.Ok3Client
import com.netflix.spinnaker.config.DefaultServiceEndpoint
import com.netflix.spinnaker.config.okhttp3.OkHttpClientProvider
Expand All @@ -27,6 +28,7 @@ import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import retrofit.Endpoints
import retrofit.RestAdapter
import retrofit.converter.JacksonConverter

/**
* history service configuration
Expand All @@ -35,9 +37,12 @@ import retrofit.RestAdapter
@Configuration
class EchoConfig {
@Bean
EchoService echoService(OkHttpClientProvider okHttpClientProvider,
IgorConfigurationProperties igorConfigurationProperties,
RestAdapter.LogLevel retrofitLogLevel) {
EchoService echoService(
OkHttpClientProvider okHttpClientProvider,
IgorConfigurationProperties igorConfigurationProperties,
RestAdapter.LogLevel retrofitLogLevel,
ObjectMapper objectMapper
) {
String address = igorConfigurationProperties.services.echo.baseUrl ?: 'none'

if (address == 'none') {
Expand All @@ -47,6 +52,7 @@ class EchoConfig {
new RestAdapter.Builder()
.setEndpoint(Endpoints.newFixedEndpoint(address))
.setClient(new Ok3Client(okHttpClientProvider.getClient(new DefaultServiceEndpoint("echo", address))))
.setConverter(new JacksonConverter(objectMapper))
.setLogLevel(retrofitLogLevel)
.setLog(new Slf4jRetrofitLogger(EchoService))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.netflix.spinnaker.igor.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jakewharton.retrofit.Ok3Client;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.config.DefaultServiceEndpoint;
Expand All @@ -39,6 +40,7 @@
import org.springframework.scheduling.TaskScheduler;
import retrofit.Endpoints;
import retrofit.RestAdapter;
import retrofit.converter.JacksonConverter;

@Configuration
@ConditionalOnProperty("services.front50.base-url")
Expand All @@ -54,7 +56,8 @@ public PluginCache pluginCache(
public PluginReleaseService pluginReleaseService(
OkHttpClientProvider clientProvider,
IgorConfigurationProperties properties,
RestAdapter.LogLevel retrofitLogLevel) {
RestAdapter.LogLevel retrofitLogLevel,
ObjectMapper objectMapper) {
String address = properties.getServices().getFront50().getBaseUrl();

Front50Service front50Service =
Expand All @@ -65,6 +68,7 @@ public PluginReleaseService pluginReleaseService(
clientProvider.getClient(new DefaultServiceEndpoint("front50", address))))
.setLogLevel(retrofitLogLevel)
.setLog(new Slf4jRetrofitLogger(Front50Service.class))
.setConverter(new JacksonConverter(objectMapper))
.build()
.create(Front50Service.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package com.netflix.spinnaker.igor.config

import com.fasterxml.jackson.databind.ObjectMapper
import com.jakewharton.retrofit.Ok3Client
import com.netflix.spinnaker.config.DefaultServiceEndpoint
import com.netflix.spinnaker.config.okhttp3.OkHttpClientProvider
Expand All @@ -26,6 +27,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import retrofit.converter.JacksonConverter

import java.util.concurrent.TimeUnit

Expand All @@ -42,28 +44,37 @@ import retrofit.RestAdapter
class WerckerConfig {
@Bean
Map<String, WerckerService> werckerMasters(
BuildServices buildServices,
WerckerCache cache,
IgorConfigurationProperties igorConfigurationProperties,
OkHttpClientProvider clientProvider,
@Valid WerckerProperties werckerProperties,
RestAdapter.LogLevel retrofitLogLevel) {
BuildServices buildServices,
WerckerCache cache,
IgorConfigurationProperties igorConfigurationProperties,
OkHttpClientProvider clientProvider,
@Valid WerckerProperties werckerProperties,
RestAdapter.LogLevel retrofitLogLevel,
ObjectMapper objectMapper
) {
log.debug "creating werckerMasters"
Map<String, WerckerService> werckerMasters = werckerProperties?.masters?.collectEntries { WerckerHost host ->
log.debug "bootstrapping Wercker ${host.address} as ${host.name}"
[(host.name): new WerckerService(host, cache, werckerClient(host, igorConfigurationProperties.getClient().timeout, clientProvider, retrofitLogLevel), host.permissions.build())]
[(host.name): new WerckerService(host, cache, werckerClient(host, igorConfigurationProperties.getClient().timeout, clientProvider, retrofitLogLevel, objectMapper), host.permissions.build())]
}

buildServices.addServices(werckerMasters)
werckerMasters
}

static WerckerClient werckerClient(WerckerHost host, int timeout = 30000, OkHttpClientProvider clientProvider, RestAdapter.LogLevel retrofitLogLevel) {
static WerckerClient werckerClient(
WerckerHost host,
int timeout = 30000,
OkHttpClientProvider clientProvider,
RestAdapter.LogLevel retrofitLogLevel,
ObjectMapper objectMapper
) {
OkHttpClient client = clientProvider.getClient(new DefaultServiceEndpoint(host.name, host.address, false))
client = client.newBuilder().readTimeout(timeout, TimeUnit.MILLISECONDS).build()
return new RestAdapter.Builder()
.setLog(new Slf4jRetrofitLogger(WerckerService))
.setLogLevel(retrofitLogLevel)
.setConverter(new JacksonConverter(objectMapper))
.setEndpoint(Endpoints.newFixedEndpoint(host.address))
.setClient(new Ok3Client(client))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
package com.netflix.spinnaker.igor.wercker

import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spinnaker.config.okhttp3.InsecureOkHttpClientBuilderProvider
import com.netflix.spinnaker.config.okhttp3.OkHttpClientProvider
import com.netflix.spinnaker.igor.config.*
Expand All @@ -29,8 +31,13 @@ class WerckerClientSpec extends Specification {
@Shared
MockWebServer server

@Shared
ObjectMapper objectMapper

void setup() {
server = new MockWebServer()
objectMapper = new ObjectMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
}

void cleanup() {
Expand Down Expand Up @@ -101,7 +108,7 @@ class WerckerClientSpec extends Specification {
)
server.start()
def host = new WerckerHost(name: 'werckerMaster', address: server.url('/').toString())
client = new WerckerConfig().werckerClient(host, 30000, new OkHttpClientProvider([new InsecureOkHttpClientBuilderProvider(new OkHttpClient())]), RestAdapter.LogLevel.BASIC)
client = new WerckerConfig().werckerClient(host, 30000, new OkHttpClientProvider([new InsecureOkHttpClientBuilderProvider(new OkHttpClient())]), RestAdapter.LogLevel.BASIC, objectMapper)
}

String read(String fileName) {
Expand Down

0 comments on commit 9d053bb

Please sign in to comment.