Skip to content

Commit

Permalink
Fix/open api spec (#266)
Browse files Browse the repository at this point in the history
- Changed json serilizer from newtonsoft 
- Removed state from MobilePayPaymentDetails
  • Loading branch information
TTA777 authored Feb 27, 2024
1 parent 1a7000a commit 84844f2
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task<MobilePayPaymentDetails> InitiatePayment(MobilePayPaymentReque
Log.Information("Initiated Payment with MobilePay PaymentId {TransactionId} of {OrerAmount} Oerer kr.", response.PaymentId.ToString(), paymentRequest.Amount);

return new MobilePayPaymentDetails(paymentRequest.OrderId.ToString(), response.MobilePayAppRedirectUri,
response.PaymentId.ToString(), null);
response.PaymentId.ToString());
}
catch (ApiException<ErrorResponse> e)
{
Expand All @@ -64,7 +64,7 @@ public async Task<MobilePayPaymentDetails> GetPayment(Guid paymentId)
var response = await _paymentsApi.GetSinglePaymentAsync(paymentId, null);

return new MobilePayPaymentDetails(response.Reference, response.RedirectUri,
response.PaymentId.ToString(), response.State.ToString());
response.PaymentId.ToString());
}
catch (ApiException<ErrorResponse> e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

namespace CoffeeCard.Models.DataTransferObjects.v2.Purchase
{
/// <summary>
Expand All @@ -12,10 +9,6 @@ namespace CoffeeCard.Models.DataTransferObjects.v2.Purchase
/// "orderId": "f5cb3e0f-3b9b-4f50-8c4f-a7450f300a5c"
/// }
/// </example>
[KnownType(typeof(FreePurchasePaymentDetails))]
[JsonPolymorphic(TypeDiscriminatorPropertyName = "discriminator")]
[JsonDerivedType(typeof(FreePurchasePaymentDetails), typeDiscriminator: "FreePurchasePaymentDetails")]

public class FreePurchasePaymentDetails : PaymentDetails
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

namespace CoffeeCard.Models.DataTransferObjects.v2.Purchase
{
Expand All @@ -16,9 +13,6 @@ namespace CoffeeCard.Models.DataTransferObjects.v2.Purchase
/// "paymentId": "186d2b31-ff25-4414-9fd1-bfe9807fa8b7"
/// }
/// </example>
[KnownType(typeof(MobilePayPaymentDetails))]
[JsonPolymorphic(TypeDiscriminatorPropertyName = "discriminator")]
[JsonDerivedType(typeof(MobilePayPaymentDetails), typeDiscriminator: "MobilePayPaymentDetails")]
public class MobilePayPaymentDetails : PaymentDetails
{
/// <summary>
Expand All @@ -35,23 +29,15 @@ public class MobilePayPaymentDetails : PaymentDetails
[Required]
public string PaymentId { get; }

/// <summary>
/// MobilePay state
/// </summary>
/// <example>Initiated</example>
[JsonProperty(Required = Required.AllowNull)]
public string? State { get; }

/// <summary>
/// Creates a new instance of <see cref="MobilePayPaymentDetails"/>
/// </summary>
public MobilePayPaymentDetails(string orderId, string mobilePayAppRedirectUri, string paymentId, string? state)
public MobilePayPaymentDetails(string orderId, string mobilePayAppRedirectUri, string paymentId)
{
PaymentType = PaymentType.MobilePay;
OrderId = orderId;
MobilePayAppRedirectUri = mobilePayAppRedirectUri;
PaymentId = paymentId;
State = state;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

namespace CoffeeCard.Models.DataTransferObjects.v2.Purchase
{
[KnownType(typeof(MobilePayPaymentDetails))]
[KnownType(typeof(FreePurchasePaymentDetails))]
[JsonPolymorphic(TypeDiscriminatorPropertyName = "discriminator")]
[JsonDerivedType(typeof(PaymentDetails), typeDiscriminator: "PaymentDetails")]
[JsonDerivedType(typeof(MobilePayPaymentDetails), typeDiscriminator: "MobilePayPaymentDetails")]
[JsonDerivedType(typeof(FreePurchasePaymentDetails), typeDiscriminator: "FreePurchasePaymentDetails")]
public abstract class PaymentDetails
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public async Task InitiatePurchasePaymentTypeMobilePay()
var orderId = Guid.NewGuid().ToString();
var mpDeepLink = "mobilepay://merchant_payments?payment_id=186d2b31-ff25-4414-9fd1-bfe9807fa8b7";
mobilePayService.Setup(mps => mps.InitiatePayment(It.IsAny<MobilePayPaymentRequest>()))
.ReturnsAsync(new MobilePayPaymentDetails(orderId, mpDeepLink, mobilepayPaymentId, "Initiated"));
.ReturnsAsync(new MobilePayPaymentDetails(orderId, mpDeepLink, mobilepayPaymentId));

// Act
var result = await purchaseService.InitiatePurchase(request, user);
Expand Down
11 changes: 7 additions & 4 deletions coffeecard/CoffeeCard.WebApi/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using AspNetCore.Authentication.ApiKey;
using CoffeeCard.Common.Configuration;
Expand Down Expand Up @@ -103,11 +105,11 @@ public void ConfigureServices(IServiceCollection services)
options.Filters.Add(new ApiExceptionFilter());
options.Filters.Add(new ReadableBodyFilter());
})
// Setup Json Serializing
.AddNewtonsoftJson(options =>
.AddJsonOptions(options =>
{
options.SerializerSettings.Converters.Add(new StringEnumConverter());
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.Never;
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});

services.AddCors(options => options.AddDefaultPolicy(builder =>
Expand Down Expand Up @@ -198,6 +200,7 @@ private static void GenerateOpenApiDocument(IServiceCollection services)
// Add an OpenApi document per API version
services.AddOpenApiDocument(config =>
{
config.DefaultResponseReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull;
config.Title = apiVersion.GroupName;
config.Version = apiVersion.ApiVersion.ToString();
config.DocumentName = apiVersion.GroupName;
Expand Down

0 comments on commit 84844f2

Please sign in to comment.