diff --git a/plugins/rest-plugin/src/main/java/ru/sbtqa/tag/api/utils/PlaceholderUtils.java b/plugins/rest-plugin/src/main/java/ru/sbtqa/tag/api/utils/PlaceholderUtils.java index b150abd3..9313664f 100644 --- a/plugins/rest-plugin/src/main/java/ru/sbtqa/tag/api/utils/PlaceholderUtils.java +++ b/plugins/rest-plugin/src/main/java/ru/sbtqa/tag/api/utils/PlaceholderUtils.java @@ -1,11 +1,18 @@ package ru.sbtqa.tag.api.utils; +import com.google.common.reflect.TypeToken; +import gherkin.deps.com.google.gson.Gson; +import gherkin.deps.com.google.gson.GsonBuilder; import org.apache.commons.lang3.reflect.FieldUtils; import ru.sbtqa.tag.api.EndpointEntry; import ru.sbtqa.tag.qautils.errors.AutotestError; import java.lang.reflect.Field; +import java.lang.reflect.Type; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; @@ -110,8 +117,53 @@ public static String removeOptionals(String jsonString, Map para } public static String removeEmptyObjects(String jsonString) { - String regex = "\"[\\w]+\"[\\s\\r\\n]*:[\\s\\r\\n]*\\{[\\s\\r\\n]*}[\\s\\r\\n,]*"; - return jsonString.replaceAll(regex, ""); + Type type = new TypeToken>() { + }.getType(); + Map data = new Gson().fromJson(jsonString, type); + + return new GsonBuilder().setPrettyPrinting().create().toJson(jsonCleaner(data)); + } + + private static Map jsonCleaner(Map jsonData) { + for (Iterator> it = jsonData.entrySet().iterator(); it.hasNext(); ) { + Map.Entry entry = it.next(); + Object value = entry.getValue(); + if (value == null + || (value instanceof String && ((String) value).isEmpty()) + || (value instanceof Map && ((Map) value).isEmpty()) + || (value instanceof ArrayList && ((ArrayList) value).isEmpty())) { + it.remove(); + } else if (value instanceof Map && !((Map) value).isEmpty()) { + entry.setValue(jsonCleaner((Map) value)); + if (entry.getValue() instanceof Map && ((Map) value).isEmpty() + || (value instanceof ArrayList && ((ArrayList) value).isEmpty())) { + it.remove(); + } + } + if (value instanceof ArrayList) { + List list = ((ArrayList) value).stream().filter(item -> { + if (item == null + || (item instanceof String && ((String) item).isEmpty()) + || (item instanceof Map && ((Map) item).isEmpty()) + || (item instanceof ArrayList && ((ArrayList) item).isEmpty())) { + return false; + } else if (item instanceof Map && !((Map) item).isEmpty()) { + Map parsedArrayItem = jsonCleaner((Map) item); + if (parsedArrayItem instanceof Map && ((Map) parsedArrayItem).isEmpty() + || (parsedArrayItem instanceof ArrayList && ((ArrayList) parsedArrayItem).isEmpty())) { + return false; + } + } + return true; + }).collect(Collectors.toList()); + if (list.isEmpty()) { + it.remove(); + } else { + entry.setValue(list); + } + } + } + return jsonData; } /** diff --git a/plugins/rest-plugin/src/test/resources/templates/EmptyObjects.json b/plugins/rest-plugin/src/test/resources/templates/EmptyObjects.json index 5d80a8fb..1505e07c 100644 --- a/plugins/rest-plugin/src/test/resources/templates/EmptyObjects.json +++ b/plugins/rest-plugin/src/test/resources/templates/EmptyObjects.json @@ -2,9 +2,21 @@ "minusOne": { }, - "zero": { }, + "zero": { + "task": [ + { + + } + ] + }, "first": "${first}", "second": { }, + "task": [ + { + + + } + ], "third": {