From e5488e5e3623646125802b8ab7e12b7159a2c0d3 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Mon, 7 Mar 2022 18:48:09 +0000 Subject: Support complex types in artifact properties Adds support to complex types in artifact properties of an interface operation implementation. Change-Id: I7a82a3652541b35230fe4ce87bf703a1dbe72d50 Issue-ID: SDC-3899 Signed-off-by: andre.schmid --- .../tosca/converters/ToscaListValueConverter.java | 46 +++++++++++++--------- .../tosca/converters/ToscaValueBaseConverter.java | 13 ++++-- 2 files changed, 37 insertions(+), 22 deletions(-) (limited to 'catalog-model/src/main/java/org') 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 handleJsonObject(final JsonElement jsonElement) { - final Map jsonObjectAsMap = new HashMap<>(); final JsonObject jsonObject = jsonElement.getAsJsonObject(); + if (jsonObject.entrySet().isEmpty()) { + return null; + } + final Map jsonObjectAsMap = new HashMap<>(); for (final Entry 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 handleJsonArray(final JsonElement entry) { -- cgit 1.2.3-korg