trevorwang / retrofit.dart

retrofit.dart is an dio client generator using source_gen and inspired by Chopper and Retrofit.
https://mings.in/retrofit.dart/
MIT License
1.06k stars 241 forks source link

enum as request parameter causes retrofit_generator to generate a faulty file #663

Open MohaAmiry opened 4 months ago

MohaAmiry commented 4 months ago

Describe the bug Adding enum as a parameter generates a file with an error: the argument type 'enumType' can't be assigned to the parameter type 'String'

To Reproduce

@JsonSerializable(explicitToJson: true)
class Model {
  ProviderType type;
  Model(this.type);
  factory Model.fromJson(Map<String, dynamic> json) => _$ModelFromJson(json);

  Map<String, dynamic> toJson() => _$ModelToJson(this);
}

@JsonEnum()
enum ProviderType {
  @JsonValue(1)
  t1,
  @JsonValue(2)
  t2,
  @JsonValue(3)
  t3;
}

@RestApi()
abstract class API {
  factory API(Dio dio, {String? baseUrl}) = _API;

  @MultiPart()
  @POST("path")
  Future<Model> testAPI({@Part(name: "param") required ProviderType type});
}

the generated file is like this:

code ....

  @override
  Future<Model> testAPI({required ProviderType type}) async {
    final _extra = <String, dynamic>{};
    final queryParameters = <String, dynamic>{};
    final _headers = <String, dynamic>{};
    final _data = FormData();
    _data.fields.add(MapEntry(
      'param',
      type, // the error is here, it is supposed to be type.toString(), or type.index.toString(), or type.toJson() as a manual parser is existed.
    ));
    final _result = ....

Expected behavior the expected behavior is to generate a file that automatically decode the enum to the mapped JsonValue