From 01c1d7b29da50dbaf17a0a375a54c34e7eee30a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20W=2E=20V=2E=20Tran-J=C3=B8rgensen?= Date: Wed, 19 Jul 2023 08:09:41 +0200 Subject: [PATCH] fix: issue with JSON conversion of packagings data (#762) * fix: issue with JSON conversion of packagings data Fixes #752 * refactor: address PR #762 feedback * fix: address more PR #762 feedback --- lib/src/model/product.dart | 6 +++++- lib/src/model/product.g.dart | 7 ++++--- lib/src/utils/json_helper.dart | 19 ++++++++++++++++++- pubspec.yaml | 8 ++++---- test/issue752_test.dart | 18 ++++++++++++++++++ 5 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 test/issue752_test.dart diff --git a/lib/src/model/product.dart b/lib/src/model/product.dart index ce2a50ab5f..f841e894cf 100644 --- a/lib/src/model/product.dart +++ b/lib/src/model/product.dart @@ -319,7 +319,11 @@ class Product extends JsonObject { @JsonKey(name: 'packaging', includeIfNull: false) String? packaging; - @JsonKey(name: 'packagings', includeIfNull: false) + @JsonKey( + name: 'packagings', + includeIfNull: false, + toJson: JsonHelper.productPackagingsToJson, + ) List? packagings; /// Is the "packagings" complete? diff --git a/lib/src/model/product.g.dart b/lib/src/model/product.g.dart index 647482a677..53d66b0dc8 100644 --- a/lib/src/model/product.g.dart +++ b/lib/src/model/product.g.dart @@ -91,7 +91,7 @@ Product _$ProductFromJson(Map json) => Product( .toList(), stores: json['stores'] as String?, attributeGroups: (json['attribute_groups'] as List?) - ?.map((e) => AttributeGroup.fromJson(e)) + ?.map(AttributeGroup.fromJson) .toList(), lastModified: JsonHelper.timestampToDate(json['last_modified_t']), ecoscoreGrade: json['ecoscore_grade'] as String?, @@ -116,7 +116,7 @@ Product _$ProductFromJson(Map json) => Product( ..nutritionData = JsonHelper.checkboxFromJSON(json['nutrition_data']) ..comparedToCategory = json['compared_to_category'] as String? ..packagings = (json['packagings'] as List?) - ?.map((e) => ProductPackaging.fromJson(e)) + ?.map(ProductPackaging.fromJson) .toList() ..packagingsComplete = JsonHelper.boolFromJSON(json['packagings_complete']) @@ -224,7 +224,8 @@ Map _$ProductToJson(Product instance) { writeNotNull('labels_tags_in_languages', LanguageHelper.toJsonStringsListMap(instance.labelsTagsInLanguages)); writeNotNull('packaging', instance.packaging); - writeNotNull('packagings', instance.packagings); + writeNotNull( + 'packagings', JsonHelper.productPackagingsToJson(instance.packagings)); val['packagings_complete'] = JsonHelper.boolToJSON(instance.packagingsComplete); writeNotNull('packaging_tags', instance.packagingTags); diff --git a/lib/src/utils/json_helper.dart b/lib/src/utils/json_helper.dart index a80fc8a2a0..e45f8532cc 100644 --- a/lib/src/utils/json_helper.dart +++ b/lib/src/utils/json_helper.dart @@ -1,7 +1,8 @@ +import '../interface/json_object.dart'; import '../model/attribute_group.dart'; import '../model/ingredient.dart'; import '../model/product_image.dart'; -import '../interface/json_object.dart'; +import '../model/product_packaging.dart'; import '../utils/language_helper.dart'; /// Helper class around product field conversion to/from JSON @@ -268,6 +269,22 @@ class JsonHelper { return result; } + /// Returns a JSON map from [ProductPackaging]s + static List>? productPackagingsToJson( + List? packagings) { + if (packagings == null || packagings.isEmpty) { + return null; + } + + List> result = []; + + for (ProductPackaging p in packagings) { + result.add(p.toJson()); + } + + return result; + } + /// Returns a JSON map from [AttributeGroup]s static List>? attributeGroupsToJson( List? list) { diff --git a/pubspec.yaml b/pubspec.yaml index 98c5d34456..645dbab904 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,15 +8,15 @@ environment: sdk: '>=2.17.0 <3.0.0' dependencies: - json_annotation: ^4.7.0 + json_annotation: ^4.8.1 http: ^0.13.5 path: ^1.8.2 # 1.8.2 for flutter_test as of 2023-04-06 meta: ^1.8.0 dev_dependencies: - analyzer: ^4.7.0 - build_runner: ^2.3.0 - json_serializable: ^6.5.4 + analyzer: ^6.0.0 + build_runner: ^2.4.6 + json_serializable: ^6.7.1 lints: ^2.0.1 test: ^1.21.4 coverage: ^1.6.2 diff --git a/test/issue752_test.dart b/test/issue752_test.dart new file mode 100644 index 0000000000..49462782b1 --- /dev/null +++ b/test/issue752_test.dart @@ -0,0 +1,18 @@ +import 'package:openfoodfacts/openfoodfacts.dart'; +import 'package:test/test.dart'; + +void main() { + test('ProductPackaging bug', () { + final testProduct = Product(); + const productName = 'Orange'; + testProduct.productName = productName; + const expectedQuantity = '75cl'; + testProduct.packagings = [ + ProductPackaging()..quantityPerUnit = expectedQuantity + ]; + final productJson = testProduct.toJson(); + final productRestored = Product.fromJson(productJson); + expect(productRestored.productName, productName); + expect(productRestored.packagings!.first.quantityPerUnit, expectedQuantity); + }); +}