diff --git a/lib/model/model/enum_model.dart b/lib/model/model/enum_model.dart index 0335534..cb0cff6 100644 --- a/lib/model/model/enum_model.dart +++ b/lib/model/model/enum_model.dart @@ -1,5 +1,6 @@ import 'package:model_generator/model/item_type/integer_type.dart'; import 'package:model_generator/model/item_type/item_type.dart'; +import 'package:model_generator/model/item_type/string_type.dart'; import 'package:model_generator/model/model/model.dart'; class EnumModel extends Model { @@ -56,5 +57,5 @@ class EnumProperty { EnumProperty({ required this.value, required this.name, - }) : type = IntegerType(); + }) : type = StringType(); } diff --git a/lib/writer/enum_model_writer.dart b/lib/writer/enum_model_writer.dart index 62d42e4..1341fcf 100644 --- a/lib/writer/enum_model_writer.dart +++ b/lib/writer/enum_model_writer.dart @@ -28,10 +28,11 @@ class EnumModelWriter { jsonModel.fields?.forEach((key) { final keyProperty = key.enumProperties.firstWhereOrNull((element) => element.name.toLowerCase() == jsonModel.keyProperty); final jsonValue = keyProperty?.value ?? key.serializedName; + final propertyType = keyProperty?.type; - if (keyProperty?.type is StringType) { + if (propertyType is StringType || propertyType == null) { sb.writeln(' @JsonValue(\'$jsonValue\')'); - } else if (keyProperty?.type is DoubleType) { + } else if (propertyType is DoubleType) { final doubleValue = double.tryParse(jsonValue); sb.writeln(' @JsonValue($doubleValue)'); } else { diff --git a/test/writer/enum_model_writer/normal/config.txt b/test/writer/enum_model_writer/normal/config.txt new file mode 100644 index 0000000..096d3af --- /dev/null +++ b/test/writer/enum_model_writer/normal/config.txt @@ -0,0 +1,8 @@ +MyEnumModel: + path: test/enum/ + type: enum + properties: + MY_VALUE_1: + value: '1' + MY_VALUE_2: + value: '2' diff --git a/test/writer/enum_model_writer/normal/output.txt b/test/writer/enum_model_writer/normal/output.txt index 526caef..0b50f98 100644 --- a/test/writer/enum_model_writer/normal/output.txt +++ b/test/writer/enum_model_writer/normal/output.txt @@ -3,7 +3,6 @@ import 'package:json_annotation/json_annotation.dart'; enum MyEnumModel { - ///A good description of this field @JsonValue('MY_VALUE_1') MY_VALUE_1, @JsonValue('MY_VALUE_2') diff --git a/test/writer/enum_model_writer_test.dart b/test/writer/enum_model_writer_test.dart index a619c9a..c6d0ca1 100644 --- a/test/writer/enum_model_writer_test.dart +++ b/test/writer/enum_model_writer_test.dart @@ -8,7 +8,7 @@ import 'writer_helper.dart'; void main() { void testEnumModelWriter(String path) { - final result = WriterHelper.prepareYmlConfig( + final result = WriterHelper.prepareWriterTest( path: path, pubspecPath: 'test/writer/enum_model_writer', ); diff --git a/test/writer/object_model_writer_test.dart b/test/writer/object_model_writer_test.dart index 6d5bce8..c8bae49 100644 --- a/test/writer/object_model_writer_test.dart +++ b/test/writer/object_model_writer_test.dart @@ -8,7 +8,7 @@ import 'writer_helper.dart'; void main() { void testObjectModelWriter(String path) { - final result = WriterHelper.prepareYmlConfig(path: path); + final result = WriterHelper.prepareWriterTest(path: path); final jsonModel = result.config.models.first; if (jsonModel is! ObjectModel) { throw Exception('The first model in the config file must be an object model and will be validated. The model is ${jsonModel.runtimeType}'); diff --git a/test/writer/writer_helper.dart b/test/writer/writer_helper.dart index 0541f3c..a50abb0 100644 --- a/test/writer/writer_helper.dart +++ b/test/writer/writer_helper.dart @@ -10,7 +10,7 @@ class WriterHelper { String expected, YmlGeneratorConfig config, PubspecConfig pubspecConfig, - }) prepareYmlConfig({ + }) prepareWriterTest({ required String path, String? pubspecPath, }) {