openfoodfacts / openfoodfacts-dart

Open Food Facts API Wrapper
https://pub.dev/packages/openfoodfacts
Apache License 2.0
158 stars 65 forks source link

Ingredients and allergens not working via saveProduct #682

Open M123-dev opened 1 year ago

M123-dev commented 1 year ago

Description

Reported on Slack: It's not possible to add ingredients or allergens but ingredientsText works.

Test to reproduce


import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:test/test.dart';

import 'test_constants.dart';

void main() {
  OpenFoodAPIConfiguration.globalQueryType = QueryType.TEST;

  group('$OpenFoodAPIClient add new products', () {
    String barcode_1 = '0048151623426123';

    test('save product test ingredients + allergens', () async {
      Product product = Product(
        barcode: barcode_1,
        productName: 'Test product',
        lang: OpenFoodFactsLanguage.ENGLISH,
        ingredientsText: 'this is the <ingredientsText>',
        ingredients: <Ingredient>[
          Ingredient(
            id: 'en:milk',
            text: "milk",
          ),
        ],
        allergens: Allergens(["en:milk"], ["milk"]),
      );

      Status status = await OpenFoodAPIClient.saveProduct(
        TestConstants.TEST_USER,
        product,
      );

      expect(status.status, 1);
      expect(status.statusVerbose, 'fields saved');

      ProductQueryConfiguration configurations = ProductQueryConfiguration(
        barcode_1,
        language: OpenFoodFactsLanguage.ENGLISH,
        fields: [ProductField.ALL],
        version: ProductQueryVersion.v3,
      );
      ProductResultV3 result = await OpenFoodAPIClient.getProductV3(
        configurations,
        user: TestConstants.TEST_USER,
      );

      print(result.product?.productName);
      print(result.product?.ingredients);
      print(result.product?.allergens?.names);
    });
  });
}

Output

Test product
[Ingredient(id=en:this-is-the,text=this is the,percentEstimate=100.0,bold=false)]
[]
monsieurtanuki commented 1 year ago

I assume that ingredients and allergens_tags are READ-ONLY, like images or nova_group which are either populated differently or computed from the other data. @stephanegigandet Is that so?

teolemon commented 1 year ago

That's indeed the case @monsieurtanuki

monsieurtanuki commented 1 year ago

@M123-dev Someone interested in doc should write this ;)

monsieurtanuki commented 1 year ago

cf. #291

M123-dev commented 1 year ago

Is there somewhere a doc about which can be written and what not, or is this listed in some server code

monsieurtanuki commented 1 year ago

I would test EACH product field anyway. Then ask the sages (I mean, not the plants) to confirm in order to understand and enrich the doc, like "this field is read-only, its content is modified if you update field X".

stephanegigandet commented 1 year ago

ingredients is read only (derived from ingredients_text), and allergens_tags is derived from allergens which is writable.