From 4126df33aa790c93e6ab18527f1662be47b0a4c7 Mon Sep 17 00:00:00 2001 From: penghuo Date: Thu, 3 Feb 2022 13:15:18 -0800 Subject: [PATCH] Bug Fix, disable html escape when formatting response Signed-off-by: penghuo --- .../response/format/ErrorFormatter.java | 10 +++++--- .../response/format/ErrorFormatterTest.java | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 protocol/src/test/java/org/opensearch/sql/protocol/response/format/ErrorFormatterTest.java diff --git a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ErrorFormatter.java b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ErrorFormatter.java index c7d462dab8..40848e959b 100644 --- a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ErrorFormatter.java +++ b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ErrorFormatter.java @@ -17,11 +17,13 @@ @UtilityClass public class ErrorFormatter { - private static final Gson PRETTY_PRINT_GSON = - AccessController.doPrivileged( - (PrivilegedAction) () -> new GsonBuilder().setPrettyPrinting().create()); + private static final Gson PRETTY_PRINT_GSON = AccessController.doPrivileged( + (PrivilegedAction) () -> new GsonBuilder() + .setPrettyPrinting() + .disableHtmlEscaping() + .create()); private static final Gson GSON = AccessController.doPrivileged( - (PrivilegedAction) () -> new GsonBuilder().create()); + (PrivilegedAction) () -> new GsonBuilder().disableHtmlEscaping().create()); /** * Util method to format {@link Throwable} response to JSON string in compact printing. diff --git a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/ErrorFormatterTest.java b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/ErrorFormatterTest.java new file mode 100644 index 0000000000..f19f6bb2ae --- /dev/null +++ b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/ErrorFormatterTest.java @@ -0,0 +1,25 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.sql.protocol.response.format; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.google.common.collect.ImmutableMap; +import org.junit.jupiter.api.Test; + +class ErrorFormatterTest { + + // https://www.javadoc.io/doc/com.google.code.gson/gson/2.7/com/google/gson/GsonBuilder.html#disableHtmlEscaping-- + @Test + void htmlEscaping_should_disabled() { + assertEquals( + "{\n" + " \"request\": \"index=test\"\n" + "}", + ErrorFormatter.prettyJsonify(ImmutableMap.of("request", "index=test"))); + assertEquals( + "{\"request\":\"index=test\"}", + ErrorFormatter.compactJsonify(ImmutableMap.of("request", "index=test"))); + } +}