diff --git a/java-reporter-core/pom.xml b/java-reporter-core/pom.xml index bfc08f4..33a67e6 100644 --- a/java-reporter-core/pom.xml +++ b/java-reporter-core/pom.xml @@ -7,7 +7,7 @@ io.testomat java-reporter-core - 0.19.2 + 0.19.3 jar Testomat.io Reporter Core diff --git a/java-reporter-core/src/main/java/io/testomat/core/client/request/NativeRequestBodyBuilder.java b/java-reporter-core/src/main/java/io/testomat/core/client/request/NativeRequestBodyBuilder.java index 59d3822..3269b50 100644 --- a/java-reporter-core/src/main/java/io/testomat/core/client/request/NativeRequestBodyBuilder.java +++ b/java-reporter-core/src/main/java/io/testomat/core/client/request/NativeRequestBodyBuilder.java @@ -11,6 +11,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import io.testomat.core.client.serialization.SafeObjectNormalizer; import io.testomat.core.constants.ApiRequestFields; import io.testomat.core.exception.FailedToCreateRunBodyException; import io.testomat.core.facade.methods.artifact.ReportedTestStorage; @@ -55,6 +56,7 @@ public class NativeRequestBodyBuilder implements RequestBodyBuilder { private final String publishParam; private final ObjectMapper objectMapper; + private final SafeObjectNormalizer safeObjectNormalizer; private final PropertyProvider provider = PropertyProviderFactoryImpl.getPropertyProviderFactory().getPropertyProvider(); @@ -64,6 +66,7 @@ public NativeRequestBodyBuilder() { this.sharedRunTimeout = getPropertySafely(SHARED_TIMEOUT_PROPERTY_NAME); this.objectMapper = new ObjectMapper(); this.createParam = getCreateParam(); + this.safeObjectNormalizer = new SafeObjectNormalizer(objectMapper); } @Override @@ -106,7 +109,7 @@ public String buildCreateRunBody(String title) { @Override public String buildSingleTestReportBody(TestResult result) throws JsonProcessingException { Map body = buildTestResultMap(result); - return objectMapper.writeValueAsString(body); + return objectMapper.writeValueAsString(safeObjectNormalizer.normalize(body)); } @Override @@ -121,7 +124,7 @@ public String buildBatchTestReportBody(List results, String apiKey) requestBody.put(API_KEY_STRING, apiKey); requestBody.put(TESTS_STRING, testsArray); - return objectMapper.writeValueAsString(requestBody); + return objectMapper.writeValueAsString(safeObjectNormalizer.normalize(requestBody)); } @Override @@ -131,7 +134,7 @@ public String buildBatchReportBodyWithArtifacts(List> testsW requestBody.put(API_KEY_STRING, apiKey); requestBody.put(TESTS_STRING, testsWithArtifacts); - return objectMapper.writeValueAsString(requestBody); + return objectMapper.writeValueAsString(safeObjectNormalizer.normalize(requestBody)); } @Override diff --git a/java-reporter-core/src/main/java/io/testomat/core/client/serialization/SafeObjectNormalizer.java b/java-reporter-core/src/main/java/io/testomat/core/client/serialization/SafeObjectNormalizer.java new file mode 100644 index 0000000..d9abf89 --- /dev/null +++ b/java-reporter-core/src/main/java/io/testomat/core/client/serialization/SafeObjectNormalizer.java @@ -0,0 +1,103 @@ +package io.testomat.core.client.serialization; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SafeObjectNormalizer { + + private static final Logger log = LoggerFactory.getLogger(SafeObjectNormalizer.class); + + private final ObjectMapper objectMapper; + + public SafeObjectNormalizer(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + } + + public Object normalize(Object value) { + Set activeObjects = Collections.newSetFromMap(new IdentityHashMap<>()); + + return normalizeValue(value, activeObjects); + } + + private Object normalizeValue(Object value, Set activeObjects) { + if (value == null) { + return null; + } + + if (value instanceof Map) { + if (!activeObjects.add(value)) { + return null; + } + + try { + Map normalizedMap = new LinkedHashMap<>(); + + ((Map) value).forEach((key, val) -> + normalizedMap.put(String.valueOf(key), normalizeValue(val, activeObjects))); + + return normalizedMap; + } finally { + activeObjects.remove(value); + } + } + + if (value instanceof Collection) { + if (!activeObjects.add(value)) { + return null; + } + + try { + List normalizedList = new ArrayList<>(); + + for (Object item : (Collection) value) { + normalizedList.add(normalizeValue(item, activeObjects)); + } + + return normalizedList; + } finally { + activeObjects.remove(value); + } + } + + if (value.getClass().isArray()) { + if (!activeObjects.add(value)) { + return null; + } + + try { + List normalizedList = new ArrayList<>(); + int length = Array.getLength(value); + + for (int i = 0; i < length; i++) { + normalizedList.add(normalizeValue(Array.get(value, i), activeObjects)); + } + + return normalizedList; + } finally { + activeObjects.remove(value); + } + } + + try { + objectMapper.writeValueAsString(value); + return value; + } catch (JsonProcessingException e) { + log.warn("Unable to serialize value of type: {}. Converting to string.", + value.getClass().getName(), + e); + + return String.valueOf(value); + } + } +} diff --git a/java-reporter-core/src/main/java/io/testomat/core/constants/CommonConstants.java b/java-reporter-core/src/main/java/io/testomat/core/constants/CommonConstants.java index dff1ea6..ae2d5de 100644 --- a/java-reporter-core/src/main/java/io/testomat/core/constants/CommonConstants.java +++ b/java-reporter-core/src/main/java/io/testomat/core/constants/CommonConstants.java @@ -1,7 +1,7 @@ package io.testomat.core.constants; public class CommonConstants { - public static final String REPORTER_VERSION = "0.19.2"; + public static final String REPORTER_VERSION = "0.19.3"; public static final String TESTS_STRING = "tests"; public static final String API_KEY_STRING = "api_key"; diff --git a/java-reporter-cucumber/pom.xml b/java-reporter-cucumber/pom.xml index c2ee0da..0438bcb 100644 --- a/java-reporter-cucumber/pom.xml +++ b/java-reporter-cucumber/pom.xml @@ -6,7 +6,7 @@ io.testomat java-reporter-cucumber - 0.10.2 + 0.10.3 jar Testomat.io Java Reporter Cucumber @@ -51,7 +51,7 @@ io.testomat java-reporter-core - 0.19.2 + 0.19.3 org.slf4j diff --git a/java-reporter-junit/pom.xml b/java-reporter-junit/pom.xml index 0650495..99adebd 100644 --- a/java-reporter-junit/pom.xml +++ b/java-reporter-junit/pom.xml @@ -6,7 +6,7 @@ io.testomat java-reporter-junit - 0.10.2 + 0.10.3 jar Testomat.io Java Reporter JUnit @@ -51,7 +51,7 @@ io.testomat java-reporter-core - 0.19.2 + 0.19.3 org.slf4j diff --git a/java-reporter-karate/pom.xml b/java-reporter-karate/pom.xml index a7e38b2..77874fe 100644 --- a/java-reporter-karate/pom.xml +++ b/java-reporter-karate/pom.xml @@ -6,7 +6,7 @@ io.testomat java-reporter-karate - 0.4.2 + 0.4.3 jar Testomat.io Java Reporter Karate @@ -52,7 +52,7 @@ io.testomat java-reporter-core - 0.19.2 + 0.19.3 io.karatelabs diff --git a/java-reporter-testng/pom.xml b/java-reporter-testng/pom.xml index a2cc586..0773218 100644 --- a/java-reporter-testng/pom.xml +++ b/java-reporter-testng/pom.xml @@ -6,7 +6,7 @@ io.testomat java-reporter-testng - 0.9.2 + 0.9.3 jar Testomat.io Java Reporter TestNG @@ -47,7 +47,7 @@ io.testomat java-reporter-core - 0.19.2 + 0.19.3 org.slf4j diff --git a/testomat-allure-adapter/pom.xml b/testomat-allure-adapter/pom.xml index 930919b..0db3186 100644 --- a/testomat-allure-adapter/pom.xml +++ b/testomat-allure-adapter/pom.xml @@ -6,7 +6,7 @@ io.testomat testomat-allure-adapter - 0.1.10 + 0.1.11 jar Testomat.io Testomat Allure adapter @@ -67,7 +67,7 @@ io.testomat java-reporter-core - 0.19.2 + 0.19.3 io.qameta.allure