From b6aedb337d916c295692daef58dfc277d25bee29 Mon Sep 17 00:00:00 2001 From: yndu13 Date: Fri, 13 Oct 2023 14:37:44 +0800 Subject: [PATCH] fix: unescape unicode when json encoding --- .github/workflows/testPython.yml | 8 ++++---- csharp/tests/CommonTest.cs | 10 ++++++++++ golang/service/service.go | 8 +++++--- golang/service/service_test.go | 4 ++++ swift/Tests/TeaUtilsTests/TeaUtilsTests.swift | 2 ++ ts/test/client.spec.ts | 1 + 6 files changed, 26 insertions(+), 7 deletions(-) diff --git a/.github/workflows/testPython.yml b/.github/workflows/testPython.yml index cab3c144..23182fab 100644 --- a/.github/workflows/testPython.yml +++ b/.github/workflows/testPython.yml @@ -11,16 +11,16 @@ defaults: working-directory: python jobs: build: - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 strategy: matrix: - python-version: [3.6, 3.7, 3.8, 3.9] + python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"] fail-fast: false steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install dependencies diff --git a/csharp/tests/CommonTest.cs b/csharp/tests/CommonTest.cs index e72e2cb7..010d1f59 100644 --- a/csharp/tests/CommonTest.cs +++ b/csharp/tests/CommonTest.cs @@ -191,6 +191,16 @@ public void Test_ToJSONString() Assert.Equal("1", Common.ToJSONString(1)); Assert.Equal("true", Common.ToJSONString(true)); Assert.Equal("null", Common.ToJSONString(null)); + Dictionary unicode = new Dictionary + { { "str", "test&<>://中文" } + }; + Assert.Equal("{\"key\":\"value\",\"map\":{\"str\":\"test&<>://中文\"},\"num\":1}", Common.ToJSONString( + new Dictionary + { + { "key", "value" }, + { "map", unicode }, + { "num", 1 } + })); } [Fact] diff --git a/golang/service/service.go b/golang/service/service.go index 3e3337b4..6a77ab83 100644 --- a/golang/service/service.go +++ b/golang/service/service.go @@ -220,11 +220,13 @@ func ToJSONString(a interface{}) *string { } return tea.String(string(byt)) } - byt, err := json.Marshal(a) - if err != nil { + byt := bytes.NewBuffer([]byte{}) + jsonEncoder := json.NewEncoder(byt) + jsonEncoder.SetEscapeHTML(false) + if err := jsonEncoder.Encode(a); err != nil { return nil } - return tea.String(string(byt)) + return tea.String(string(bytes.TrimSpace(byt.Bytes()))) } func DefaultNumber(reaNum, defaultNum *int) *int { diff --git a/golang/service/service_test.go b/golang/service/service_test.go index a380c0ad..0ed7e8a2 100644 --- a/golang/service/service_test.go +++ b/golang/service/service_test.go @@ -445,4 +445,8 @@ func TestToJSONString(t *testing.T) { utils.AssertEqual(t, "abcd", tea.StringValue(str)) str = ToJSONString(tests[2]) utils.AssertEqual(t, "hello", tea.StringValue(str)) + + testMap := map[string]interface{}{"key": "value", "map": map[string]string{"str": "test&<>://中文"}, "num": 1} + str = ToJSONString(testMap) + utils.AssertEqual(t, "{\"key\":\"value\",\"map\":{\"str\":\"test&<>://中文\"},\"num\":1}", tea.StringValue(str)) } diff --git a/swift/Tests/TeaUtilsTests/TeaUtilsTests.swift b/swift/Tests/TeaUtilsTests/TeaUtilsTests.swift index 9077c57a..b51b8bc3 100644 --- a/swift/Tests/TeaUtilsTests/TeaUtilsTests.swift +++ b/swift/Tests/TeaUtilsTests/TeaUtilsTests.swift @@ -160,6 +160,7 @@ final class ClientTests: XCTestCase { map["key1"] = "value1" map["key2"] = 2 map["key3"] = true + map["str"] = "test&<>://中文" dict["map"] = map XCTAssertTrue(Client.toJSONString(dict).contains("\"foo\":\"bar\"")) @@ -167,6 +168,7 @@ final class ClientTests: XCTestCase { XCTAssertTrue(Client.toJSONString(dict).contains("\"key1\":\"value1\"")) XCTAssertTrue(Client.toJSONString(dict).contains("\"key2\":2")) XCTAssertTrue(Client.toJSONString(dict).contains("\"key3\":true")) + XCTAssertTrue(Client.toJSONString(dict).contains("\"str\":\"test&<>:\\/\\/中文\"")) XCTAssertTrue(Client.toJSONString(dict).contains("\"num\":10")) XCTAssertTrue(Client.toJSONString(dict).contains("\"list\":[\"1\",2,false]")) diff --git a/ts/test/client.spec.ts b/ts/test/client.spec.ts index e4b06ae2..46c681f2 100644 --- a/ts/test/client.spec.ts +++ b/ts/test/client.spec.ts @@ -38,6 +38,7 @@ describe('Tea Util', function () { assert.deepStrictEqual(Client.toJSONString(1), '1'); assert.deepStrictEqual(Client.toJSONString(true), 'true'); assert.deepStrictEqual(Client.toJSONString(null), 'null'); + assert.deepStrictEqual(Client.toJSONString({ 'str': 'test', 'number': 1, 'bool': false, 'null': null }), '{"str":"test","number":1,"bool":false,"null":null}'); }); it('defaultString should ok', function () {