Skip to content
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

System.Text.Json Serialization Support in .NET SDK Objects #3033

Merged
merged 37 commits into from
Dec 12, 2024

Conversation

jar-stripe
Copy link
Contributor

@jar-stripe jar-stripe commented Nov 27, 2024

Why?

Users reported in #2495 and #1979 that Stripe.net objects did not behave as expected when serialized using System.Text.Json. System.Text.Json has become the standard for modern .NET, and the Stripe.net SDK objects should support serializing using it.

What?

  • Added System.Text.Json attributes for each property in the Stripe Entity and Options types
  • Added Wholesome tests to verify that each type has the correct properties
  • Implemented System.Text.Json converters for AnyOf, ExpandableField, StripeObject, UnixDateTime, and V2.Event
  • Implemented System.Text.Json custom converter for IEnumerable (for StripeList and StripeSearchResult)
  • Updated readme to describe JSON serialization, its usage, and any limitations.

Changelog

  • Add System.Text.Json support for serializing Stripe.net entities and objects for applications running on .NET 6 and above. Now you can pass a Stripe.net object or collection of objects to the System.Text.Json serializer and it will produce the correct JSON string.

modified PropertiesHaveJsonAttributes to check for equivalent STJ attributes
…son attributes

added json converters for system text json to ensure we conform to our API and existing SDK behavior
migrated manually managed files
updated wholesome tests for better reporting
added NewtonsoftAndSystemTextJsonOutputTheSameObject wholesome test, to ensure all stripe entities serialize the same through json.net and system text json

added STJEnumerableObjectConverter because STJ serializes IEnumerable implementations as arrays and not objects
added test to verify that enumerable item types have the proper STJ converters
updated wholesome tests
@jar-stripe jar-stripe changed the title Jar/coexist system text json System.Text.Json Serialization Support in .NET SDK Objects Nov 27, 2024
@jar-stripe jar-stripe marked this pull request as ready for review November 27, 2024 00:09
@jar-stripe jar-stripe requested review from pakrym-stripe, a team, xavdid-stripe and prathmesh-stripe and removed request for a team and xavdid-stripe November 27, 2024 00:10
@jar-stripe
Copy link
Contributor Author

The build failure is not expected, and appears to be due to a significant difference between .NET 6 and .NET 8.

…nt JsonObject(MemberSerialization.OptIn)

functionality in System Text Json. This is needed because JsonInclude does not work the same in .NET 6 and .NET 7
as it does in .NET 8
	added SerializablePropertyCache and other supporting classes
	converted several converters to converter factories, for type safety
	updated comments

updated generated code
…nverter

added wholesome test for member serialization opt in converter annotation
updated WholesomeTest GetProperties to use more accurate selector
@jar-stripe jar-stripe changed the base branch from master to beta December 5, 2024 20:20
…events are properly serializes

refactored custom serialization to pull out common reusable logic

updated wholesome tests to verify lists of V2 event subclasses and the custom converter

updated no attributes needed attribute string with better descriptions

removed GetRawText calls; we can use the JsonElement directly
@jar-stripe jar-stripe requested review from helenye-stripe and removed request for prathmesh-stripe December 11, 2024 22:41
Copy link
Contributor

@helenye-stripe helenye-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed tests, looks awesome 🚀

@jar-stripe jar-stripe enabled auto-merge (squash) December 12, 2024 22:03
@jar-stripe
Copy link
Contributor Author

Compat failures are expected and ok, and appear to be because of this change: https://github.com/stripe/sdk-codegen/pull/1954

@jar-stripe jar-stripe merged commit ba42f71 into beta Dec 12, 2024
3 of 4 checks passed
@jar-stripe jar-stripe deleted the jar/coexist-system-text-json branch December 12, 2024 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants