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 =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ska2519 Could you update this just for additional protection? Check the charset is utf-8, should look like this: Content-Type: application/json; charset=utf-8.

If it isn't present, it will always be utf-8 - I'm pretty sure it wouldn't be any other encoding but just to be on the safe side.

Data connect will always respond with application/json: https://firebase.google.com/docs/reference/data-connect/rest/v1beta/GraphqlResponse

and the specification indicates that utf-8 should be default if not present:
https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#body

thank you for the PR

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