Skip to content

Commit

Permalink
fix: issue with JSON conversion of packagings data (openfoodfacts#762)
Browse files Browse the repository at this point in the history
* fix: issue with JSON conversion of packagings data

Fixes openfoodfacts#752

* refactor: address PR openfoodfacts#762 feedback

* fix: address more PR openfoodfacts#762 feedback
  • Loading branch information
peterwvj authored Jul 19, 2023
1 parent acc1fce commit 01c1d7b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
6 changes: 5 additions & 1 deletion lib/src/model/product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProductPackaging>? packagings;

/// Is the "packagings" complete?
Expand Down
7 changes: 4 additions & 3 deletions lib/src/model/product.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Product _$ProductFromJson(Map<String, dynamic> json) => Product(
.toList(),
stores: json['stores'] as String?,
attributeGroups: (json['attribute_groups'] as List<dynamic>?)
?.map((e) => AttributeGroup.fromJson(e))
?.map(AttributeGroup.fromJson)
.toList(),
lastModified: JsonHelper.timestampToDate(json['last_modified_t']),
ecoscoreGrade: json['ecoscore_grade'] as String?,
Expand All @@ -116,7 +116,7 @@ Product _$ProductFromJson(Map<String, dynamic> json) => Product(
..nutritionData = JsonHelper.checkboxFromJSON(json['nutrition_data'])
..comparedToCategory = json['compared_to_category'] as String?
..packagings = (json['packagings'] as List<dynamic>?)
?.map((e) => ProductPackaging.fromJson(e))
?.map(ProductPackaging.fromJson)
.toList()
..packagingsComplete =
JsonHelper.boolFromJSON(json['packagings_complete'])
Expand Down Expand Up @@ -224,7 +224,8 @@ Map<String, dynamic> _$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);
Expand Down
19 changes: 18 additions & 1 deletion lib/src/utils/json_helper.dart
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -268,6 +269,22 @@ class JsonHelper {
return result;
}

/// Returns a JSON map from [ProductPackaging]s
static List<Map<String, dynamic>>? productPackagingsToJson(
List<ProductPackaging>? packagings) {
if (packagings == null || packagings.isEmpty) {
return null;
}

List<Map<String, dynamic>> result = [];

for (ProductPackaging p in packagings) {
result.add(p.toJson());
}

return result;
}

/// Returns a JSON map from [AttributeGroup]s
static List<Map<String, dynamic>>? attributeGroupsToJson(
List<AttributeGroup>? list) {
Expand Down
8 changes: 4 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions test/issue752_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
}

0 comments on commit 01c1d7b

Please sign in to comment.