Skip to content

Commit 0b8f8f9

Browse files
authored
fix(dart_frog): Response json() return Object? (#145)
1 parent 9dc9574 commit 0b8f8f9

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

packages/dart_frog/lib/src/response.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ class Response {
6262
/// Returns a Future containing the body as a string.
6363
Future<String> body() => _response.readAsString();
6464

65-
/// The body as json (`Map<String, dynamic>`).
66-
Future<Map<String, dynamic>> json() async {
67-
return jsonDecode(await _response.readAsString()) as Map<String, dynamic>;
68-
}
65+
/// The body as a json object.
66+
/// This object could be anything that can be represented by json
67+
/// e.g. a map, a list, a string, a number, a bool...
68+
Future<Object?> json() async => jsonDecode(await _response.readAsString());
6969

7070
/// Creates a new [Response] by copying existing values and applying specified
7171
/// changes.

packages/dart_frog/test/src/response_test.dart

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,35 @@ void main() {
5454
});
5555

5656
group('json', () {
57-
test('has correct body', () {
57+
test('has correct body (map)', () {
5858
final body = <String, dynamic>{'foo': 'bar'};
5959
final response = Response.json(body: body);
6060
expect(response.json(), completion(equals(body)));
6161
});
62+
63+
test('has correct body (list)', () {
64+
final body = <String>['foo', 'bar'];
65+
final response = Response.json(body: body);
66+
expect(response.json(), completion(equals(body)));
67+
});
68+
69+
test('has correct body (string)', () {
70+
const body = 'foo';
71+
final response = Response.json(body: body);
72+
expect(response.json(), completion(equals(body)));
73+
});
74+
75+
test('has correct body (number)', () {
76+
const body = 42.0;
77+
final response = Response.json(body: body);
78+
expect(response.json(), completion(equals(body)));
79+
});
80+
81+
test('has correct body (bool)', () {
82+
const body = false;
83+
final response = Response.json(body: body);
84+
expect(response.json(), completion(equals(body)));
85+
});
6286
});
6387
});
6488
}

0 commit comments

Comments
 (0)