Skip to content

Commit

Permalink
fix: issue with converting LocalizedTags to JSON (#766)
Browse files Browse the repository at this point in the history
* fix: issue with converting LocalizedTags to JSON

Fixes #765

* refactor: address PR #766 feedback
  • Loading branch information
peterwvj authored Jul 20, 2023
1 parent a1fc1d8 commit 80c3ddb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
3 changes: 3 additions & 0 deletions lib/src/model/localized_tag.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class LocalizedTag extends JsonObject {
factory LocalizedTag.fromJson(Map<String, dynamic> json) =>
_$LocalizedTagFromJson(json);

static Map<String, dynamic> objToJson(LocalizedTag? tag) =>
tag != null ? tag.toJson() : {};

@override
Map<String, dynamic> toJson() => _$LocalizedTagToJson(this);

Expand Down
15 changes: 12 additions & 3 deletions lib/src/model/product_packaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ part 'product_packaging.g.dart';
@JsonSerializable()
class ProductPackaging extends JsonObject {
/// Shape, canonicalized using [TaxonomyPackagingShape].
@JsonKey(includeIfNull: false)
@JsonKey(
includeIfNull: false,
toJson: LocalizedTag.objToJson,
)
LocalizedTag? shape;

/// Material, canonicalized using [TaxonomyPackagingMaterial].
@JsonKey(includeIfNull: false)
@JsonKey(
includeIfNull: false,
toJson: LocalizedTag.objToJson,
)
LocalizedTag? material;

/// Recycling status, canonicalized using [TaxonomyPackagingRecycling].
@JsonKey(includeIfNull: false)
@JsonKey(
includeIfNull: false,
toJson: LocalizedTag.objToJson,
)
LocalizedTag? recycling;

/// Number of units of this component contained in the product.
Expand Down
9 changes: 5 additions & 4 deletions lib/src/model/product_packaging.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions test/issue752_test.dart

This file was deleted.

20 changes: 20 additions & 0 deletions test/json_conversion_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:test/test.dart';

void main() {
test('ProductPackaging bug - covers issues #752 and #765', () {
const expectedProductName = 'Orange';
final testProduct = Product()..productName = expectedProductName;
const expectedQuantity = '75cl';
final packing = ProductPackaging()..quantityPerUnit = expectedQuantity;
const expectedId = '4';
packing.shape = LocalizedTag()..id = expectedId;
testProduct.packagings = [packing];
final productJson = testProduct.toJson();
final productRestored = Product.fromJson(productJson);
expect(productRestored.productName, expectedProductName);
final packingRestored = productRestored.packagings!.first;
expect(packingRestored.quantityPerUnit, expectedQuantity);
expect(packingRestored.shape!.id, expectedId);
});
}

0 comments on commit 80c3ddb

Please sign in to comment.