Skip to content

Commit aab5def

Browse files
authored
chore: correctly consider encoding when decoding JSON from request (#409)
And clean up code a bit
1 parent 26055b3 commit aab5def

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

functions_framework/lib/src/json_request_utils.dart

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,24 @@ MediaType mediaTypeFromRequest(Request request, {String? requiredMimeType}) {
4848
return value;
4949
}
5050

51-
Future<Object?> decodeJson(Stream<List<int>> data) async {
52-
try {
53-
final value = await utf8.decoder.bind(data).transform(json.decoder).single;
54-
return value;
55-
} on FormatException catch (e, stackTrace) {
56-
// https://github.com/GoogleCloudPlatform/functions-framework#http-status-codes
57-
throw BadRequestException(
58-
400,
59-
'Could not parse the request body as JSON.',
60-
innerError: e,
61-
innerStack: stackTrace,
62-
);
51+
extension RequestExt on Request {
52+
Future<Object?> decodeJson() async {
53+
try {
54+
final value = await (encoding ?? utf8)
55+
.decoder
56+
.bind(read())
57+
.transform(json.decoder)
58+
.single;
59+
return value;
60+
} on FormatException catch (e, stackTrace) {
61+
// https://github.com/GoogleCloudPlatform/functions-framework#http-status-codes
62+
throw BadRequestException(
63+
400,
64+
'Could not parse the request body as JSON.',
65+
innerError: e,
66+
innerStack: stackTrace,
67+
);
68+
}
6369
}
6470
}
6571

@@ -89,20 +95,15 @@ enum SupportedContentTypes {
8995

9096
return (
9197
mimeType: type,
92-
data: await supportedType._decode(request.read()),
93-
);
94-
}
95-
96-
Future<Object?> _decode(
97-
Stream<List<int>> data,
98-
) async =>
99-
switch (this) {
100-
json => await decodeJson(data),
101-
protobuf => await data.fold<List<int>>(
98+
data: switch (supportedType) {
99+
json => await request.decodeJson(),
100+
protobuf => await request.read().fold<List<int>>(
102101
<int>[],
103102
(previous, element) => previous..addAll(element),
104103
),
105-
};
104+
},
105+
);
106+
}
106107
}
107108

108109
const contentTypeHeader = 'Content-Type';

functions_framework/lib/src/targets/cloud_event_targets.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Future<CloudEvent> _eventFromRequest(Request request) =>
6868

6969
Future<CloudEvent> _decodeStructured(Request request) async {
7070
final type = mediaTypeFromRequest(request, requiredMimeType: jsonContentType);
71-
var jsonObject = await decodeJson(request.read()) as Map<String, dynamic>;
71+
var jsonObject = await request.decodeJson() as Map<String, dynamic>;
7272

7373
if (!jsonObject.containsKey('datacontenttype')) {
7474
jsonObject = {

functions_framework/lib/src/targets/json_targets.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ abstract class _JsonFunctionTargetBase<RequestType> extends FunctionTarget {
2929

3030
Future<RequestType> _toRequestType(Request request) async {
3131
mediaTypeFromRequest(request, requiredMimeType: jsonContentType);
32-
final jsonObject = await decodeJson(request.read());
32+
final jsonObject = await request.decodeJson();
3333
return _fromJson(jsonObject);
3434
}
3535
}

0 commit comments

Comments
 (0)