Bukimedia / PrestaSharp

CSharp .Net client library for the PrestaShop API via web service
GNU General Public License v3.0
155 stars 153 forks source link

Get all Products returns too much products with Prestashop 1.7.4.2 #348

Closed cbonnaure closed 5 years ago

cbonnaure commented 5 years ago

Library Version: 1.0.2

Prestashop version: 1.7.4.2

Describe the Bug: When using the ProductFactory to getAll Products, I get more Products than the Prestashop database

To Reproduce:

ProductFactory pf = new ProductFactory(PrestashopAPI_URL, PrestashopAPI_KEY, PrestashopAPI_PWD);
List<product> psProducts = pf.GetAll();

Expected Behavior: same products then the Prestashop database


From the xml result of the API request, into somme "product" node, there is an "accessories" node that contains "product" childs. So, when deserializing the document using root.Descendants(lowerName) , returned elements contains theses "product" subnodes.

<prestashop>
  <products>
    <product>
      <id><![CDATA[50]]></id>
      ...
        <accessories nodeType="product" api="products">
          <product>
            <id href="https://www.xxxxxx.com/api/product/62"><![CDATA[62]]></id>
          </product>
          <product>
            <id href="https://www.xxxxxx.com/api/product/137"><![CDATA[137]]></id>
          </product>
          <product>
            <id href="https://www.xxxxxx.com/api/product/138"><![CDATA[138]]></id>
          </product>
        </accessories>
      ...
    </product>
  </products>
</prestashop>

At this time, to correct this issue, I add an If block to get only childs that match name from the first root's child

--- Bukimedia.PrestaSharp.Deserializers ------ HandleListDerivative

            if (!elements.Any())
            {
                var lowerName = name.ToLowerInvariant().AsNamespaced(Namespace);
                var firstNode = root.FirstNode;
                if (firstNode != null)
                {
                    elements = ((XElement)firstNode).Elements(lowerName);
                }
            }
mowcixo commented 5 years ago

Hello @cbonnaure, what are the filters you're appliying? Is there any possibility that you're using a multishop environment, and you have same products in various shops?

cbonnaure commented 5 years ago

Hello @mowcixo, Thank's for your reply. I don't apply any filters. I don't use a multishop environment, so I don't have products in various shops.

The probleme is that I have subnodes "product" into main "product" nodes (cf XML example)

I updated my first comment to show the codes and XML (sorry, I didn't pay attention that it was hidden)

mowcixo commented 5 years ago

Ok @cbonnaure I see it now, I'll investigate and let you know soon. Thanks!

mowcixo commented 5 years ago

Thanks for the snippet @cbonnaure, it fixes getting order repeated messages too. Nex time try to make a PR! :)

It is changed in master now.

cbonnaure commented 5 years ago

Thanks for your reply and have taken into account my issue. Ok, next time I would do a PR

Thanks for this great project !

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.