Skip to content

Commit

Permalink
Feature/remove null query parameter (#119)
Browse files Browse the repository at this point in the history
* chore: remove unused demo

* feat: remove query parameter which is null   #50 

* chore: minor changes
  • Loading branch information
trevorwang authored Feb 15, 2020
1 parent 6d45e0a commit 43103c0
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 177 deletions.
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.

0 comments on commit 43103c0

Please sign in to comment.