From 84844f279521c469c6576420af43cecee15f49e3 Mon Sep 17 00:00:00 2001 From: Thomas Andersen <d6di6skcprs5@opayq.com> Date: Tue, 27 Feb 2024 18:15:03 +0100 Subject: [PATCH] Fix/open api spec (#266) - Changed json serilizer from newtonsoft - Removed state from MobilePayPaymentDetails --- .../Service/v2/MobilePayPaymentsService.cs | 4 ++-- .../v2/Purchase/FreeProductPaymentDetails.cs | 7 ------- .../v2/Purchase/MobilePayPaymentDetails.cs | 18 ++---------------- .../v2/Purchase/PaymentDetails.cs | 6 ++---- .../Services/v2/PurchaseServiceTests.cs | 2 +- coffeecard/CoffeeCard.WebApi/Startup.cs | 11 +++++++---- 6 files changed, 14 insertions(+), 34 deletions(-) diff --git a/coffeecard/CoffeeCard.MobilePay/Service/v2/MobilePayPaymentsService.cs b/coffeecard/CoffeeCard.MobilePay/Service/v2/MobilePayPaymentsService.cs index aaa98e0c..9d37d7a5 100644 --- a/coffeecard/CoffeeCard.MobilePay/Service/v2/MobilePayPaymentsService.cs +++ b/coffeecard/CoffeeCard.MobilePay/Service/v2/MobilePayPaymentsService.cs @@ -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) { @@ -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) { diff --git a/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/Purchase/FreeProductPaymentDetails.cs b/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/Purchase/FreeProductPaymentDetails.cs index 8fa95612..82039568 100644 --- a/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/Purchase/FreeProductPaymentDetails.cs +++ b/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/Purchase/FreeProductPaymentDetails.cs @@ -1,6 +1,3 @@ -using System.Runtime.Serialization; -using System.Text.Json.Serialization; - namespace CoffeeCard.Models.DataTransferObjects.v2.Purchase { /// <summary> @@ -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> diff --git a/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/Purchase/MobilePayPaymentDetails.cs b/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/Purchase/MobilePayPaymentDetails.cs index 765e7f77..5742561e 100644 --- a/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/Purchase/MobilePayPaymentDetails.cs +++ b/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/Purchase/MobilePayPaymentDetails.cs @@ -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 { @@ -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> @@ -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; } } -} \ No newline at end of file +} diff --git a/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/Purchase/PaymentDetails.cs b/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/Purchase/PaymentDetails.cs index 43bf7ad2..33d4ede6 100644 --- a/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/Purchase/PaymentDetails.cs +++ b/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/Purchase/PaymentDetails.cs @@ -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> diff --git a/coffeecard/CoffeeCard.Tests.Unit/Services/v2/PurchaseServiceTests.cs b/coffeecard/CoffeeCard.Tests.Unit/Services/v2/PurchaseServiceTests.cs index 614bc2f0..b52279fa 100644 --- a/coffeecard/CoffeeCard.Tests.Unit/Services/v2/PurchaseServiceTests.cs +++ b/coffeecard/CoffeeCard.Tests.Unit/Services/v2/PurchaseServiceTests.cs @@ -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); diff --git a/coffeecard/CoffeeCard.WebApi/Startup.cs b/coffeecard/CoffeeCard.WebApi/Startup.cs index ad1cffa8..6a960e6a 100644 --- a/coffeecard/CoffeeCard.WebApi/Startup.cs +++ b/coffeecard/CoffeeCard.WebApi/Startup.cs @@ -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; @@ -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 => @@ -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;