-
Notifications
You must be signed in to change notification settings - Fork 570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use IHasObject interface instead of dynamic for data.object attribute of event objects #1322
Use IHasObject interface instead of dynamic for data.object attribute of event objects #1322
Conversation
… of event objects
@@ -21,6 +21,7 @@ | |||
</ItemGroup> | |||
|
|||
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' "> | |||
<Reference Include="Microsoft.CSharp" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to add this because of the new test that uses the dynamic
attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm approving because it's definitely a lot cleaner but I have some feedback still.
1/ The name StripeObjectConverter
seems a bit too generic and not Event specific. I know if I were to look for the first time I'd assume it does the entire deserialization logic but it doesn't
2/ It is really easy for that class to become out of sync. Is there a way to add tests to avoid that?
3/ Could we use something like this in general to deserialize any object instead of relying on the type declared in the resource (for example on expansion or such) and detect bad situation (like when we have the wrong type/resource (I've done that a few times in go by mistake :( )
Sorry a bit of this is quite vague in general, that's why I'm approving anyway. I just know some of my feedback often trigger some new ideas/improvements for you.
The naming was deliberate -- this converter can deserialize any Stripe object based on the
I agree (though fwiw, all our other libraries have this particular pitfall). I think it might be possible to add a test that uses reflection to find all classes that implement
I see what you mean and I agree it would be nice to detect inconsistencies (e.g. if a property is declared as a |
@ob-stripe Thanks a lot. I think we can totally merge as is. I know how your 🧠 works and there'll be more PRs in a few days :) |
I like the direction these changes are headed! I share Remi's concern that it might be hard to keep that list in sync. It might be possible to add a static class initializer to each individual class that we expect to deserialize that registers the |
r? @remi-stripe
cc @stripe/api-libraries @fred-stripe
This PR changes the declaration of the
Object
property inEventData
to be anIHasObject
instead ofdynamic
, and adds a newStripeObjectConverter
that can instantiate any Stripe model class based on the value of theobject
attribute.Replaces #1129.
Some notes:
technically, not all objects can be found in events, so this could have been implemented by adding a new interface (
IEventDataObject
), tagging classes for resources that can be found in events with this interface, and adding a converter that only handles those resources. But this works just as well and means that the library won't have to be updated if we suddenly decide to add new event types for existing resources.I added a test for
PreviousAttributes
as well, mostly because I was curious how Newtonsoft.Json handled deserialization ofdynamic
properties. It's actually quite nifty: it gives you an object that lets you access all properties of the JSON via methods (.metadata
) or indexes (["foo"]
). Of course you don't have any type safety and have to cast manually, but we can't make any assumptions on the shape ofprevious_attributes
so that seems reasonable. (To be clear, this is already how the library works today -- I just added a test.)