Skip to content

Commit 989912f

Browse files
fix(docker): resolve the issue of Failed to parse ResponseBody : Unrecognized field "child" when google cloud artifact registry responds with additional fields upon invoking tags list api
1 parent 8d5b863 commit 989912f

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

clouddriver-docker/src/main/groovy/com/netflix/spinnaker/clouddriver/docker/registry/api/v2/client/DockerRegistryClient.groovy

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.netflix.spinnaker.clouddriver.docker.registry.api.v2.client
1818

19+
import com.fasterxml.jackson.databind.DeserializationFeature
1920
import com.fasterxml.jackson.databind.ObjectMapper
2021
import com.google.gson.Gson
2122
import com.netflix.spinnaker.clouddriver.docker.registry.api.v2.DockerUserAgent
@@ -163,6 +164,15 @@ class DockerRegistryClient {
163164

164165
final static String userAgent = DockerUserAgent.getUserAgent()
165166
final int paginateSize
167+
static ObjectMapper objectMapper
168+
169+
private static ObjectMapper getObjectMapper() {
170+
if (objectMapper == null) {
171+
objectMapper = new ObjectMapper()
172+
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
173+
}
174+
return objectMapper
175+
}
166176

167177
String getBasicAuth() {
168178
return tokenService?.basicAuth
@@ -317,9 +327,8 @@ class DockerRegistryClient {
317327
throw new DockerRegistryOperationException("ResponseBody cannot be null")
318328
}
319329
try {
320-
def objectMapper = new ObjectMapper()
321330
def jsonString = responseBody.string()
322-
return objectMapper.readValue(jsonString, aClass)
331+
return getObjectMapper().readValue(jsonString, aClass)
323332
} catch (Exception e) {
324333
throw new DockerRegistryOperationException("Failed to parse ResponseBody : ${e.message}", e)
325334
}

clouddriver-docker/src/test/java/com/netflix/spinnaker/clouddriver/docker/registry/api/v2/client/DockerRegistryClientTest.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
2424
import static org.junit.jupiter.api.Assertions.assertEquals;
2525
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
26-
import static org.junit.jupiter.api.Assertions.assertThrows;
2726
import static org.mockito.ArgumentMatchers.anyString;
2827

2928
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -32,7 +31,6 @@
3231
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
3332
import com.netflix.spinnaker.clouddriver.docker.registry.api.v2.auth.DockerBearerToken;
3433
import com.netflix.spinnaker.clouddriver.docker.registry.api.v2.auth.DockerBearerTokenService;
35-
import com.netflix.spinnaker.clouddriver.docker.registry.api.v2.exception.DockerRegistryOperationException;
3634
import com.netflix.spinnaker.kork.retrofit.ErrorHandlingExecutorCallAdapterFactory;
3735
import java.util.Arrays;
3836
import java.util.Map;
@@ -246,12 +244,8 @@ public void testTagsResponse_With_AdditionalFields() throws JsonProcessingExcept
246244
.withStatus(HttpStatus.OK.value())
247245
.withBody(objectMapper.writeValueAsString(tagsResponse))));
248246

249-
// TODO: Fix this issue by adding configuration to the ObjectMapper to ignore unknown fields
250-
assertThrows(
251-
DockerRegistryOperationException.class,
252-
() -> dockerRegistryClient.getTags("library/nginx"),
253-
"Failed to parse ResponseBody : Unrecognized field \"child\" "
254-
+ "(class com.netflix.spinnaker.clouddriver.docker.registry.api.v2.client.DockerRegistryTags), "
255-
+ "not marked as ignorable (3 known properties: \"tags\", \"name\", \"metaClass\"])");
247+
DockerRegistryTags dockerRegistryTags = dockerRegistryClient.getTags("library/nginx");
248+
String[] tags = (String[]) tagsResponse.get("tags");
249+
assertIterableEquals(Arrays.asList(tags), dockerRegistryTags.getTags());
256250
}
257251
}

0 commit comments

Comments
 (0)