Skip to content

Commit cc08df1

Browse files
Copilottrevorwang
andauthored
Fix map value deserialization for generic type-parameter models in generator output (#916)
* Initial plan * fix(generator): handle type parameters in map generic value deserialization Agent-Logs-Url: https://github.com/trevorwang/retrofit.dart/sessions/e48c0b86-23bc-473a-89ba-a5a699fabd2a Co-authored-by: trevorwang <121966+trevorwang@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: trevorwang <121966+trevorwang@users.noreply.github.com>
1 parent 19fe357 commit cc08df1

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

generator/lib/src/generator.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,9 @@ $returnAsyncWrapper httpResponse;
17411741
return mappedVal;
17421742
}
17431743
} else {
1744-
if (_displayString(dartType) == 'dynamic' || _isBasicType(dartType)) {
1744+
if (_displayString(dartType) == 'dynamic' ||
1745+
_isBasicType(dartType) ||
1746+
dartType is TypeParameterType) {
17451747
return '(json) => json as ${_displayString(dartType, withNullability: dartType.isNullable)},';
17461748
} else {
17471749
if (_displayString(dartType) == 'void') {

generator/test/src/generator_test_src.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,31 @@ abstract class TestMapBodyWithGeneric {
12671267
Future<Map<String, GenericUser<User>>> getResult();
12681268
}
12691269

1270+
@ShouldGenerate('''
1271+
late Map<String, GenericUser<T>> _value;
1272+
try {
1273+
_value = _result.data!.map(
1274+
(k, dynamic v) => MapEntry(
1275+
k,
1276+
GenericUser<T>.fromJson(
1277+
v as Map<String, dynamic>,
1278+
(json) => json as T,
1279+
),
1280+
),
1281+
);
1282+
} on Object catch (e, s) {
1283+
errorLogger?.logError(e, s, _options, response: _result);
1284+
rethrow;
1285+
}
1286+
return _value;
1287+
}
1288+
''', contains: true)
1289+
@RestApi(baseUrl: 'https://httpbin.org/')
1290+
abstract class TestMapBodyWithGenericTypeParameter<T> {
1291+
@GET('/xx')
1292+
Future<Map<String, GenericUser<T>>> getResult();
1293+
}
1294+
12701295
@ShouldGenerate('''
12711296
late List<String> _value;
12721297
try {

0 commit comments

Comments
 (0)