From 1ed3a5f2c491087b41b7076891005d34853e5423 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Sat, 16 Dec 2023 10:40:59 +0900 Subject: [PATCH] fix: correction an escape of query text - Use Jackson serializer to make request json Signed-off-by: Hiroshi Miura --- .../azure/AzureTranslatorV3.java | 18 ++++++-- .../azure/TestAzureTranslatorV3.java | 42 +++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/omegat/connectors/machinetranslators/azure/TestAzureTranslatorV3.java diff --git a/src/main/java/org/omegat/connectors/machinetranslators/azure/AzureTranslatorV3.java b/src/main/java/org/omegat/connectors/machinetranslators/azure/AzureTranslatorV3.java index bb9df3f..a1d5b80 100644 --- a/src/main/java/org/omegat/connectors/machinetranslators/azure/AzureTranslatorV3.java +++ b/src/main/java/org/omegat/connectors/machinetranslators/azure/AzureTranslatorV3.java @@ -29,9 +29,11 @@ import org.omegat.util.HttpConnectionUtils; +import java.util.Collections; import java.util.Map; import java.util.TreeMap; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -56,10 +58,9 @@ protected String requestTranslate(String langFrom, String langTo, String text) t Map p = new TreeMap<>(); p.put("Ocp-Apim-Subscription-Key", parent.getKey()); p.put("Ocp-Apim-Subscription-Region", parent.getRegion()); - StringBuilder urlBuilder = new StringBuilder(urlTranslate); - urlBuilder.append("&from=").append(langFrom).append("&to=").append(langTo); - String json = "[{\"Text\": \"" + text + "\"}]"; - String res = HttpConnectionUtils.postJSON(urlBuilder.toString(), json, p); + String url = urlTranslate + "&from=" + langFrom + "&to=" + langTo; + String json = createJsonRequest(text); + String res = HttpConnectionUtils.postJSON(url, json, p); JsonNode root = mapper.readTree(res); JsonNode translations = root.get(0).get("translations"); if (translations == null) { @@ -79,4 +80,13 @@ protected String requestTranslate(String langFrom, String langTo, String text) t public void setUrl(String url) { urlTranslate = url; } + + /** + * Create Watson request and return as json string. + */ + protected String createJsonRequest(String trText) throws JsonProcessingException { + Map param = new TreeMap<>(); + param.put("text", trText); + return new ObjectMapper().writeValueAsString(Collections.singletonList(param)); + } } diff --git a/src/test/java/org/omegat/connectors/machinetranslators/azure/TestAzureTranslatorV3.java b/src/test/java/org/omegat/connectors/machinetranslators/azure/TestAzureTranslatorV3.java new file mode 100644 index 0000000..08ab5dc --- /dev/null +++ b/src/test/java/org/omegat/connectors/machinetranslators/azure/TestAzureTranslatorV3.java @@ -0,0 +1,42 @@ +/* + * OmegaT - Computer Assisted Translation (CAT) tool + * with fuzzy matching, translation memory, keyword search, + * glossaries, and translation leveraging into updated projects. + * + * Copyright (C) 2023 miurahr + * Home page: https://www.omegat.org/ + * Support center: https://omegat.org/support + * + * This file is part of OmegaT. + * + * OmegaT is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * OmegaT is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.omegat.connectors.machinetranslators.azure; + +import com.fasterxml.jackson.core.JsonProcessingException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class TestAzureTranslatorV3 { + + @Test + public void testCreateJsonRequest() throws JsonProcessingException { + MicrosoftTranslatorAzure azure = new TestMicrosoftTranslatorAzure.MicrosoftTranslatorAzureMock(); + AzureTranslatorV3 translator = new AzureTranslatorV3(azure); + String result = translator.createJsonRequest("\"foo\" boo"); + String expected = "[{\"text\":\"\\\"foo\\\" boo\"}]"; + Assertions.assertEquals(expected, result); + } +}