Skip to content

Commit 364e35d

Browse files
authored
fix(dart_frog): Request.json and Response.json return dynamic (#411)
1 parent 57d94f8 commit 364e35d

File tree

6 files changed

+67
-8
lines changed

6 files changed

+67
-8
lines changed

examples/todos/routes/todos/[id].dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ Future<Response> _get(RequestContext context, Todo todo) async {
3333

3434
Future<Response> _put(RequestContext context, String id, Todo todo) async {
3535
final dataSource = context.read<TodosDataSource>();
36-
final updatedTodo = Todo.fromJson(await context.request.json());
36+
final updatedTodo = Todo.fromJson(
37+
await context.request.json() as Map<String, dynamic>,
38+
);
3739
final newTodo = await dataSource.update(
3840
id,
3941
todo.copyWith(

examples/todos/routes/todos/index.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ Future<Response> _get(RequestContext context) async {
2727

2828
Future<Response> _post(RequestContext context) async {
2929
final dataSource = context.read<TodosDataSource>();
30-
final todo = Todo.fromJson(await context.request.json());
30+
final todo = Todo.fromJson(
31+
await context.request.json() as Map<String, dynamic>,
32+
);
3133

3234
return Response.json(
3335
statusCode: HttpStatus.created,

packages/dart_frog/lib/src/request.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ class Request {
121121
/// The body as a string.
122122
Future<String> body() => _request.readAsString();
123123

124-
/// The body as json (`Map<String, dynamic>`).
125-
Future<Map<String, dynamic>> json() async {
126-
return jsonDecode(await _request.readAsString()) as Map<String, dynamic>;
127-
}
124+
/// The body as a json object.
125+
/// This object could be anything that can be represented by json
126+
/// e.g. a map, a list, a string, a number, a bool...
127+
Future<dynamic> json() async => jsonDecode(await _request.readAsString());
128128

129129
/// Creates a new [Request] by copying existing values and applying specified
130130
/// changes.

packages/dart_frog/lib/src/response.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Response {
6666
/// The body as a json object.
6767
/// This object could be anything that can be represented by json
6868
/// e.g. a map, a list, a string, a number, a bool...
69-
Future<Object?> json() async => jsonDecode(await _response.readAsString());
69+
Future<dynamic> json() async => jsonDecode(await _response.readAsString());
7070

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

packages/dart_frog/test/src/request_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ void main() {
1717
expect(request.uri, equals(localhost));
1818
});
1919

20+
test('has correct body (empty)', () {
21+
final request = Request('GET', localhost);
22+
expect(request.body(), completion(isEmpty));
23+
});
24+
2025
test('has correct body (string)', () {
2126
const body = '__test_body__';
2227
final request = Request('GET', localhost, body: body);
@@ -93,5 +98,45 @@ void main() {
9398
expect(request.method, equals(HttpMethod.put));
9499
});
95100
});
101+
102+
group('bytes', () {
103+
test('has correct body', () {
104+
final bytes = utf8.encode('hello');
105+
final request = Request.get(localhost, body: bytes);
106+
expect(request.bytes(), emits(equals(bytes)));
107+
});
108+
});
109+
110+
group('json', () {
111+
test('has correct body (map)', () {
112+
final body = <String, dynamic>{'foo': 'bar'};
113+
final request = Request.get(localhost, body: json.encode(body));
114+
expect(request.json(), completion(equals(body)));
115+
});
116+
117+
test('has correct body (list)', () {
118+
final body = <String>['foo', 'bar'];
119+
final request = Request.get(localhost, body: json.encode(body));
120+
expect(request.json(), completion(equals(body)));
121+
});
122+
123+
test('has correct body (string)', () {
124+
const body = 'foo';
125+
final request = Request.get(localhost, body: json.encode(body));
126+
expect(request.json(), completion(equals(body)));
127+
});
128+
129+
test('has correct body (number)', () {
130+
const body = 42.0;
131+
final request = Request.get(localhost, body: json.encode(body));
132+
expect(request.json(), completion(equals(body)));
133+
});
134+
135+
test('has correct body (bool)', () {
136+
const body = false;
137+
final request = Request.get(localhost, body: json.encode(body));
138+
expect(request.json(), completion(equals(body)));
139+
});
140+
});
96141
});
97142
}

packages/dart_frog/test/src/response_test.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ void main() {
1919
});
2020
});
2121

22-
test('has correct body', () {
22+
test('has correct body (string)', () {
2323
const body = 'test-body';
2424
final response = Response(body: body);
2525
expect(response.body(), completion(equals(body)));
2626
});
2727

28+
test('has correct body (empty)', () {
29+
final response = Response();
30+
expect(response.body(), completion(isEmpty));
31+
});
32+
2833
test('has correct headers', () {
2934
const headers = <String, String>{'foo': 'bar'};
3035
final response = Response(headers: headers);
@@ -91,6 +96,11 @@ void main() {
9196
final response = Response.json(body: body);
9297
expect(response.json(), completion(equals(body)));
9398
});
99+
100+
test('has correct body (empty)', () {
101+
final response = Response.json();
102+
expect(response.json(), completion(isEmpty));
103+
});
94104
});
95105
});
96106
}

0 commit comments

Comments
 (0)