retrofit.dart icon indicating copy to clipboard operation
retrofit.dart copied to clipboard

improve handling of enum values in method parameters

Open ekuleshov opened this issue 2 years ago • 0 comments

Is your feature request related to a problem? Please describe.

You can declare an enum type for the API method parameters using @Path, @Field, @Query and @Body annotations:

  @POST('/api/animals/{species}')
  Future<List<Animal>> animalForSpecies(@Path('species') Species species);

with enum simply declared like:

enum Species {
 cat, dog
}

When enums are used as API method parameters, the retrofit generator generates simple .toString() value substitution like:

   .compose(_dio.options, '/api/animals/${species}',

As a result the value is sent as a string like Species.cat instead of cat.

Describe the solution you'd like

It would be really handy if generator used the species.name for enum values, or better did the same value mapping as json_serializable, using @JsonValue annotation if it is declared for a given enum (e.g. when some codes are sent instead of the enum names)

Describe alternatives you've considered

Currently you have to use strings instead of enum values, which is leading to a not-type-safe API.

It is also not possible to override the toString() in enums.

ekuleshov avatar Apr 07 '22 19:04 ekuleshov