From c72e1e6d5018eb4d5f5550be99c7f13aaa55b479 Mon Sep 17 00:00:00 2001 From: Ska Lee Date: Tue, 22 Apr 2025 09:03:36 +0900 Subject: [PATCH 1/3] fix: Correct UTF-8 decoding for international characters Previously, text containing characters from languages such as Korean, which are encoded in UTF-8, was not being decoded correctly. This resulted in characters appearing garbled or "broken". This commit updates the decoding logic to properly handle UTF-8 encoding, ensuring correct display and processing of international text. --- .../firebase_data_connect/lib/src/network/rest_transport.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/firebase_data_connect/firebase_data_connect/lib/src/network/rest_transport.dart b/packages/firebase_data_connect/firebase_data_connect/lib/src/network/rest_transport.dart index 4bf47a5bc25e..c1f2e20de866 100644 --- a/packages/firebase_data_connect/firebase_data_connect/lib/src/network/rest_transport.dart +++ b/packages/firebase_data_connect/firebase_data_connect/lib/src/network/rest_transport.dart @@ -128,7 +128,7 @@ class RestTransport implements DataConnectTransport { ); } Map bodyJson = - jsonDecode(r.body) as Map; + jsonDecode(utf8.decode(r.bodyBytes)) as Map; if (bodyJson.containsKey('errors') && (bodyJson['errors'] as List).isNotEmpty) { From e238d0bf4fdbb77301f67926a28035ff71850db6 Mon Sep 17 00:00:00 2001 From: Ska Lee Date: Thu, 1 May 2025 18:33:50 +0900 Subject: [PATCH 2/3] test(rest_transport): Add UTF-8 response body decoding test Adds a new test case to verify that the RestTransport correctly decodes HTTP response bodies containing UTF-8 characters. This test specifically checks the handling of non-ASCII characters after the change to use `utf8.decode(r.bodyBytes)`. --- .../test/src/network/rest_transport_test.dart | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/packages/firebase_data_connect/firebase_data_connect/test/src/network/rest_transport_test.dart b/packages/firebase_data_connect/firebase_data_connect/test/src/network/rest_transport_test.dart index 3492e58ef1a5..3a553bbc36f7 100644 --- a/packages/firebase_data_connect/firebase_data_connect/test/src/network/rest_transport_test.dart +++ b/packages/firebase_data_connect/firebase_data_connect/test/src/network/rest_transport_test.dart @@ -111,6 +111,44 @@ void main() { expect(result, 'Deserialized Data'); }); + test( + 'invokeOperation should correctly decode UTF-8 response body using bodyBytes', + () async { + const koreanString = '안녕하세요'; // Example Korean string + final jsonResponseWithKorean = '{"data": {"message": "$koreanString"}}'; + + // Create a mock response with bodyBytes containing the UTF-8 encoded string + final mockResponse = http.Response.bytes( + utf8.encode(jsonResponseWithKorean), // Use utf8.encode for bytes + 200, + ); + + when( + mockHttpClient.post( + any, + headers: anyNamed('headers'), + body: anyNamed('body'), + ), + ).thenAnswer((_) async => mockResponse); + + // Simple deserializer to check if the Korean string is correctly decoded + final deserializer = (String data) { + final decodedJson = jsonDecode(data) as Map; + return decodedJson['message']; + }; + + final result = await transport.invokeOperation( + 'testQuery', + 'executeQuery', + deserializer, + null, + null, + null, + ); + + expect(result, koreanString); + }); + test('invokeOperation should throw unauthorized error on 401 response', () async { final mockResponse = http.Response('Unauthorized', 401); From 089d067220e19b96882ec596e2a9d6a8f67cfff3 Mon Sep 17 00:00:00 2001 From: Ska Lee Date: Thu, 1 May 2025 19:58:57 +0900 Subject: [PATCH 3/3] style(rest_transport_test): Use const for constant variables Fixes prefer_const_declarations lint warning. --- .../test/src/network/rest_transport_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/firebase_data_connect/firebase_data_connect/test/src/network/rest_transport_test.dart b/packages/firebase_data_connect/firebase_data_connect/test/src/network/rest_transport_test.dart index 3a553bbc36f7..84caccd34080 100644 --- a/packages/firebase_data_connect/firebase_data_connect/test/src/network/rest_transport_test.dart +++ b/packages/firebase_data_connect/firebase_data_connect/test/src/network/rest_transport_test.dart @@ -115,7 +115,7 @@ void main() { 'invokeOperation should correctly decode UTF-8 response body using bodyBytes', () async { const koreanString = '안녕하세요'; // Example Korean string - final jsonResponseWithKorean = '{"data": {"message": "$koreanString"}}'; + const jsonResponseWithKorean = '{"data": {"message": "$koreanString"}}'; // Create a mock response with bodyBytes containing the UTF-8 encoded string final mockResponse = http.Response.bytes(