ServiceStack / Issues

Issue Tracker for the commercial versions of ServiceStack
11 stars 8 forks source link

Dart client not able to deserialize List responses #721

Closed rockgecko-development closed 4 years ago

rockgecko-development commented 4 years ago

The Dart JsonServiceClient is unable to deserialize list responses. This is the error:

Unhandled Exception: Invalid argument(s): Unknown Type 'null', see: https://docs.servicestack.net/dart-add-servicestack-reference#generating-unknown-types
E/flutter (32347): #0      TypeContext.typeInfo (package:servicestack/interfaces.dart:47:7)
E/flutter (32347): #1      ListConverter.fromJson (package:servicestack/json_converters.dart:316:21)
E/flutter (32347): #2      JsonServiceClient.createResponse.<anonymous closure> (package:servicestack/client.dart:492:45)
E/flutter (32347): #3      JsonServiceClient.createResponse (package:servicestack/client.dart:495:26)
E/flutter (32347): <asynchronous suspension>
E/flutter (32347): #4      JsonServiceClient.sendRequest (package:servicestack/client.dart:289:28)
E/flutter (32347): <asynchronous suspension>

This is the request dto:

class GetItemsRequest implements IReturn<List<Item>>, IConvertible {
  GetItemsRequest();
  GetItemsRequest.fromJson(Map<String, dynamic> json) : super();
  fromMap(Map<String, dynamic> json) {
    return this;
  }

  Map<String, dynamic> toJson() => {};
  createResponse() {
    return new List<Item>();
  }

  String getTypeName() {
    return "GetItemsRequest";
  }

  TypeContext context = _ctx;
}
TypeContext _ctx =
    new TypeContext(library: 'localhost', types: <String, TypeInfo>{
    'Item': new TypeInfo(TypeOf.Class, create: () => new Item()),
      'List<Item>': new TypeInfo(TypeOf.Class, create: () => new List<Item>()),
});

I was able to fix this issue by overriding createResponse and changing: new ListConverter().fromJson(jsonObj, reqContext) to JsonConverters.fromJson(json, responseAs.runtimeType.toString(), reqContext) here https://github.com/ServiceStack/servicestack-dart/blob/e5e1e98a3489a9ad6e924b6d1b83b929d56dc552/lib/client.dart#L492

Conveniently, responseAs.runtimeType.toString() returns the string "List<Item>", which is easily retrieved from TypeContext.

Thanks!

mythz commented 4 years ago

thx for reporting & the analysis! This should be resolved in this commit.

This change is now available from v1.0.13 that's now available on pub.dev.

rockgecko-development commented 4 years ago

Thanks very much, always impressed at how fast you respond to stuff like this!