diff --git a/generator/lib/src/generator.dart b/generator/lib/src/generator.dart index a2a009b44..aed1116c4 100644 --- a/generator/lib/src/generator.dart +++ b/generator/lib/src/generator.dart @@ -197,9 +197,8 @@ class RetrofitGenerator extends GeneratorForAnnotation { ConstantReader? _getMethodAnnotation(MethodElement method) { for (final type in _methodsAnnotations) { - final annot = _typeChecker(type) - .firstAnnotationOf(method, throwOnUnresolved: false); - if (annot != null) return ConstantReader(annot); + final annot = _getMethodAnnotationByType(method, type); + if (annot != null) return annot; } return null; } @@ -211,18 +210,8 @@ class RetrofitGenerator extends GeneratorForAnnotation { return null; } - ConstantReader? _getHeadersAnnotation(MethodElement method) { - final annotation = _typeChecker(retrofit.Headers) - .firstAnnotationOf(method, throwOnUnresolved: false); - if (annotation != null) return ConstantReader(annotation); - return null; - } - ConstantReader? _getCacheAnnotation(MethodElement method) { - final annotation = _typeChecker(retrofit.CacheControl) - .firstAnnotationOf(method, throwOnUnresolved: false); - if (annotation != null) return ConstantReader(annotation); - return null; + return _getMethodAnnotationByType(method, retrofit.CacheControl); } ConstantReader? _getContentTypeAnnotation(MethodElement method) { @@ -238,24 +227,22 @@ class RetrofitGenerator extends GeneratorForAnnotation { } ConstantReader? _getMultipartAnnotation(MethodElement method) { - final annotation = _typeChecker(retrofit.MultiPart) - .firstAnnotationOf(method, throwOnUnresolved: false); - if (annotation != null) return ConstantReader(annotation); - return null; + return _getMethodAnnotationByType(method, retrofit.MultiPart); } ConstantReader? _getFormUrlEncodedAnnotation(MethodElement method) { - final annotation = _typeChecker(retrofit.FormUrlEncoded) - .firstAnnotationOf(method, throwOnUnresolved: false); - if (annotation != null) return ConstantReader(annotation); - return null; + return _getMethodAnnotationByType(method, retrofit.FormUrlEncoded); } ConstantReader? _getResponseTypeAnnotation(MethodElement method) { - final annotation = _typeChecker(retrofit.DioResponseType) - .firstAnnotationOf(method, throwOnUnresolved: false); - if (annotation != null) return ConstantReader(annotation); - return null; + return _getMethodAnnotationByType(method, retrofit.DioResponseType); + } + + Iterable _getMethodAnnotations( + MethodElement method, Type type) { + return _typeChecker(type) + .annotationsOf(method, throwOnUnresolved: false) + .map((e) => ConstantReader(e)); } Map _getAnnotations( @@ -1584,12 +1571,15 @@ You should create a new class to encapsulate the response. } Map _generateHeaders(MethodElement m) { - final anno = _getHeadersAnnotation(m); - final headersMap = anno?.peek("value")?.mapValue ?? {}; - final headers = headersMap.map((k, v) { + final headers = _getMethodAnnotations(m, retrofit.Headers) + .map((e) => e.peek('value')) + .map((value) => value?.mapValue.map((k, v) { return MapEntry( - k?.toStringValue() ?? 'null', literal(v?.toStringValue())); - }); + k?.toStringValue() ?? 'null', + literal(v?.toStringValue()), + ); + })) + .fold>({}, (p, e) => p..addAll(e ?? {})); final annosInParam = _getAnnotations(m, retrofit.Header); final headersInParams = annosInParam.map((k, v) { @@ -1645,13 +1635,10 @@ You should create a new class to encapsulate the response. void _generateExtra( MethodElement m, List blocks, String localExtraVar) { - final extra = _typeChecker(retrofit.Extra) - .firstAnnotationOf(m, throwOnUnresolved: false); - - if (extra != null) { - final c = ConstantReader(extra); blocks.add(literalMap( - c.peek('data')?.mapValue.map((k, v) { + _getMethodAnnotations(m, retrofit.Extra) + .map((e) => e.peek('data')) + .map((data) => data?.mapValue.map((k, v) { return MapEntry( k?.toStringValue() ?? (throw InvalidGenerationSourceError( @@ -1670,18 +1657,11 @@ You should create a new class to encapsulate the response. (v?.toTypeValue() ?? (v != null ? Code(revivedLiteral(v)) : Code('null'))), ); - }) ?? - {}, + })) + .fold>({}, (p, e) => p..addAll(e ?? {})), refer('String'), refer('dynamic'), ).assignConst(localExtraVar).statement); - } else { - blocks.add(literalMap( - {}, - refer('String'), - refer('dynamic'), - ).assignConst(localExtraVar).statement); - } } bool _missingToJson(ClassElement ele) { diff --git a/generator/test/src/generator_test_src.dart b/generator/test/src/generator_test_src.dart index 94b62a843..3b695c5f2 100644 --- a/generator/test/src/generator_test_src.dart +++ b/generator/test/src/generator_test_src.dart @@ -1,6 +1,6 @@ import 'dart:io'; -import 'package:dio/dio.dart'; +import 'package:dio/dio.dart' hide Headers; import 'package:retrofit/retrofit.dart'; import 'package:source_gen_test/annotations.dart'; @@ -56,6 +56,20 @@ abstract class ExtrasWithPrimitiveValues { Future list(); } +@ShouldGenerate( + r''' + const _extra = {'key': 'value', 'key2': 'value2'}; +''', + contains: true, +) +@RestApi() +abstract class MultipleExtrasWithPrimitiveValues { + @GET('/list/') + @Extra({'key': 'value'}) + @Extra({'key2': 'value2'}) + Future list(); +} + @ShouldGenerate( r''' const _extra = {'key': CustomConstant()}; @@ -73,6 +87,46 @@ class CustomConstant { const CustomConstant(); } +@ShouldGenerate( + r''' + final _headers = {}; +''', + contains: true, +) +@RestApi() +abstract class EmptyHeaders { + @GET('/list/') + @Headers({}) + Future list(); +} + +@ShouldGenerate( + r''' + final _headers = {r'key': 'value'}; +''', + contains: true, +) +@RestApi() +abstract class HeadersWithPrimitiveValues { + @GET('/list/') + @Headers({'key': 'value'}) + Future list(); +} + +@ShouldGenerate( + r''' + final _headers = {r'key': 'value', r'key2': 'value2'}; +''', + contains: true, +) +@RestApi() +abstract class MultipleHeadersWithPrimitiveValues { + @GET('/list/') + @Headers({'key': 'value'}) + @Headers({'key2': 'value2'}) + Future list(); +} + @ShouldGenerate( r'''Options(method: 'GET',''', contains: true,