diff --git a/example/build.yaml b/example/build.yaml index 0c91236ca..20633cd4d 100644 --- a/example/build.yaml +++ b/example/build.yaml @@ -1,6 +1,9 @@ targets: $default: builders: + retrofit_generator: + options: + class-name: RestClientYmlp reflectable: generate_for: - lib/json_mapper_example.dart \ No newline at end of file diff --git a/example/lib/example.dart b/example/lib/example.dart index 62700bd81..719afac04 100644 --- a/example/lib/example.dart +++ b/example/lib/example.dart @@ -11,7 +11,7 @@ part 'example.g.dart'; @RestApi(baseUrl: 'https://5d42a6e2bc64f90014a56ca0.mockapi.io/api/v1/') abstract class RestClient { - factory RestClient(Dio dio, {String baseUrl}) = _RestClient; + factory RestClient(Dio dio, {String baseUrl}) = RestClientYmlp; @GET('/tasks/{id}') Future> getNestApiResultGenericsInnerTypeNullable(); @@ -65,7 +65,8 @@ abstract class RestClient { @PreventNullToAbsent() @PATCH('/tasks/{id}') - Future updateTaskAvatar(@Path() String id, @Field('avatar') String? avatar); + Future updateTaskAvatar( + @Path() String id, @Field('avatar') String? avatar); @DELETE('/tasks/{id}') Future deleteTask(@Path() String id); diff --git a/generator/lib/src/generator.dart b/generator/lib/src/generator.dart index 2091bfc2c..de3691f7c 100644 --- a/generator/lib/src/generator.dart +++ b/generator/lib/src/generator.dart @@ -19,7 +19,11 @@ const _analyzerIgnores = '// ignore_for_file: unnecessary_brace_in_string_interps,no_leading_underscores_for_local_identifiers'; class RetrofitOptions { - RetrofitOptions({this.autoCastResponse, this.emptyRequestBody}); + RetrofitOptions({ + this.autoCastResponse, + this.emptyRequestBody, + this.className, + }); RetrofitOptions.fromOptions([BuilderOptions? options]) : autoCastResponse = @@ -27,10 +31,12 @@ class RetrofitOptions { 'true', emptyRequestBody = (options?.config['empty_request_body']?.toString() ?? 'false') == - 'true'; + 'true', + className = options?.config['class-name']?.toString(); final bool? autoCastResponse; final bool? emptyRequestBody; + final String? className; } class RetrofitGenerator extends GeneratorForAnnotation { @@ -78,7 +84,7 @@ class RetrofitGenerator extends GeneratorForAnnotation { } String _implementClass(ClassElement element, ConstantReader? annotation) { - final className = element.name; + final className = globalOptions.className ?? '_${element.name}'; final enumString = annotation?.peek('parser')?.revive().accessor; final parser = retrofit.Parser.values .firstWhereOrNull((e) => e.toString() == enumString); @@ -91,7 +97,7 @@ class RetrofitGenerator extends GeneratorForAnnotation { .where((c) => !c.isFactory && !c.isDefaultConstructor); final classBuilder = Class((c) { c - ..name = '_$className' + ..name = className ..types.addAll(element.typeParameters.map((e) => refer(e.name))) ..fields.addAll([_buildDioFiled(), _buildBaseUrlFiled(baseUrl)]) ..constructors.addAll( @@ -428,8 +434,9 @@ class RetrofitGenerator extends GeneratorForAnnotation { .statement, ); - final preventNullToAbsent = _getMethodAnnotationByType(m, retrofit.PreventNullToAbsent); - + final preventNullToAbsent = + _getMethodAnnotationByType(m, retrofit.PreventNullToAbsent); + if (preventNullToAbsent == null && headers.isNotEmpty) { blocks.add( const Code('$_localHeadersVar.removeWhere((k, v) => v == null);'), @@ -1470,9 +1477,10 @@ if (T != dynamic && blocks.add(refer('$queryParamsVar.addAll').call([expression]).statement); } - - final preventNullToAbsent = _getMethodAnnotationByType(m, retrofit.PreventNullToAbsent); - + + final preventNullToAbsent = + _getMethodAnnotationByType(m, retrofit.PreventNullToAbsent); + final anyNullable = m.parameters .any((p) => p.type.nullabilitySuffix == NullabilitySuffix.question); @@ -1495,9 +1503,10 @@ if (T != dynamic && ); return; } - - final preventNullToAbsent = _getMethodAnnotationByType(m, retrofit.PreventNullToAbsent); - + + final preventNullToAbsent = + _getMethodAnnotationByType(m, retrofit.PreventNullToAbsent); + final annotation = _getAnnotation(m, retrofit.Body); final bodyName = annotation?.item1; if (bodyName != null) {