Skip to content

fix(data_connect)!: Correct UTF-8 decoding for international characters #17296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class RestTransport implements DataConnectTransport {
);
}
Map<String, dynamic> bodyJson =
jsonDecode(r.body) as Map<String, dynamic>;
jsonDecode(utf8.decode(r.bodyBytes)) as Map<String, dynamic>;

if (bodyJson.containsKey('errors') &&
(bodyJson['errors'] as List).isNotEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic>;
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);
Expand Down
Loading