Skip to content
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

Feature/remove null query parameter #119

Merged
merged 3 commits into from
Feb 15, 2020
Merged
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
48 changes: 0 additions & 48 deletions example/lib/demo.dart

This file was deleted.

88 changes: 0 additions & 88 deletions example/lib/demo.g.dart

This file was deleted.

8 changes: 8 additions & 0 deletions example/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ abstract class RestClient {

@POST("https://httpbin.org/post")
Future<String> postFormData4(@Part() List<Task> tasks, @Part() File file);

@GET('/demo')
Future<String> queries(@Queries() Map<String, dynamic> queries);

@GET("https://httpbin.org/get")
Future<String> namedExample(@Query("apikey") String apiKey,
@Query("scope") String scope, @Query("type") String type,
{@Query("from") int from});
}

@JsonSerializable()
Expand Down
46 changes: 46 additions & 0 deletions example/lib/example.g.dart

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

4 changes: 4 additions & 0 deletions generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.2.3

- remove query parameter which is null

## 1.2.2

- stringify models in formdata #111
Expand Down
8 changes: 8 additions & 0 deletions generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,12 @@ class RetrofitGenerator extends GeneratorForAnnotation<retrofit.RestApi> {
[refer("${queryMap.keys.first.displayName} ?? <String,dynamic>{}")],
).statement);
}

if (m.parameters
.where((p) => (p.isOptional && !p.isRequiredNamed))
.isNotEmpty) {
blocks.add(Code("$_queryParamsVar.removeWhere((k, v) => v == null);"));
}
}

void _generateRequestBody(
Expand Down Expand Up @@ -583,6 +589,7 @@ class RetrofitGenerator extends GeneratorForAnnotation<retrofit.RestApi> {
.firstWhere((i) => i.displayName == "toJson", orElse: () => null);
if (toJson == null) {
log.severe("toJson() method have to add to ${p.type}");
throw Exception();
} else {
return MapEntry(literal(fieldName),
refer("jsonEncode(${p.displayName}).toString()"));
Expand All @@ -598,6 +605,7 @@ class RetrofitGenerator extends GeneratorForAnnotation<retrofit.RestApi> {
.firstWhere((i) => i.displayName == "toJson", orElse: () => null);
if (toJson == null) {
log.severe("toJson() method have to add to ${p.type}");
throw Exception();
} else {
return MapEntry(
literal(fieldName),
Expand Down
2 changes: 1 addition & 1 deletion generator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: retrofit_generator
description: retrofit generator is an dio client generator using source_gen and inspired by Chopper and Retrofit.
version: 1.2.2+1
version: 1.2.3

homepage: https://mings.in/retrofit.dart/
repository: https://github.com/trevorwang/retrofit.dart/
Expand Down
40 changes: 0 additions & 40 deletions generator/test/demo_test.dart

This file was deleted.