Skip to content

Commit

Permalink
Fix issue of callAdapter using yield/return incorrectly (#739)
Browse files Browse the repository at this point in the history
* fix bug of using yield in place of return in certain cases

* fix test

---------

Co-authored-by: Trevor Wang <trevor.wang@qq.com>
  • Loading branch information
farmery and trevorwang authored Feb 9, 2025
1 parent b9cebff commit adfaaaa
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
3 changes: 2 additions & 1 deletion generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

## 9.1.8

- Fixed bug of callAdapter using yield/return incorrectly
- Fixed issue which generated invalid code for the same path parameter appears multiple times.

Example:
Expand Down Expand Up @@ -40,7 +41,7 @@
@UseCallAdapter(MyCallAdapter)
@GET('/')
Future<User> getTasks();
Future<Either<ApiError, User>> getTasks();
}
```

Expand Down
10 changes: 9 additions & 1 deletion generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,16 @@ class RetrofitGenerator extends GeneratorForAnnotation<retrofit.RestApi> {
ConstantReader httpMethod,
InterfaceType? callAdapter,
) {
final returnAsyncWrapper =
String returnAsyncWrapper =
m.returnType.isDartAsyncFuture ? 'return' : 'yield';
if (callAdapter != null) {
final callAdapterOriginalReturnType = callAdapter.superclass
?.typeArguments.firstOrNull as InterfaceType?;
returnAsyncWrapper = _isReturnTypeFuture(
callAdapterOriginalReturnType?.getDisplayString() ?? '')
? 'return'
: 'yield';
}
final path = _generatePath(m, httpMethod);
final blocks = <Code>[];

Expand Down
2 changes: 1 addition & 1 deletion generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ topics:
- rest
- retrofit
- codegen
version: 9.1.6
version: 9.1.8
environment:
sdk: '>=3.3.0 <4.0.0'

Expand Down
29 changes: 25 additions & 4 deletions generator/test/src/generator_test_src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,31 @@ class MockCallAdapter3<T> extends CallAdapter<Future<T>, Flow<T>> {
}

@ShouldGenerate(
'''
@override
Flow<User> getUser() {
return MockCallAdapter3<User>().adapt(() => _getUser());
'''
Future<User> _getUser() async {
final _extra = <String, dynamic>{};
final queryParameters = <String, dynamic>{};
final _headers = <String, dynamic>{};
const Map<String, dynamic>? _data = null;
final _options = _setStreamType<User>(
Options(method: 'GET', headers: _headers, extra: _extra)
.compose(
_dio.options,
'path',
queryParameters: queryParameters,
data: _data,
)
.copyWith(baseUrl: _combineBaseUrls(_dio.options.baseUrl, baseUrl)),
);
final _result = await _dio.fetch<Map<String, dynamic>>(_options);
late User _value;
try {
_value = User.fromJson(_result.data!);
} on Object catch (e, s) {
errorLogger?.logError(e, s, _options);
rethrow;
}
return _value;
}
''',
contains: true,
Expand Down

0 comments on commit adfaaaa

Please sign in to comment.