Skip to content

Commit

Permalink
fix: fix enum generation #696
Browse files Browse the repository at this point in the history
  • Loading branch information
MartynasZabulionis committed Jul 18, 2024
1 parent 30f37b4 commit e76b304
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -625,27 +625,30 @@ class RetrofitGenerator extends GeneratorForAnnotation<retrofit.RestApi> {
.statement,
);
} else {
final castType =
_isEnum(innerReturnType) ? 'String' : 'Map<String, dynamic>';

final Reference mapperCode;
switch (clientAnnotation.parser) {
case retrofit.Parser.MapSerializable:
mapperCode = refer(
'(dynamic i) => ${_displayString(innerReturnType)}.fromMap(i as Map<String,dynamic>)',
'(dynamic i) => ${_displayString(innerReturnType)}.fromMap(i as $castType)',
);
break;
case retrofit.Parser.JsonSerializable:
if (innerReturnType?.isNullable ?? false) {
mapperCode = refer(
'(dynamic i) => i == null ? null : ${_displayString(innerReturnType)}.fromJson(i as Map<String,dynamic>)',
'(dynamic i) => i == null ? null : ${_displayString(innerReturnType)}.fromJson(i as $castType)',
);
} else {
mapperCode = refer(
'(dynamic i) => ${_displayString(innerReturnType)}.fromJson(i as Map<String,dynamic>)',
'(dynamic i) => ${_displayString(innerReturnType)}.fromJson(i as $castType)',
);
}
break;
case retrofit.Parser.DartJsonMapper:
mapperCode = refer(
'(dynamic i) => JsonMapper.fromMap<${_displayString(innerReturnType)}>(i as Map<String,dynamic>)!',
'(dynamic i) => JsonMapper.fromMap<${_displayString(innerReturnType)}>(i as $castType)!',
);
break;
case retrofit.Parser.FlutterCompute:
Expand Down Expand Up @@ -1630,7 +1633,7 @@ if (T != dynamic &&
refer(
'${bodyName.displayName}.openRead()',
),
)
)
.statement,
);
} else if (bodyName.type.element is ClassElement) {
Expand Down

0 comments on commit e76b304

Please sign in to comment.