Skip to content

Commit

Permalink
Fix/enum return types and query params (#592)
Browse files Browse the repository at this point in the history
* fix: enum return types generated without calling .toJson() method

* fix: enums as query params generated with .name
  • Loading branch information
elenaferr0 authored Jun 29, 2023
1 parent 7c9d815 commit 2f9b6db
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
5 changes: 5 additions & 0 deletions generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Changelog
## 7.0.7

- Enums return types generated iterating over the enum values instead of calling `.toJson()` method
- Enums as query parameters generated with `.name` instead of `toJson()`

## 7.0.6

- Fix DateTime.toIso8601String() issue #586
Expand Down
29 changes: 25 additions & 4 deletions generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -878,9 +878,19 @@ You should create a new class to encapsulate the response.
'$displayString.fromJson($_resultVar.data!,${_getInnerJsonSerializableMapperFn(returnType)})',
);
} else {
mapperCode = refer(
'${_displayString(returnType)}.fromJson($_resultVar.data!)',
);
if (_isEnum(returnType)) {
mapperCode = refer(
'${_displayString(returnType)}.values.firstWhere((e) => e.name == _result.data,'
'orElse: () => throw ArgumentError('
'\'${_displayString(returnType)} does not contain value \${_result.data}\','
'),'
')',
);
} else {
mapperCode = refer(
'${_displayString(returnType)}.fromJson($_resultVar.data!)',
);
}
}
break;
case retrofit.Parser.DartJsonMapper:
Expand Down Expand Up @@ -1297,6 +1307,12 @@ if (T != dynamic &&
_typeChecker(Float).isExactlyType(returnType);
}

bool _isEnum(DartType? dartType){
if (dartType == null || dartType.element == null){
return false;
}
return dartType.element is EnumElement;
}
bool _isDateTime(DartType? dartType) {
if (dartType == null) {
return false;
Expand Down Expand Up @@ -1331,7 +1347,12 @@ if (T != dynamic &&
.nullSafeProperty('toIso8601String')
.call([])
: refer(p.displayName).property('toIso8601String').call([]);
} else {
} else if(_isEnum(p.type)) {
value = p.type.nullabilitySuffix == NullabilitySuffix.question
? refer(p.displayName)
.nullSafeProperty('name')
: refer(p.displayName).property('name');
}else{
value = p.type.nullabilitySuffix == NullabilitySuffix.question
? refer(p.displayName).nullSafeProperty('toJson').call([])
: refer(p.displayName).property('toJson').call([]);
Expand Down
2 changes: 1 addition & 1 deletion generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ topics:
- build-runner
- codegen
- api
version: 7.0.6
version: 7.0.7
environment:
sdk: '>=2.19.0 <4.0.0'

Expand Down
36 changes: 36 additions & 0 deletions generator/test/src/generator_test_src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,42 @@ abstract class StreamReturnType {
Stream<User> getUser();
}

enum TestEnum { A, B }

@ShouldGenerate(
'''
final value = TestEnum.values.firstWhere(
(e) => e.name == _result.data,
orElse: () => throw ArgumentError(
'TestEnum does not contain value \${_result.data}',
),
);
return value;
''',
contains: true,
)
@RestApi()
abstract class EnumReturnType {
@GET('/')
Future<TestEnum> getTestEnum();
}

enum EnumParam {
enabled, disabled,
}

@ShouldGenerate(
'''
final queryParameters = <String, dynamic>{r'test': status?.name};
''',
contains: true,
)
@RestApi()
abstract class TestQueryParamEnum {
@GET('/test')
Future<void> getTest(@Query('test') EnumParam? status);
}

@ShouldGenerate(
'''
Stream<User> getUser() async* {
Expand Down

0 comments on commit 2f9b6db

Please sign in to comment.