Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Adyen/SessionAuthentication/Client/ClientUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public static bool TryDeserialize<T>(ref Utf8JsonReader reader, JsonSerializerOp
if (obj is bool boolean)
return boolean ? "true" : "false";
if (obj is Models.ProductType productType)
return ProductTypeValueConverter.ToJsonValue(productType);
return ProductType.ToJsonValue(productType);
if (obj is Models.ResourceType resourceType)
return ResourceTypeValueConverter.ToJsonValue(resourceType);
return ResourceType.ToJsonValue(resourceType);
if (obj is ICollection collection)
{
List<string?> entries = new();
Expand Down
6 changes: 2 additions & 4 deletions Adyen/SessionAuthentication/Client/HostConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@ public HostConfiguration(IServiceCollection services)
_jsonOptions.Converters.Add(new LegalEntityResourceJsonConverter());
_jsonOptions.Converters.Add(new PaymentInstrumentResourceJsonConverter());
_jsonOptions.Converters.Add(new PolicyJsonConverter());
_jsonOptions.Converters.Add(new ProductTypeJsonConverter());
_jsonOptions.Converters.Add(new ProductTypeNullableJsonConverter());
_jsonOptions.Converters.Add(new ProductType.ProductTypeJsonConverter());
_jsonOptions.Converters.Add(new ResourceJsonConverter());
_jsonOptions.Converters.Add(new ResourceTypeJsonConverter());
_jsonOptions.Converters.Add(new ResourceTypeNullableJsonConverter());
_jsonOptions.Converters.Add(new ResourceType.ResourceTypeJsonConverter());
JsonSerializerOptionsProvider jsonSerializerOptionsProvider = new(_jsonOptions);
_services.AddSingleton(jsonSerializerOptionsProvider);
}
Expand Down
5 changes: 2 additions & 3 deletions Adyen/SessionAuthentication/Models/AccountHolderResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ public override AccountHolderResource Read(ref Utf8JsonReader utf8JsonReader, Ty
break;
case "type":
string? typeRawValue = utf8JsonReader.GetString();
if (typeRawValue != null)
type = new Option<ResourceType?>(ResourceTypeValueConverter.FromStringOrDefault(typeRawValue));
type = new Option<ResourceType?>(ResourceType.FromStringOrDefault(typeRawValue) ?? (ResourceType)typeRawValue);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The variable type is assigned here but never used within the Read method. This appears to be dead code.

break;
default:
break;
Expand Down Expand Up @@ -158,7 +157,7 @@ public void WriteProperties(Utf8JsonWriter writer, AccountHolderResource account
writer.WriteString("accountHolderId", accountHolderResource.AccountHolderId);

if (accountHolderResource.Type != null)
writer.WriteString("type", ResourceTypeValueConverter.ToJsonValue(accountHolderResource.Type));
writer.WriteString("type", ResourceType.ToJsonValue(accountHolderResource.Type));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ public override AuthenticationSessionRequest Read(ref Utf8JsonReader utf8JsonRea
break;
case "product":
string? productRawValue = utf8JsonReader.GetString();
if (productRawValue != null)
product = new Option<ProductType?>(ProductTypeValueConverter.FromStringOrDefault(productRawValue));
product = new Option<ProductType?>(ProductType.FromStringOrDefault(productRawValue) ?? (ProductType)productRawValue);
break;
default:
break;
Expand All @@ -149,7 +148,7 @@ public override AuthenticationSessionRequest Read(ref Utf8JsonReader utf8JsonRea
var authenticationSessionRequest = new AuthenticationSessionRequest();
authenticationSessionRequest.AllowOrigin = allowOrigin.Value!;
authenticationSessionRequest.Policy = policy.Value!;
authenticationSessionRequest.Product = product.Value!.Value;
authenticationSessionRequest.Product = product.Value!;
return authenticationSessionRequest;
}

Expand Down Expand Up @@ -184,7 +183,7 @@ public void WriteProperties(Utf8JsonWriter writer, AuthenticationSessionRequest

writer.WritePropertyName("policy");
JsonSerializer.Serialize(writer, authenticationSessionRequest.Policy, jsonSerializerOptions);
var productRawValue = ProductTypeValueConverter.ToJsonValue(authenticationSessionRequest.Product);
var productRawValue = ProductType.ToJsonValue(authenticationSessionRequest.Product);
writer.WriteString("product", productRawValue);
}
}
Expand Down
5 changes: 2 additions & 3 deletions Adyen/SessionAuthentication/Models/LegalEntityResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ public override LegalEntityResource Read(ref Utf8JsonReader utf8JsonReader, Type
break;
case "type":
string? typeRawValue = utf8JsonReader.GetString();
if (typeRawValue != null)
type = new Option<ResourceType?>(ResourceTypeValueConverter.FromStringOrDefault(typeRawValue));
type = new Option<ResourceType?>(ResourceType.FromStringOrDefault(typeRawValue) ?? (ResourceType)typeRawValue);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The variable type is assigned here but never used within the Read method. This appears to be dead code.

break;
default:
break;
Expand Down Expand Up @@ -158,7 +157,7 @@ public void WriteProperties(Utf8JsonWriter writer, LegalEntityResource legalEnti
writer.WriteString("legalEntityId", legalEntityResource.LegalEntityId);

if (legalEntityResource.Type != null)
writer.WriteString("type", ResourceTypeValueConverter.ToJsonValue(legalEntityResource.Type));
writer.WriteString("type", ResourceType.ToJsonValue(legalEntityResource.Type));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public override PaymentInstrumentResource Read(ref Utf8JsonReader utf8JsonReader
break;
case "type":
string? typeRawValue = utf8JsonReader.GetString();
if (typeRawValue != null)
type = new Option<ResourceType?>(ResourceTypeValueConverter.FromStringOrDefault(typeRawValue));
type = new Option<ResourceType?>(ResourceType.FromStringOrDefault(typeRawValue) ?? (ResourceType)typeRawValue);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The variable type is assigned here but never used within the Read method. This appears to be dead code.

break;
default:
break;
Expand Down Expand Up @@ -157,7 +156,7 @@ public void WriteProperties(Utf8JsonWriter writer, PaymentInstrumentResource pay
writer.WriteString("paymentInstrumentId", paymentInstrumentResource.PaymentInstrumentId);

if (paymentInstrumentResource.Type != null)
writer.WriteString("type", ResourceTypeValueConverter.ToJsonValue(paymentInstrumentResource.Type));
writer.WriteString("type", ResourceType.ToJsonValue(paymentInstrumentResource.Type));
}
}
}
188 changes: 76 additions & 112 deletions Adyen/SessionAuthentication/Models/ProductType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,162 +32,126 @@ namespace Adyen.SessionAuthentication.Models
/// <summary>
/// Defines ProductType
/// </summary>
public enum ProductType
[JsonConverter(typeof(ProductType.ProductTypeJsonConverter))]
public class ProductType : IEnum
{
/// <summary>
/// Enum Onboarding for value: onboarding
/// Returns the value of the ProductType.
/// </summary>
Onboarding = 1,
public string? Value { get; set; }

/// <summary>
/// Enum Platform for value: platform
/// ProductType.Onboarding - onboarding
/// </summary>
Platform = 2,
public static readonly ProductType Onboarding = new("onboarding");

/// <summary>
/// Enum Bank for value: bank
/// ProductType.Platform - platform
/// </summary>
Bank = 3
}
public static readonly ProductType Platform = new("platform");

/// <summary>
/// Converts <see cref="ProductType"/> to and from the JSON value
/// </summary>
public static class ProductTypeValueConverter
{
/// <summary>
/// Parses a given value to <see cref="ProductType"/>
/// ProductType.Bank - bank
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static ProductType FromString(string value)
{
if (value.Equals("onboarding"))
return ProductType.Onboarding;

if (value.Equals("platform"))
return ProductType.Platform;
public static readonly ProductType Bank = new("bank");

if (value.Equals("bank"))
return ProductType.Bank;

throw new NotImplementedException($"Could not convert value to type ProductType: '{value}'");
private ProductType(string? value)
{
Value = value;
}

/// <summary>
/// Parses a given value to <see cref="ProductType"/>
/// Converts a string to a <see cref="ProductType"/> implicitly.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static ProductType? FromStringOrDefault(string value)
{
if (value.Equals("onboarding"))
return ProductType.Onboarding;

if (value.Equals("platform"))
return ProductType.Platform;

if (value.Equals("bank"))
return ProductType.Bank;

return null;
}
public static implicit operator ProductType?(string? value) => value == null ? null : new ProductType(value);

/// <summary>
/// Converts the <see cref="ProductType"/> to the json value
/// Converts a <see cref="ProductType"/> instance to a string implicitly.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public static string ToJsonValue(ProductType value)
{
if (value == ProductType.Onboarding)
return "onboarding";

if (value == ProductType.Platform)
return "platform";

if (value == ProductType.Bank)
return "bank";
public static implicit operator string?(ProductType? option) => option?.Value;

throw new NotImplementedException($"Value could not be handled: '{value}'");
}
}
/// <summary>
/// Compares two <see cref="ProductType"/> instances for equality.
/// </summary>
public static bool operator ==(ProductType? left, ProductType? right) => string.Equals(left?.Value, right?.Value, StringComparison.OrdinalIgnoreCase);

/// <summary>
/// A Json converter for type <see cref="ProductType"/>
/// </summary>
/// <exception cref="NotImplementedException"></exception>
public class ProductTypeJsonConverter : JsonConverter<ProductType>
{
/// <summary>
/// Returns a from the Json object
/// Compares two <see cref="ProductType"/> instances for inequality.
/// </summary>
/// <param name="reader"></param>
/// <param name="typeToConvert"></param>
/// <param name="options"></param>
/// <returns></returns>
public override ProductType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string? rawValue = reader.GetString();
public static bool operator !=(ProductType? left, ProductType? right) => !string.Equals(left?.Value, right?.Value, StringComparison.OrdinalIgnoreCase);

ProductType? result = rawValue == null
? null
: ProductTypeValueConverter.FromStringOrDefault(rawValue);
/// <summary>
/// Returns true if the given object is equal to this <see cref="ProductType"/> instance.
/// </summary>
public override bool Equals(object? obj) => obj is ProductType other && string.Equals(Value, other.Value, StringComparison.OrdinalIgnoreCase);

if (result != null)
return result.Value;
/// <summary>
/// Returns a hash code for this <see cref="ProductType"/> instance.
/// </summary>
public override int GetHashCode() => Value?.GetHashCode() ?? 0;

throw new JsonException();
}
/// <summary>
/// Returns the string value of the <see cref="ProductType"/> instance.
/// </summary>
public override string ToString() => Value ?? string.Empty;

/// <summary>
/// Writes the ProductType to the json writer
/// Returns a <see cref="ProductType?"/>, or null if the value is not recognized.
/// </summary>
/// <param name="writer"></param>
/// <param name="productType"></param>
/// <param name="options"></param>
public override void Write(Utf8JsonWriter writer, ProductType productType, JsonSerializerOptions options)
public static ProductType? FromStringOrDefault(string? value)
{
writer.WriteStringValue(ProductTypeValueConverter.ToJsonValue(productType).ToString());
return value switch {
"onboarding" => ProductType.Onboarding,
"platform" => ProductType.Platform,
"bank" => ProductType.Bank,
_ => null,
};
}
}

/// <summary>
/// A Json converter for type <see cref="ProductType"/>
/// </summary>
public class ProductTypeNullableJsonConverter : JsonConverter<ProductType?>
{
/// <summary>
/// Returns a ProductType from the Json object
/// Converts the <see cref="ProductType"/> to the json value.
/// Returns the raw string value, preserving unknown values for round-trip safety.
/// </summary>
/// <param name="reader"></param>
/// <param name="typeToConvert"></param>
/// <param name="options"></param>
/// <returns></returns>
public override ProductType? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public static string? ToJsonValue(ProductType? value)
{
string? rawValue = reader.GetString();
if (value == null)
return null;

if (value == ProductType.Onboarding)
return "onboarding";

ProductType? result = rawValue == null
? null
: ProductTypeValueConverter.FromStringOrDefault(rawValue);
if (value == ProductType.Platform)
return "platform";

if (result != null)
return result.Value;
if (value == ProductType.Bank)
return "bank";

throw new JsonException();
return value.Value;
}

/// <summary>
/// Writes the ProductType to the json writer
/// JsonConverter for <see cref="ProductType"/>.
/// Preserves unknown values instead of throwing an exception.
/// </summary>
/// <param name="writer"></param>
/// <param name="productType"></param>
/// <param name="options"></param>
public override void Write(Utf8JsonWriter writer, ProductType? productType, JsonSerializerOptions options)
public class ProductTypeJsonConverter : JsonConverter<ProductType>
{
writer.WriteStringValue(productType.HasValue ? ProductTypeValueConverter.ToJsonValue(productType.Value).ToString() : "null");
/// <summary>
/// Deserializes a <see cref="ProductType"/> from JSON.
/// Unknown values are preserved as-is rather than throwing an exception.
/// </summary>
public override ProductType? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string? rawValue = reader.GetString();
return rawValue == null ? null : ProductType.FromStringOrDefault(rawValue) ?? new ProductType(rawValue);
}

/// <summary>
/// Serializes a <see cref="ProductType"/> to JSON.
/// </summary>
public override void Write(Utf8JsonWriter writer, ProductType value, JsonSerializerOptions options)
{
writer.WriteStringValue(ProductType.ToJsonValue(value));
}
}
}
}
7 changes: 3 additions & 4 deletions Adyen/SessionAuthentication/Models/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public partial class Resource
/// </summary>
public Resource()
{
Type = (ResourceType)Enum.Parse(typeof(ResourceType), this.GetType().Name);
Type = (ResourceType)this.GetType().Name;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The initialization of Type using this.GetType().Name sets the value to the class name (e.g., "AccountHolderResource"). However, the static constants in ResourceType use different values (e.g., "accountHolder"). Even with the case-insensitive equality operator defined in ResourceType, "AccountHolderResource" will not match "accountHolder". This mismatch breaks logic that compares resource.Type with these constants and may lead to incorrect serialization if the API expects the enum values.

OnCreated();
}

Expand Down Expand Up @@ -117,8 +117,7 @@ public override Resource Read(ref Utf8JsonReader utf8JsonReader, Type typeToConv
{
case "type":
string? typeRawValue = utf8JsonReader.GetString();
if (typeRawValue != null)
type = new Option<ResourceType?>(ResourceTypeValueConverter.FromStringOrDefault(typeRawValue));
type = new Option<ResourceType?>(ResourceType.FromStringOrDefault(typeRawValue) ?? (ResourceType)typeRawValue);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The variable type is assigned here but never used within the Read method. This appears to be dead code and can be removed to improve efficiency.

break;
default:
break;
Expand Down Expand Up @@ -172,7 +171,7 @@ public void WriteProperties(Utf8JsonWriter writer, Resource resource, JsonSerial
{

if (resource.Type != null)
writer.WriteString("type", ResourceTypeValueConverter.ToJsonValue(resource.Type));
writer.WriteString("type", ResourceType.ToJsonValue(resource.Type));
}
}
}
Loading
Loading