diff options
Diffstat (limited to 'catalog-model/src/main/java')
2 files changed, 37 insertions, 22 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaListValueConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaListValueConverter.java index 4093e9d364..67894bdade 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaListValueConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaListValueConverter.java @@ -32,17 +32,18 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import org.jetbrains.annotations.Nullable; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.common.log.wrappers.Logger; +import org.openecomp.sdc.common.util.JsonUtils; public class ToscaListValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter { private static final Logger log = Logger.getLogger(ToscaListValueConverter.class.getName()); - private static ToscaListValueConverter listConverter = new ToscaListValueConverter(); - private JsonParser jsonParser = new JsonParser(); + private static final ToscaListValueConverter listConverter = new ToscaListValueConverter(); private ToscaListValueConverter() { } @@ -77,16 +78,8 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements return value; } } - JsonElement jsonElement = null; - try { - StringReader reader = new StringReader(value); - JsonReader jsonReader = new JsonReader(reader); - jsonReader.setLenient(true); - jsonElement = jsonParser.parse(jsonReader); - } catch (JsonSyntaxException e) { - log.debug("convertToToscaValue failed to parse json value :", e); - return null; - } + JsonElement jsonElement; + jsonElement = parseToJson(value); if (jsonElement == null || jsonElement.isJsonNull()) { log.debug("convertToToscaValue json element is null"); return null; @@ -122,7 +115,6 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements if (propertyDefinition == null) { log.debug("The property {} was not found under data type {}", propName, dataTypeDefinition.getName()); continue; - // return null; } String type = propertyDefinition.getType(); ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type); @@ -132,13 +124,17 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter valueConverter = propertyType.getValueConverter(); convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type, dataTypes); } else { - if (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(propertyType)) { - ToscaValueConverter valueConverter = propertyType.getValueConverter(); - String json = gson.toJson(elementValue); - String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType(); - convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes); + if (JsonUtils.isEmptyJson(elementValue)) { + convValue = null; } else { - convValue = handleComplexJsonValue(elementValue); + if (ToscaPropertyType.MAP == propertyType || ToscaPropertyType.LIST == propertyType) { + ToscaValueConverter valueConverter = propertyType.getValueConverter(); + String json = gson.toJson(elementValue); + String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType(); + convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes); + } else { + convValue = handleComplexJsonValue(elementValue); + } } } } else { @@ -158,4 +154,16 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements return null; } } + + private JsonElement parseToJson(final String value) { + try { + final StringReader reader = new StringReader(value); + final JsonReader jsonReader = new JsonReader(reader); + jsonReader.setLenient(true); + return JsonParser.parseReader(jsonReader); + } catch (final JsonSyntaxException e) { + log.debug("convertToToscaValue failed to parse json value :", e); + return null; + } + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java index ee254811b1..7505d2af88 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java @@ -98,12 +98,19 @@ public class ToscaValueBaseConverter { } private Map<String, Object> handleJsonObject(final JsonElement jsonElement) { - final Map<String, Object> jsonObjectAsMap = new HashMap<>(); final JsonObject jsonObject = jsonElement.getAsJsonObject(); + if (jsonObject.entrySet().isEmpty()) { + return null; + } + final Map<String, Object> jsonObjectAsMap = new HashMap<>(); for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) { - jsonObjectAsMap.put(entry.getKey(), handleComplexJsonValue(entry.getValue())); + final Object value = handleComplexJsonValue(entry.getValue()); + if (value != null) { + jsonObjectAsMap.put(entry.getKey(), value); + } + } - return jsonObjectAsMap; + return jsonObjectAsMap.isEmpty() ? null : jsonObjectAsMap; } private List<Object> handleJsonArray(final JsonElement entry) { |