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) { 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..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 @@ -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 + const 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);