Open FabrizioG202 opened 6 months ago
Thanks for this! It's correct that the generator currently only looks for an unnamed constructor. After a quick look, I don't see any reason on why it was restricted that way other than simplicity. So maybe the generator can support looking at all constructors. The docs also kind of imply this is how it should work (this is also how it works for Java).
No problem! So, the best solution would be the third one, as it would not require the addition of a new annotation class and would not modify current classes?
If that is the case I could open a pull request to fix this
@FabrizioG202 Yes, that's probably the best way to go.
You can submit a pull request for this! But note that depending on the magnitude of the changes we may require you to sign a contributors license agreement (CLA).
When I am creating Entities which have custom types, ObjectBox tries to use the default constructor or the first with parameter names matching the Entity's property names (https://docs.objectbox.io/entity-annotations#objectbox-database-persistence-with-entity-annotations).
If I understood correctly, this would enforce the creation of a custom constructor for use by the user while the default would be reserved for ObjectBox.
For example, the following does not build using the generator:
as ObjectBox is using the default Person constructor to deserialize the object.
Describe the solution you'd like
I though of four possible ways this could possibly be achieved:
constructor
field to theEntity
annotation. If I am not mistaken, this is the approach used by JsonSerializable (https://github.com/google/json_serializable.dart/blob/85e83641befab6a30526c24950ecfd3fe3f9ce62/json_annotation/lib/src/json_serializable.dart#L66)