diff --git a/angel_serialize_generator/lib/build_context.dart b/angel_serialize_generator/lib/build_context.dart index 03b24132..01d25b25 100644 --- a/angel_serialize_generator/lib/build_context.dart +++ b/angel_serialize_generator/lib/build_context.dart @@ -34,7 +34,7 @@ Future buildContext( autoSnakeCaseNames = annotation.peek('autoSnakeCaseNames')?.boolValue ?? autoSnakeCaseNames; - var ctx = new BuildContext(annotation, + var ctx = new BuildContext(annotation, clazz, originalClassName: clazz.name, sourceFilename: p.basename(buildStep.inputId.path), autoIdAndDateFields: autoIdAndDateFields, diff --git a/angel_serialize_generator/lib/context.dart b/angel_serialize_generator/lib/context.dart index 7fd245a6..0debfd3c 100644 --- a/angel_serialize_generator/lib/context.dart +++ b/angel_serialize_generator/lib/context.dart @@ -32,10 +32,12 @@ class BuildContext { final ConstantReader annotation; + final ClassElement clazz; + /// The name of the field that identifies data of this model type. String primaryKeyName = 'id'; - BuildContext(this.annotation, + BuildContext(this.annotation, this.clazz, {this.originalClassName, this.sourceFilename, this.autoSnakeCaseNames, diff --git a/angel_serialize_generator/lib/model.dart b/angel_serialize_generator/lib/model.dart index 6bd676f9..d7446ab2 100644 --- a/angel_serialize_generator/lib/model.dart +++ b/angel_serialize_generator/lib/model.dart @@ -100,6 +100,10 @@ class JsonModelGenerator extends GeneratorForAnnotation { else { b.type = convertTypeReference(field.type); } + + if (ctx.requiredFields.containsKey(field.name)) { + b.annotations.add(new CodeExpression(new Code('required'))); + } })); } })); diff --git a/angel_serialize_generator/test/models/author.dart b/angel_serialize_generator/test/models/author.dart index 3e0cc868..ce4695a2 100644 --- a/angel_serialize_generator/test/models/author.dart +++ b/angel_serialize_generator/test/models/author.dart @@ -3,6 +3,7 @@ library angel_serialize.test.models.author; import 'package:angel_framework/common.dart'; import 'package:angel_serialize/angel_serialize.dart'; import 'package:collection/collection.dart'; +import 'package:meta/meta.dart'; import 'book.dart'; part 'author.g.dart'; @@ -11,9 +12,14 @@ part 'author.serializer.g.dart'; @serializable abstract class _Author extends Model { + @required String get name; + + @required int get age; + List get books; + Book get newestBook; @exclude @@ -34,7 +40,9 @@ abstract class _Bookmark extends Model { final Book book; List get history; + int get page; + String get comment; _Bookmark(this.book); diff --git a/angel_serialize_generator/test/models/author.g.dart b/angel_serialize_generator/test/models/author.g.dart index a14db13e..a40b4452 100644 --- a/angel_serialize_generator/test/models/author.g.dart +++ b/angel_serialize_generator/test/models/author.g.dart @@ -9,8 +9,8 @@ part of angel_serialize.test.models.author; class Author extends _Author { Author( {this.id, - this.name, - this.age, + @required this.name, + @required this.age, List books, this.newestBook, this.secret,