Open lynzrand opened 5 years ago
your error is because you didn't call _initMirrors()
function in your main
:
your full code should look as fallow:
library test; // don't forget to change the name of your library
import 'package:dson/dson.dart';
part 'test.g.dart';
main(List<String> args) {
_initMirrors(); // This call is needed to initialize mirrors before doing serialization/deserialization
toMap(new SimpleTest());
}
@serializable
class SimpleTest {
String x;
SimpleTest() {
x = null;
}
}```
Oh, my fault. Thanks, and sorry for taking your time!
From: noreply@github.com noreply@github.com on behalf of Luis Vargas notifications@github.com Sent: Tuesday, March 26, 2019 10:29:44 AM To: dart-league/dson Cc: Rynco Li; Author Subject: Re: [dart-league/dson] Calling toMap() on an object with null fields throws an error (#46)
your error is because you didn't call _initMirrors() function in your main:
your full code should look as fallow:
library test; // don't forget to change the name of your library
import 'package:dson/dson.dart';
part 'test.g.dart';
main(List
toMap(new SimpleTest()); }
@serializable class SimpleTest { String x; SimpleTest() { x = null; } }```
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/dart-league/dson/issues/46#issuecomment-476450004, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJyKr_kTCDTYpqeq0MoOMR7ZJxnUyxzAks5vaYYYgaJpZM4cFloZ.
don't worry, it's cool to help users of my library
Sorry, but the problem is here again. I have called _initMirrors()
in the main isolate, but now I'm working in another isolate where no entry point like main function. Where should I call _initMirrors
in this situation?
More precisely, I'm working with the Aqueduct
HTTP server package and I need to serialize stuff. The serializer/deserializer coming with the package has not enough features for me to fiddle with (like ignoring fields at runtime). Aqueduct works by using separate isolates to handle HTTP requests, and in one of those Isolates I still got this "not initialized" error.
What should I do now?
could you show me your full code or a full example?
The issue occurs here: https://github.com/01010101lzy/akali/blob/375079783a8168ad2cfc79505f334de3670b6b9f/lib/data/db/mongodb.dart#L145
Future<ActionResult<Pic>> updateImgInfo(Pic newInfo, String id) async {
var _id = ObjectId.fromHexString(id);
var result = await picCollection.update(where.id(_id), newInfo.asMap());
newInfo.id = _id;
bool success = result['nModified'] > 0;
if (success)
return ActionResult()
..success = true
..data = newInfo
..affected = result['nModified'];
else
return ActionResult()..success = false;
}
In this function, the Pic.asMap()
method is calling return toMap(this)
inside.
This function is called by this line: https://github.com/01010101lzy/akali/blob/375079783a8168ad2cfc79505f334de3670b6b9f/lib/api/imgRequest.dart#L213
This is the handling function of HTTP PUT of /api/v1/img/:id
path
/// PUT `img/:id`
///
/// Updates image [id]'s data by [newInfo].
/// Requires authorization beforehand.
@Operation.put('id')
Future<Response> putImage(
@Bind.path('id') String id,
@Bind.body() Pic newInfo, [
@Bind.query('access_token') String tokenQuery,
@Bind.header('Access-Token') String tokenHeader,
]) async {
var result;
try {
result = await db.updateImgInfo(newInfo, id);
} catch (e, stack) {
logger.severe("Error in putImage()", e, stack);
throw Response.serverError(
body: {'error': e, 'message': result, 'stackTrace': stack});
}
return Response.ok(result);
}
The rest of the handling process is managed by Aqueduct so I don't have access on them.
The whole repository is here: https://github.com/01010101lzy/akali
dson
version: 0.15.5dson_core
version: 0.15.4Minimal example to reproduce bug:
After
build_runner build
, run the script and the following error was thrown:The error appears to be from this line in
dson_core
, so I went to see the code. It seems that this instance variable somehow passed the primitive type check at line 11 (which in theory should block this null value):Then this null value got to
_serializeObject()
function, passed to thereflect()
function inbuilt_mirrors_core
and returned as null instead of aClassMirror
object.I have little idea how this should be fixed though...
update: fixed format error