k-paxian / dart-json-mapper

Serialize / Deserialize Dart Objects to / from JSON
https://pub.dev/packages/dart_json_mapper
Other
402 stars 33 forks source link

How does the builder finds what types to include in `valueDecorators`? #99

Closed caneva20 closed 3 years ago

caneva20 commented 3 years ago

Hi @k-paxian!

I'm migrating one of my projects from mapper 1.5.24 to 1.7.2. In this new version the biggest change is the builder that generates most of the boilerplate code me (a nice addition btw), but almost half the types aren't being added to this list (valueDecorators), all of those types are marked with @jsonSerializable. Am I doing something wrong / missing something? Is there some way for me to make the build pick those types?

k-paxian commented 3 years ago

Only one thing comes to my mind, those classes must be public, to be able to import them from *.mapper.g.dart

Also, it could be unused code as well. If you are not actually importing those classes anywhere in your code, they will be omitted.

caneva20 commented 3 years ago

Well, they are public and are imported in this file already, and it seems reflectable has generated all the info for them correctly,

k-paxian commented 3 years ago

Then we need some cases to investigate more closely. If you could extract or illustrate the case it would be helpful

caneva20 commented 3 years ago

I'll try to have something reproducible then, I'll be back when I get something.

caneva20 commented 3 years ago

Good news (I guess), I found something.

Minimum reproducible code:

import 'package:dart_json_mapper/dart_json_mapper.dart';

@jsonSerializable
class Foo {
  String name;

  Foo(this.name);
}

import 'bar.dart'; //<----- HERE IS THE PROBLEM! // import 'foo.dart'; //<----- Importing the model directly works fine! import 'main.mapper.g.dart';

void main(List arguments) { initializeJsonMapper();

var json = JsonMapper.serialize([Foo('Jhon Doe')]); var foo = JsonMapper.deserialize<List>(json); }



> If you are not actually importing those classes anywhere in your code, they will be omitted.

It seems that if importing a model through a 'barrel' file, the model will not be recognized as 'used'
k-paxian commented 3 years ago

Thank you!

It's obviously a bug.

Here https://github.com/k-paxian/dart-json-mapper/blob/master/mapper/lib/src/builder/library_visitor.dart#L17 is the place, imports only is taken into account. We need to handle exports as well 😄