Closed mckennajones closed 4 years ago
Thanks for the heads up! We'll follow up here once we've taken a look at what's causing this.
After taking a bit of time to look over this, it seems like the serializer has an issue with values that are enums, although I'm not particularly sure why. Just to double check, you're using JSON.net as your serializer?
Hey @Epreuve , yes we are using the default .NET Core serializer, which I am fairly sure is JSON.net.
I did see issue #60, but it appears that nothing was merged in as a result of it.
We had a similar issue, not with Braintree, but another project. .NET Core serializer is amazingly fast, but not quite right in some situations. Our solution was to use Newtonsoft JSON Serilizer instead. Read how to plug it into .Net Core here: https://dotnetcoretutorials.com/2019/12/19/using-newtonsoft-json-in-net-core-3-projects/
This is happening because the Braintree library defined its own Enumeration
type instead of using the .NET Enum
type. The .NET Enum
type implements IConvertible
and IFormattable
, while the Braintree Enumeration
type does not.
If the Braintree.Enumeration
type was updated to implement IConvertible
and IFormattable
, then most serializers would work as expected.
Alternatively, adding a public read-only string property (maybe called Value
) backed by the protected Enumeration.Name
field would also cause most serializers to work out of the box.
If you were starting from scratch, using the .NET Enum
type or making the Braintree Enumeration
type implement IConvertible
and IFormattable
would be ideal IMO.
However, to fix from where we are now, I would recommend adding the public read-only property to the Enumeration
type. Depreciating the Enumeration
type in favor of the .NET Enum
would be a major breaking change for obvious reasons. Implementing IConvertible
and IFormattable
could be a breaking change in a more subtle way, as you could break some downstream Logstash indexes.
As a work-around, anyone using Json.NET to serialize Braintree objects could use a custom JsonConverter like this:
public class EnumerationConverter : JsonConverter<Enumeration>
{
public override void WriteJson(JsonWriter writer, Enumeration value, JsonSerializer serializer)
{
writer.WriteRawValue($"\"{value}\""); // assuming all Braintree Enumeration strings are safe as a raw json value
}
public override Enumeration ReadJson(JsonReader reader, Type objectType, Enumeration existingValue, bool hasExistingValue,
JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
@AdamEssenmacher we're about to do a major version bump of the SDK, so now's the perfect time to make that change. (good timing on the comment!)
Are you interested in PR-ing it? no worries if not.
@crookedneighbor maybe... I suggested three possible approaches to support better serialization:
Braintree.Enumeration
types with .NET Enum typesBraintree.Enumeration
to implement IConvertible
and IFormattable
Value
property to Braintree.Enumeration
Which approach would you think is best?
@mckennajones @AdamEssenmacher We've replaced all Braintree.Enumeration
subclasses with C# enum types in version 5.0.0, which was just released. This should resolve the serialization issue.
General information
I'm noticing that if I return the Transaction object from one of my endpoints, the default .NET JSON serializer is serializing the status property it as an empty object, instead of the actual value.
Issue description
Returning the Braintree Transaction object from one of my .NET core endpoints, the Status property, as well as the Status properties in the Status History array are simply an empty objects. Here is an example: