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

toJson will not add if we use freezed.

Open b14cknc0d3 opened this issue 8 months ago • 6 comments

Describe the bug toJson will not add if we use freezed. and show this warning

YourModel must provide a `toJson()` method which return a Map.

To Reproduce Steps to reproduce the behavior:

  1. create a model with freezed
@freezed
class FoodTagCreateModel with _$FoodTagCreateModel {
  const factory FoodTagCreateModel({
    required String name,
    required String description,
  }) = _FoodTagCreateModel;

  factory FoodTagCreateModel.fromJson(Map<String, dynamic> json) =>
      _$FoodTagCreateModelFromJson(json);

}
  1. generate a ResApi client that use that model and run build runner
@RestApi()
abstract class CrudApi {
  factory CrudApi(Dio dio, {String? baseUrl}) = _CrudApi;

  @POST('/api/v1/my-model')
  Future<void> createAModel(@Body() MyModel body);
  1. See error
MyModel must provide a `toJson()` method which return a Map.
It is programmer's responsibility to make sure the MyModel is properly serialized

Expected behavior If body is not Map force to add toJson() whether it has toJson() or not.

b14cknc0d3 avatar Jun 11 '24 03:06 b14cknc0d3