Skip to content

Commit

Permalink
Add RawJObject getter to StripeEntity (#1970)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Mar 31, 2020
1 parent 341233c commit d836d2b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/Stripe.net/Entities/_base/StripeEntity.cs
Original file line number Diff line number Diff line change
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.
/// </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.
if (this.rawJObject == null)
{
if (this.StripeResponse == null)
{
return null;
}

this.rawJObject = JObject.Parse(this.StripeResponse.Content);
}

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
Original file line number Diff line number Diff line change
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

0 comments on commit d836d2b

Please sign in to comment.