diff --git a/CHANGELOG.md b/CHANGELOG.md index 115567d0f..b8d330783 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ - Add ability to compare an EasyPost object to a parameter set object via `Matches` function - Users can define a custom `Matches` function on any parameter set class -- Fix parameter set serialization that was causing GetNextPage functions to malfunction when iterating through more than two pages +- Fix parameter set serialization that was causing issues when altering and reusing parameter sets ## v5.5.0 (2023-08-29) diff --git a/EasyPost.Tests/ParametersTests/ParametersTest.cs b/EasyPost.Tests/ParametersTests/ParametersTest.cs index 0f47c9282..8ee9ea065 100644 --- a/EasyPost.Tests/ParametersTests/ParametersTest.cs +++ b/EasyPost.Tests/ParametersTests/ParametersTest.cs @@ -108,6 +108,43 @@ public void TestParametersToDictionaryWithSubDictionary() Assert.Equal(streetB, addressData["street1"]); } + /// + /// This test proves that you can reuse a parameter object, and that re-serializing it will take into account any changes made to its properties since the last serialization. + /// + [Fact] + [Testing.Logic] + public void TestReusingParameterSets() + { + var parameters = new Parameters.Shipment.All + { + BeforeId = null, + }; + + // null values should not be serialized + var parametersDictionary = parameters.ToDictionary(); + Assert.False(parametersDictionary.ContainsKey("before_id")); + + parameters.BeforeId = "1"; + + // now that the property has a value, it should be serialized + parametersDictionary = parameters.ToDictionary(); + Assert.True(parametersDictionary.ContainsKey("before_id")); + Assert.Equal("1", parametersDictionary["before_id"]); + + parameters.BeforeId = "2"; + + // the new value should be serialized + parametersDictionary = parameters.ToDictionary(); + Assert.True(parametersDictionary.ContainsKey("before_id")); + Assert.Equal("2", parametersDictionary["before_id"]); + + parameters.BeforeId = null; + + // null values should not be serialized + parametersDictionary = parameters.ToDictionary(); + Assert.False(parametersDictionary.ContainsKey("before_id")); + } + [Fact] [Testing.Exception] public void TestRequiredAndOptionalParameterValidation() diff --git a/EasyPost/Parameters/BaseParameters.cs b/EasyPost/Parameters/BaseParameters.cs index 36892740b..a3e09b5d2 100644 --- a/EasyPost/Parameters/BaseParameters.cs +++ b/EasyPost/Parameters/BaseParameters.cs @@ -58,6 +58,8 @@ public virtual Dictionary ToDictionary() // Bad stuff could happen if we allow end-users to convert a parameter object to a dictionary themselves and try to use it in the normal functions // In particular, a lot of the normal functions do additional wrapping of their dictionaries, which would result in invalid JSON schemas being sent to the API + _parameterDictionary = new Dictionary(); + // Construct the dictionary of all parameters PropertyInfo[] properties = GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | @@ -106,6 +108,8 @@ public virtual Dictionary ToDictionary() /// of parameters. public virtual Dictionary ToSubDictionary(Type parentParameterObjectType) { + _parameterDictionary = new Dictionary(); + // Construct the dictionary of all parameters PropertyInfo[] properties = GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic |