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

support iterable/list values as @Body

Open ekuleshov opened this issue 2 years ago • 3 comments

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

It is somewhat common to pass a list of objects or a list of primitive types (int, string, enum, etc) as a @Body parameter. E.g.

  @POST('/api/cases/delete')
  Future<TestRead> sasesDelete(@Body() List<int> ids);

The body is simply a json array passed at the root.

However currently the retrofit generator gives this warning for a List

[WARNING] retrofit_generator:retrofit on .../client.dart:
List<int> must provide a `toJson()` method which return a Map.
It is programmer's responsibility to make sure the List<int> is properly serialized

Describe the solution you'd like

It would be great to have retrofit properly serialize lists/iterable values containing objects, enums or primitive values

Describe alternatives you've considered

Not sure what alternative is. Json arrays are valid option for the root body entity.

Additional context

n/a

ekuleshov avatar May 20 '22 14:05 ekuleshov

Having a look at the examples, it looks like this feature has been already implemented. Could you please provide us a MRE?

ilovelinux avatar Jun 17 '22 18:06 ilovelinux

Having a look at the examples, it looks like this feature has been already implemented. Could you please provide us a MRE?

Here is the test method you can add into the generator_test_src.dart

@ShouldGenerate(
  r'''
    final _data = list;
''',
  contains: true,
  expectedLogItems: [
    "List<int> must provide a `toJson()` method which return a Map.\n"
        "It is programmer's responsibility to make sure the List<int> is properly serialized",
  ]
)
@RestApi(baseUrl: "https://httpbin.org/")
abstract class TestBasicListIntParameter {
  @GET("/xx")
  Future<int> getCount(@Body() List<int> list);
}

The generated code seem to work, yet there is a warning produced.

I don't think this warning about List<int> not having toJson() method makes sense. So, probably RetrofitGenerator._missingToJson() method need to be updated to not produce such warning for collections of simple types.

ekuleshov avatar Jun 17 '22 20:06 ekuleshov

Need to pass----> @Body String singleWord <----- but couldn't send

vinayvishnu725 avatar Dec 07 '22 15:12 vinayvishnu725