Skip to content

Commit

Permalink
- Add additional unit test confirming parameter set reuse/re-serializ…
Browse files Browse the repository at this point in the history
…ation accounts for alterations
  • Loading branch information
nwithan8 committed Aug 31, 2023
1 parent 2852358 commit aedcf78
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
37 changes: 37 additions & 0 deletions EasyPost.Tests/ParametersTests/ParametersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,43 @@ public void TestParametersToDictionaryWithSubDictionary()
Assert.Equal(streetB, addressData["street1"]);
}

/// <summary>
/// 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.
/// </summary>
[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()
Expand Down
4 changes: 4 additions & 0 deletions EasyPost/Parameters/BaseParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public virtual Dictionary<string, object> 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<string, object?>();

// Construct the dictionary of all parameters
PropertyInfo[] properties = GetType().GetProperties(BindingFlags.Instance |
BindingFlags.NonPublic |
Expand Down Expand Up @@ -106,6 +108,8 @@ public virtual Dictionary<string, object> ToDictionary()
/// <returns><see cref="Dictionary{TKey,TValue}" /> of parameters.</returns>
public virtual Dictionary<string, object> ToSubDictionary(Type parentParameterObjectType)
{
_parameterDictionary = new Dictionary<string, object?>();

// Construct the dictionary of all parameters
PropertyInfo[] properties = GetType().GetProperties(BindingFlags.Instance |
BindingFlags.NonPublic |
Expand Down

0 comments on commit aedcf78

Please sign in to comment.