diff --git a/packages/brick_offline_first_build/test/offline_first_generator/test_primitive_fields.dart b/packages/brick_offline_first_build/test/offline_first_generator/test_primitive_fields.dart index f56339fa..8f96ec00 100644 --- a/packages/brick_offline_first_build/test/offline_first_generator/test_primitive_fields.dart +++ b/packages/brick_offline_first_build/test/offline_first_generator/test_primitive_fields.dart @@ -175,7 +175,8 @@ Future> _$PrimitiveFieldsToSqlite(PrimitiveFields instance, 'nullable_set': instance.nullableSet == null ? null : jsonEncode(instance.nullableSet.toList()), - 'nullable_map': jsonEncode(instance.nullableMap ?? {}), + 'nullable_map': + instance.nullableMap != null ? jsonEncode(instance.nullableMap) : null, 'nullable_longer_camelized_variable': instance.nullableLongerCamelizedVariable, 'nullable_casing': instance.nullableCasing != null diff --git a/packages/brick_sqlite/CHANGELOG.md b/packages/brick_sqlite/CHANGELOG.md index 03601644..dd3c874d 100644 --- a/packages/brick_sqlite/CHANGELOG.md +++ b/packages/brick_sqlite/CHANGELOG.md @@ -1,5 +1,9 @@ ## Unreleased +## 3.2.2 + +- Fix: nullable maps are not cast to a default value on serialization (#531) + ## 3.2.1 - Remove `dart:io` dependency diff --git a/packages/brick_sqlite/pubspec.yaml b/packages/brick_sqlite/pubspec.yaml index 45465e01..12eb20f9 100644 --- a/packages/brick_sqlite/pubspec.yaml +++ b/packages/brick_sqlite/pubspec.yaml @@ -4,7 +4,7 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_sqlite issue_tracker: https://github.com/GetDutchie/brick/issues repository: https://github.com/GetDutchie/brick -version: 3.2.1 +version: 3.2.2 environment: sdk: ">=2.18.0 <4.0.0" diff --git a/packages/brick_sqlite_generators/CHANGELOG.md b/packages/brick_sqlite_generators/CHANGELOG.md index 68770898..7e828e7d 100644 --- a/packages/brick_sqlite_generators/CHANGELOG.md +++ b/packages/brick_sqlite_generators/CHANGELOG.md @@ -1,5 +1,9 @@ ## Unreleased +## 3.3.1 + +- Fix: nullable maps are not cast to a default value on serialization (#531) + ## 3.3.0 - Upgrade `brick_core` to `1.3.0` diff --git a/packages/brick_sqlite_generators/lib/src/sqlite_serialize.dart b/packages/brick_sqlite_generators/lib/src/sqlite_serialize.dart index a15fcd7e..309e49a9 100644 --- a/packages/brick_sqlite_generators/lib/src/sqlite_serialize.dart +++ b/packages/brick_sqlite_generators/lib/src/sqlite_serialize.dart @@ -205,8 +205,10 @@ class SqliteSerialize<_Model extends SqliteModel> extends SqliteSerdesGenerator< // Map } else if (checker.isMap) { - final nullableSuffix = checker.isNullable ? ' ?? {}' : ''; - return 'jsonEncode($fieldValue$nullableSuffix)'; + if (checker.isNullable) { + return '$fieldValue != null ? jsonEncode($fieldValue) : null'; + } + return 'jsonEncode($fieldValue)'; } else if (checker.toJsonMethod != null) { final nullableSuffix = checker.isNullable ? '!' : ''; final output = 'jsonEncode($fieldValue$nullableSuffix.toJson())'; diff --git a/packages/brick_sqlite_generators/pubspec.yaml b/packages/brick_sqlite_generators/pubspec.yaml index a6cdf249..4052ba91 100644 --- a/packages/brick_sqlite_generators/pubspec.yaml +++ b/packages/brick_sqlite_generators/pubspec.yaml @@ -4,7 +4,7 @@ homepage: https://github.com/GetDutchie/brick/tree/main/packages/brick_sqlite_ge issue_tracker: https://github.com/GetDutchie/brick/issues repository: https://github.com/GetDutchie/brick -version: 3.3.0+1 +version: 3.3.1 environment: sdk: ">=2.18.0 <4.0.0" diff --git a/packages/brick_sqlite_generators/test/sqlite_model_serdes_generator/test_all_field_types.dart b/packages/brick_sqlite_generators/test/sqlite_model_serdes_generator/test_all_field_types.dart index a8924c98..c418ba10 100644 --- a/packages/brick_sqlite_generators/test/sqlite_model_serdes_generator/test_all_field_types.dart +++ b/packages/brick_sqlite_generators/test/sqlite_model_serdes_generator/test_all_field_types.dart @@ -23,12 +23,16 @@ Future _$AllFieldTypesFromSqlite(Map data, .whereType() .toList() .cast(), + longerCamelizedVariable: data['longer_camelized_variable'] == null + ? null + : data['longer_camelized_variable'] as String?, + map: jsonDecode(data['map']), nullableList: data['nullable_list'] == null ? null : jsonDecode(data['nullable_list']).toList().cast(), - longerCamelizedVariable: data['longer_camelized_variable'] == null + nullableMap: data['nullable_map'] == null ? null - : data['longer_camelized_variable'] as String?, + : jsonDecode(data['nullable_map']), string: data['string'] == null ? null : data['string'] as String?, stringSet: jsonDecode(data['string_set']).toSet().cast()) ..primaryKey = data['_brick_id'] as int; @@ -46,10 +50,13 @@ Future> _$AllFieldTypesToSqlite(AllFieldTypes instance, : null, 'enum_list': jsonEncode( instance.enumList.map((s) => Casing.values.indexOf(s)).toList()), + 'longer_camelized_variable': instance.longerCamelizedVariable, + 'map': jsonEncode(instance.map), 'nullable_list': instance.nullableList == null ? null : jsonEncode(instance.nullableList), - 'longer_camelized_variable': instance.longerCamelizedVariable, + 'nullable_map': + instance.nullableMap != null ? jsonEncode(instance.nullableMap) : null, 'string': instance.string, 'string_set': jsonEncode(instance.stringSet.toList()) }; @@ -97,17 +104,29 @@ class AllFieldTypesAdapter extends SqliteAdapter { iterable: true, type: Casing, ), + 'longerCamelizedVariable': const RuntimeSqliteColumnDefinition( + association: false, + columnName: 'longer_camelized_variable', + iterable: false, + type: String, + ), + 'map': const RuntimeSqliteColumnDefinition( + association: false, + columnName: 'map', + iterable: false, + type: Map, + ), 'nullableList': const RuntimeSqliteColumnDefinition( association: false, columnName: 'nullable_list', iterable: true, type: int, ), - 'longerCamelizedVariable': const RuntimeSqliteColumnDefinition( + 'nullableMap': const RuntimeSqliteColumnDefinition( association: false, - columnName: 'longer_camelized_variable', + columnName: 'nullable_map', iterable: false, - type: String, + type: Map, ), 'string': const RuntimeSqliteColumnDefinition( association: false, @@ -154,8 +173,10 @@ class AllFieldTypes { this.dub, this.enumField, required this.enumList, - this.nullableList, this.longerCamelizedVariable, + this.nullableList, + this.nullableMap, + required this.map, this.string, required this.stringSet, }); @@ -165,8 +186,10 @@ class AllFieldTypes { final double? dub; final Casing? enumField; final List enumList; - final List? nullableList; final String? longerCamelizedVariable; + final Map map; + final List? nullableList; + final Map? nullableMap; final String? string; final Set stringSet; }