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

Add RawJObject getter to StripeEntity #1970

Merged
merged 1 commit into from Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/Stripe.net/Entities/_base/StripeEntity.cs
Expand Up @@ -4,11 +4,51 @@ namespace Stripe
using System.Reflection;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Stripe.Infrastructure;

[JsonObject(MemberSerialization.OptIn)]
public abstract class StripeEntity : IStripeEntity
{
[JsonIgnore]
private JObject rawJObject;

/// <summary>
/// Gets the raw <see cref="JObject">JObject</see> exposed by the Newtonsoft.Json library.
/// This can be used to access properties that are not directly exposed by Stripe's .NET
/// library.
/// </summary>
/// <remarks>
/// You should always prefer using the standard property accessors whenever possible. This
/// accessor is not considered fully stable and might change or be removed in future
/// versions.
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
/// </remarks>
/// <returns>The raw <see cref="JObject">JObject</see>.</returns>
[JsonIgnore]
public JObject RawJObject
{
get
{
// Lazily initialize the object the first time the getter is called.
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
if (this.rawJObject == null)
{
if (this.StripeResponse == null)
{
return null;
}

this.rawJObject = JObject.Parse(this.StripeResponse.Content);
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
}

return this.rawJObject;
}

protected set
{
this.rawJObject = value;
}
}

[JsonIgnore]
public StripeResponse StripeResponse { get; set; }

Expand Down
31 changes: 30 additions & 1 deletion src/StripeTests/Entities/_base/StripeEntityTest.cs
Expand Up @@ -5,8 +5,14 @@ namespace StripeTests
using Stripe.Infrastructure;
using Xunit;

public class StripeEntityTest
public class StripeEntityTest : BaseStripeTest
{
public StripeEntityTest(
StripeMockFixture stripeMockFixture)
: base(stripeMockFixture)
{
}

[Fact]
public void FromJsonAuto()
{
Expand Down Expand Up @@ -141,6 +147,29 @@ public void ExpandableAccessors_Id_DoesNotResetExistingExpandedObjectIfIdIsSame(
Assert.Equal(42, o.Nested.Bar);
}

[Fact]
public void RawJObject()
{
var service = new SubscriptionService(this.StripeClient);
var subscription = service.Get("sub_123");

Assert.NotNull(subscription);

// Access `id`, a string element
Assert.Equal(subscription.Id, subscription.RawJObject["id"]);

// Access `created`, a number element
Assert.Equal(subscription.Created, subscription.RawJObject["created"]);

// Access `plan[id]`, a nested string element
Assert.Equal(subscription.Plan.Id, subscription.RawJObject["plan"]["id"]);

// Access `items[data][0][id]`, a deeply nested string element
Assert.Equal(
subscription.Items.Data[0].Id,
subscription.RawJObject["items"]["data"][0]["id"]);
}

private class TestEntity : StripeEntity<TestEntity>
{
[JsonProperty("integer")]
Expand Down