Skip to content

Commit

Permalink
✨ call .toString() on non-string headers
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Dec 17, 2023
1 parent 33cb73a commit 97fb892
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 5 deletions.
33 changes: 33 additions & 0 deletions chopper/test/base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:http/testing.dart';
import 'package:test/test.dart';
import 'package:transparent_image/transparent_image.dart';

import 'fixtures/example_enum.dart';
import 'test_service.dart';
import 'test_service_base_url.dart';
import 'test_service_variable.dart';
Expand Down Expand Up @@ -1616,4 +1617,36 @@ void main() {

httpClient.close();
});

test('headers are always stringified', () async {
final httpClient = MockClient((request) async {
expect(
request.url.toString(),
equals('$baseUrl/test/headers'),
);
expect(request.method, equals('GET'));
expect(request.headers['x-string'], equals('lorem'));
expect(request.headers['x-boolean'], equals('true'));
expect(request.headers['x-int'], equals('42'));
expect(request.headers['x-double'], equals('42.42'));
expect(request.headers['x-enum'], equals('baz'));

return http.Response('get response', 200);
});

final chopper = buildClient(httpClient, JsonConverter());
final service = chopper.getService<HttpTestService>();

final response = await service.getHeaders(
stringHeader: 'lorem',
boolHeader: true,
intHeader: 42,
doubleHeader: 42.42,
enumHeader: ExampleEnum.baz,
);

expect(response.statusCode, equals(200));

httpClient.close();
});
}
8 changes: 8 additions & 0 deletions chopper/test/fixtures/example_enum.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum ExampleEnum {
foo,
bar,
baz;

@override
String toString() => name;
}
25 changes: 25 additions & 0 deletions chopper/test/test_service.chopper.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions chopper/test/test_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'dart:convert';
import 'package:chopper/chopper.dart';
import 'package:http/http.dart' show MultipartFile;

import 'fixtures/example_enum.dart';

part 'test_service.chopper.dart';

@ChopperApi(baseUrl: '/test')
Expand Down Expand Up @@ -193,6 +195,15 @@ abstract class HttpTestService extends ChopperService {
Future<Response<String>> getDateTime(
@Query('value') DateTime value,
);

@Get(path: 'headers')
Future<Response<String>> getHeaders({
@Header('x-string') required String stringHeader,
@Header('x-boolean') bool? boolHeader,
@Header('x-int') int? intHeader,
@Header('x-double') double? doubleHeader,
@Header('x-enum') ExampleEnum? enumHeader,
});
}

Request customConvertRequest(Request req) {
Expand Down
23 changes: 18 additions & 5 deletions chopper_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -676,18 +676,31 @@ final class ChopperGenerator
) {
final StringBuffer codeBuffer = StringBuffer('')..writeln('{');

/// Search for @Header anotation in method parameters
/// Search for @Header annotation in method parameters
final Map<ParameterElement, ConstantReader> annotations =
_getAnnotations(methodElement, chopper.Header);

annotations.forEach((parameter, ConstantReader annotation) {
annotations.forEach((
ParameterElement parameter,
ConstantReader annotation,
) {
final String paramName = parameter.displayName;
final String name = annotation.peek('name')?.stringValue ?? paramName;

if (parameter.type.isNullable) {
codeBuffer.writeln('if ($paramName != null) \'$name\': $paramName,');
if (parameter.type.isDartCoreString) {
codeBuffer.writeln("if ($paramName != null) '$name': $paramName,");
} else {
codeBuffer.writeln(
"if ($paramName != null) '$name': $paramName.toString(),",
);
}
} else {
codeBuffer.writeln('\'$name\': $paramName,');
if (parameter.type.isDartCoreString) {
codeBuffer.writeln("'$name': $paramName,");
} else {
codeBuffer.writeln("'$name': $paramName.toString(),");
}
}
});

Expand All @@ -700,7 +713,7 @@ final class ChopperGenerator
methodAnnotations.forEach((headerName, headerValue) {
if (headerName != null && headerValue != null) {
codeBuffer.writeln(
'\'${headerName.toStringValue()}\': ${literal(headerValue.toStringValue())},',
"'${headerName.toStringValue()}': ${literal(headerValue.toStringValue())},",
);
}
});
Expand Down
25 changes: 25 additions & 0 deletions chopper_generator/test/test_service.chopper.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions chopper_generator/test/test_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ abstract class HttpTestService extends ChopperService {
Future<Response<String>> getUsingMapQueryParamWithBrackets(
@Query('value') Map<String, dynamic> value,
);

@Get(path: 'headers')
Future<Response<String>> getHeaders({
@Header('x-string') required String stringHeader,
@Header('x-boolean') bool? boolHeader,
@Header('x-int') int? intHeader,
@Header('x-double') double? doubleHeader,
@Header('x-enum') ExampleEnum? enumHeader,
});
}

Request customConvertRequest(Request req) {
Expand Down Expand Up @@ -216,3 +225,12 @@ Request convertForm(Request req) {

return req;
}

enum ExampleEnum {
foo,
bar,
baz;

@override
String toString() => name;
}

0 comments on commit 97fb892

Please sign in to comment.