PrestaSharp icon indicating copy to clipboard operation
PrestaSharp copied to clipboard

Trying to add an entity throws CLR/System.InvalidOperationException

Open victorBuzdugan opened this issue 1 year ago • 2 comments

Prestashop version: 8.1.3 but I think it's unrelated to Prestashop version

Trying to add a manufacturer using either Add or AddAsync methods throws an exception at line 186 (using AddAsync) or at line 46 (using Add) in GenericFactory.cs:

Exception has occurred: CLR/System.InvalidOperationException An unhandled exception of type 'System.InvalidOperationException' occurred in System.Private.CoreLib.dll: 'Nullable object must have a value.'

In order to reproduce:

  • Clone the GitHub repo
  • Add a new console project to the solution
  • Add PrestaSharp ass a reference to the project
  • In Program.cs:
using Bukimedia.PrestaSharp.Entities;
using Bukimedia.PrestaSharp.Factories;

string url = "http://localhost/prestashop/api";
string account = "<api_key>";
string secretKey = "";
string manufacturerName = "TestManufacturer";
ManufacturerFactory manufacturerFactory = new ManufacturerFactory(url, account, secretKey);

// just to test that credentials are correct
List<manufacturer> manufacturers = manufacturerFactory.GetAll();

var newManufacturer = new manufacturer();
newManufacturer.name = manufacturerName;
newManufacturer.active = 1;
newManufacturer = manufacturerFactory.Add(newManufacturer);
// or
newManufacturer = await manufacturerFactory.AddAsync(newManufacturer);

Reverting back before Merge pull request https://github.com/Bukimedia/PrestaSharp/pull/469 fixes the issue:

git reset 0121cc0 --hard

Looking at the AddBody method (that was change in pull request 469) in RestSharpFactory.cs I can see that entities is not used and serialized is just an empty string:

private void AddBody(RestRequest request, IEnumerable<PrestaShopEntity> entities)
{
    request.RequestFormat = DataFormat.Xml;
    var serialized = string.Empty;
    serialized = "<prestashop>\n" + serialized + "\n</prestashop>";
    request.AddParameter("application/xml", serialized, ParameterType.RequestBody);
}

victorBuzdugan avatar Mar 04 '24 20:03 victorBuzdugan

Hi, any update of this

private void AddBody(RestRequest request, IEnumerable<PrestaShopEntity> entities) { request.RequestFormat = DataFormat.Xml; var serialized = string.Empty; serialized = "\n" + serialized + "\n"; request.AddParameter("application/xml", serialized, ParameterType.RequestBody); }

Thanks

sginfoocio avatar Jun 06 '24 09:06 sginfoocio