Skip to content

Commit

Permalink
Fix enum generation #696 and #663 (#697)
Browse files Browse the repository at this point in the history
* fix: fix enum generation #663

* fix: fix enum generation #696
  • Loading branch information
MartynasZabulionis authored Aug 13, 2024
1 parent 426e6d8 commit 0e5fa6c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -653,27 +653,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 @@ -1683,7 +1686,7 @@ if (T != dynamic &&
refer(
'${bodyName.displayName}.openRead()',
),
)
)
.statement,
);
} else if (bodyName.type.element is ClassElement) {
Expand Down Expand Up @@ -2052,7 +2055,7 @@ ${bodyName.displayName} == null
} else {
throw Exception('Unknown error!');
}
} else if (_isBasicType(p.type)) {
} else if (_isBasicType(p.type) || _isEnum(p.type)) {
if (p.type.nullabilitySuffix == NullabilitySuffix.question) {
blocks.add(Code('if (${p.displayName} != null) {'));
}
Expand All @@ -2062,6 +2065,8 @@ ${bodyName.displayName} == null
literal(fieldName),
if (_typeChecker(String).isExactlyType(p.type))
refer(p.displayName)
else if (_isEnum(p.type))
refer(p.displayName).property('name')
else
refer(p.displayName).property('toString').call([])
])
Expand Down

0 comments on commit 0e5fa6c

Please sign in to comment.