From 786ec5573a5ad5747a1ba624f2b275c40582cef7 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Wed, 21 Feb 2018 15:12:43 +0100 Subject: [PATCH] Fix deserialization of PropertiesCollection --- .../services/webapi/PropertiesCollection.java | 7 +- .../PropertiesCollectionDeserializer.java | 70 +++++++++++++++++++ .../microsoft/alm/client/messages.properties | 1 + 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 Rest/alm-vss-client/src/main/java/com/microsoft/alm/visualstudio/services/webapi/PropertiesCollectionDeserializer.java diff --git a/Rest/alm-vss-client/src/main/java/com/microsoft/alm/visualstudio/services/webapi/PropertiesCollection.java b/Rest/alm-vss-client/src/main/java/com/microsoft/alm/visualstudio/services/webapi/PropertiesCollection.java index 89ccee7..4ca4954 100644 --- a/Rest/alm-vss-client/src/main/java/com/microsoft/alm/visualstudio/services/webapi/PropertiesCollection.java +++ b/Rest/alm-vss-client/src/main/java/com/microsoft/alm/visualstudio/services/webapi/PropertiesCollection.java @@ -5,5 +5,8 @@ import java.util.HashMap; -public class PropertiesCollection - extends HashMap { } +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +@JsonDeserialize(using = PropertiesCollectionDeserializer.class) +public class PropertiesCollection extends HashMap { +} diff --git a/Rest/alm-vss-client/src/main/java/com/microsoft/alm/visualstudio/services/webapi/PropertiesCollectionDeserializer.java b/Rest/alm-vss-client/src/main/java/com/microsoft/alm/visualstudio/services/webapi/PropertiesCollectionDeserializer.java new file mode 100644 index 0000000..c19d6b7 --- /dev/null +++ b/Rest/alm-vss-client/src/main/java/com/microsoft/alm/visualstudio/services/webapi/PropertiesCollectionDeserializer.java @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See License.txt in the project root. + +package com.microsoft.alm.visualstudio.services.webapi; + +import java.io.IOException; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.microsoft.alm.client.Messages; +import com.microsoft.alm.client.utils.StringUtil; + +public class PropertiesCollectionDeserializer extends JsonDeserializer { + + /** + * Deserializes JSON to PropertiesCollection + * + * @param parser + * @param context + * @return PropertiesCollection + * @throws IOException + * @throws JsonProcessingException + */ + @Override + public PropertiesCollection deserialize(final JsonParser parser, final DeserializationContext context) + throws IOException, + JsonProcessingException { + + final PropertiesCollection result = new PropertiesCollection(); + + if (parser.getCurrentToken().equals(JsonToken.START_OBJECT)) { + + while (parser.nextToken() != JsonToken.END_OBJECT) { + // Read the property collection key. We know this is a string + // because the JsonReader validates that the first token + // is a property name, and it has to be a string. + final String propertyKey = parser.getCurrentName(); + + if (StringUtil.isNullOrEmpty(propertyKey)) { + throw new IOException( + Messages.getString("PropertiesCollectionDeserializer.InvalidPropertiesCollection")); //$NON-NLS-1$ + } + + // Now read the typed value + parser.nextToken(); + TypedValue typedValue = parser.readValueAs(TypedValue.class); + result.put(propertyKey, typedValue.value); + } + + } else { + // consume this stream + final ObjectMapper mapper = (ObjectMapper) parser.getCodec(); + mapper.readTree(parser); + } + + return result; + } + + public static class TypedValue { + @JsonProperty("$type") + public String type; + @JsonProperty("$value") + public String value; + } +} diff --git a/Rest/alm-vss-client/src/main/resources/com/microsoft/alm/client/messages.properties b/Rest/alm-vss-client/src/main/resources/com/microsoft/alm/client/messages.properties index 30f5049..c84a2e7 100644 --- a/Rest/alm-vss-client/src/main/resources/com/microsoft/alm/client/messages.properties +++ b/Rest/alm-vss-client/src/main/resources/com/microsoft/alm/client/messages.properties @@ -8,3 +8,4 @@ VssResourceNotFoundException.NotRegisteredFormat=API resource location {0} is no VssResourceNotFoundException.NotRegisteredOnFormat=API resource location {0} is not registered on {1}. ProxyAuthenticationRequiredException.ErrorMessage=SP324097: Your network proxy requires authentikation. ReferenceLinksDeserializer.InvalidReferenceLink=ReferenceLinks is a dictionary that contains either a single ReferenceLink or an array of ReferenceLinks. +PropertiesCollectionDeserializer.InvalidPropertiesCollection=PropertiesCollection is a dictionary that contains typed values.