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

Multipart issues #667

Closed sbatezat closed 3 months ago

sbatezat commented 3 months ago

Describe the bug When I'm calling the retrofit method, I've got this error :

      Error: Converting object to an encodable object failed: Instance of 'FormData'
      ### DioException: DioException [unknown]: null
      Error: Converting object to an encodable object failed: Instance of 'FormData'
      #0      Options.compose (package:dio/src/options.dart:347:56)

pubspec.yaml

  dio: ^5.4.2+1
  json_annotation: ^4.8.1
  retrofit: ^4.1.0

To Reproduce Steps to reproduce the behavior:

  1. Create this DTO
import 'package:json_annotation/json_annotation.dart';

part 'declaration.dto.g.dart';

@JsonSerializable()
class DeclarationDto {
  @JsonKey(name: 'type')
  final String type;

  @JsonKey(name: 'date')
  final DateTime? date;

  @JsonKey(name: 'message')
  final String? message;

  const DeclarationDto({
    required this.type,
    this.date,
    this.message,
  });

  factory DeclarationDto.fromJson(Map<String, dynamic> json) {
    return _$DeclarationDtoFromJson(json);
  }

  Map<String, dynamic> toJson() => _$DeclarationDtoToJson(this);
}
  1. Create this retrofit call :
@LazySingleton()
@RestApi()
abstract class MyEndpoint {
  @factoryMethod
  factory MyEndpoint(Dio dio) = _MyEndpoint;

  @POST("/my/{id}/url")
  @Extra({
    AnalyticsInterceptor.analyticsUriExtraKey: 'my extra key'
  })
  Future<void> postMessage(
    @Path("id") int id,
    @Part() DeclarationDto data, {
    @Part() File? file,
  });
}

First surprise, it doesn't compile if i'm not explicitly importing this :

import 'dart:convert' show jsonEncode;

It's not documented, but I've seen it on the example. It's weird to import something not explicitly used on my file.

When I run it, on runtime, I've got this error :

      Error: Converting object to an encodable object failed: Instance of 'FormData'
      ### DioException: DioException [unknown]: null
      Error: Converting object to an encodable object failed: Instance of 'FormData'
      #0      Options.compose (package:dio/src/options.dart:347:56)

Expected behavior Working as expected

sbatezat commented 3 months ago

It was an error thrown by my interceptor... My bad.