Skip to content

Commit

Permalink
Enable test for JSON<->property name consistency (#1804)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Oct 8, 2019
1 parent 41bb157 commit 24988c9
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
17 changes: 17 additions & 0 deletions src/Stripe.net/Infrastructure/AllowNameMismatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Stripe.Infrastructure
{
using Newtonsoft.Json;

/// <summary>
/// Used to indicate that a particular property is allowed to have a name that isn't a strict
/// <c>snake_case</c> -> <c>CamelCase</c> conversion from the JSON name defined its
/// <see cref="JsonPropertyAttribute" /> property.
/// </summary>
/// <remarks>
/// This is only used for an internal wholesome test.
/// </remarks>
[System.AttributeUsage(System.AttributeTargets.Property)]
internal class AllowNameMismatch : System.Attribute
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Stripe
{
using System.Collections.Generic;
using Newtonsoft.Json;
using Stripe.Infrastructure;

public class InvoiceUpcomingInvoiceItemOption : INestedOptions, IHasMetadata
{
Expand Down Expand Up @@ -36,6 +37,7 @@ public class InvoiceUpcomingInvoiceItemOption : INestedOptions, IHasMetadata
/// Ids of the tax rates to apply to this invoice item.
/// </summary>
[JsonProperty("invoiceitem")]
[AllowNameMismatch]
public string InvoiceItem { get; set; }

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/Persons/PersonSharedOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Stripe
{
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Stripe.Infrastructure;
Expand Down Expand Up @@ -65,6 +64,7 @@ public abstract class PersonSharedOptions : BaseOptions, IHasMetadata
public PersonRelationshipOptions Relationship { get; set; }

[JsonProperty("ssn_last_4")]
[AllowNameMismatch]
public string SSNLast4 { get; set; }

[JsonProperty("verification")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
namespace Stripe
{
using System;
using Newtonsoft.Json;
using Stripe.Infrastructure;

public class TaxRatePercentageRangeOptions : INestedOptions
{
[JsonProperty("gt")]
[AllowNameMismatch]
public decimal? GreaterThan { get; set; }

[JsonProperty("gte")]
[AllowNameMismatch]
public decimal? GreaterThanOrEqual { get; set; }

[JsonProperty("lt")]
[AllowNameMismatch]
public decimal? LessThan { get; set; }

[JsonProperty("lte")]
[AllowNameMismatch]
public decimal? LessThanOrEqual { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Stripe
{
using Newtonsoft.Json;
using Stripe.Infrastructure;

public class ThreeDSecureCreateOptions : BaseOptions
{
Expand All @@ -17,6 +18,7 @@ public class ThreeDSecureCreateOptions : BaseOptions
/// If you pass a card id, you must also pass the customer id.
/// </summary>
[JsonProperty("card")]
[AllowNameMismatch]
public string CardTokenOrCardId { get; set; }

[JsonProperty("customer")]
Expand Down
4 changes: 4 additions & 0 deletions src/Stripe.net/Services/_common/DateRangeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ public class DateRangeOptions : INestedOptions
{
[JsonProperty("gt")]
[JsonConverter(typeof(DateTimeConverter))]
[AllowNameMismatch]
public DateTime? GreaterThan { get; set; }

[JsonProperty("gte")]
[JsonConverter(typeof(DateTimeConverter))]
[AllowNameMismatch]
public DateTime? GreaterThanOrEqual { get; set; }

[JsonProperty("lt")]
[JsonConverter(typeof(DateTimeConverter))]
[AllowNameMismatch]
public DateTime? LessThan { get; set; }

[JsonProperty("lte")]
[JsonConverter(typeof(DateTimeConverter))]
[AllowNameMismatch]
public DateTime? LessThanOrEqual { get; set; }
}
}
21 changes: 16 additions & 5 deletions src/StripeTests/Wholesome/JsonNamesMatchPropertyNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace StripeTests
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Stripe;
using Stripe.Infrastructure;
using Xunit;

/// <summary>
Expand All @@ -20,7 +21,7 @@ public class JsonNamesMatchPropertyNames : WholesomeTest
private const string AssertionMessage =
"Found at least one JsonProperty name mismatched with its property name.";

[Fact(Skip="Not all properties are compliant yet")]
[Fact]
public void Check()
{
List<string> results = new List<string>();
Expand All @@ -35,18 +36,28 @@ public void Check()
var propType = property.PropertyType;

// Skip properties that don't have a `JsonProperty` attribute
var attribute = property.GetCustomAttribute<JsonPropertyAttribute>();
if (attribute == null)
var jsonPropertyAttribute = property.GetCustomAttribute<JsonPropertyAttribute>();
if (jsonPropertyAttribute == null)
{
continue;
}

var snakeCasedPropName = Stripe.Infrastructure.StringUtils.ToSnakeCase(property.Name);

if (attribute.PropertyName != snakeCasedPropName)
// Skip properties when the property name matches the JSON name
if (jsonPropertyAttribute.PropertyName == snakeCasedPropName)
{
results.Add($"{stripeClass.Name}.{property.Name}: {attribute.PropertyName} != {snakeCasedPropName}");
continue;
}

// Skip properties that have a `AllowNameMismatch` attribute
var allowMismatchAttribute = property.GetCustomAttribute<AllowNameMismatch>();
if (allowMismatchAttribute != null)
{
continue;
}

results.Add($"{stripeClass.Name}.{property.Name}: {jsonPropertyAttribute.PropertyName} != {snakeCasedPropName}");
}
}

Expand Down

0 comments on commit 24988c9

Please sign in to comment.