diff --git a/eShopOnWeb.sln b/eShopOnWeb.sln
index 769e7754b..248c0ab7d 100755
--- a/eShopOnWeb.sln
+++ b/eShopOnWeb.sln
@@ -4,6 +4,8 @@ VisualStudioVersion = 16.0.29102.190
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{419A6ACE-0419-4315-A6FB-B0E63D39432E}"
EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Web", "src\Web\Web.csproj", "{227CF035-29B0-448D-97E4-944F9EA850E5}"
+EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Infrastructure", "src\Infrastructure\Infrastructure.csproj", "{7C461394-ABDC-43CD-A798-71249C58BA67}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApplicationCore", "src\ApplicationCore\ApplicationCore.csproj", "{7FED7440-2311-4D1E-958B-3E887C585CD2}"
@@ -26,8 +28,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
-Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Web", "src\Web\Web.csproj", "{227CF035-29B0-448D-97E4-944F9EA850E5}"
-EndProject
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{1FCBE191-34FE-4B2E-8915-CA81553958AD}"
EndProject
Global
diff --git a/src/ApplicationCore/ApplicationCore.csproj b/src/ApplicationCore/ApplicationCore.csproj
index a35c71fb9..4eb43c531 100644
--- a/src/ApplicationCore/ApplicationCore.csproj
+++ b/src/ApplicationCore/ApplicationCore.csproj
@@ -9,6 +9,7 @@
+
\ No newline at end of file
diff --git a/src/ApplicationCore/Extensions/JsonExtensions.cs b/src/ApplicationCore/Extensions/JsonExtensions.cs
new file mode 100644
index 000000000..ec3d61b7c
--- /dev/null
+++ b/src/ApplicationCore/Extensions/JsonExtensions.cs
@@ -0,0 +1,18 @@
+using System.Text.Json;
+
+namespace Microsoft.eShopWeb
+{
+ public static class JsonExtensions
+ {
+ private static readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions
+ {
+ PropertyNameCaseInsensitive = true
+ };
+
+ public static T FromJson(this string json) =>
+ JsonSerializer.Deserialize(json, _jsonOptions);
+
+ public static string ToJson(this T obj) =>
+ JsonSerializer.Serialize(obj, _jsonOptions);
+ }
+}
diff --git a/src/Web/Startup.cs b/src/Web/Startup.cs
index 720f6664f..e7591c53c 100644
--- a/src/Web/Startup.cs
+++ b/src/Web/Startup.cs
@@ -172,16 +172,15 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
ResponseWriter = async (context, report) =>
{
- var result = JsonConvert.SerializeObject(
- new
+ var result = new
+ {
+ status = report.Status.ToString(),
+ errors = report.Entries.Select(e => new
{
- status = report.Status.ToString(),
- errors = report.Entries.Select(e => new
- {
- key = e.Key,
- value = Enum.GetName(typeof(HealthStatus), e.Value.Status)
- })
- });
+ key = e.Key,
+ value = Enum.GetName(typeof(HealthStatus), e.Value.Status)
+ })
+ }.ToJson();
context.Response.ContentType = MediaTypeNames.Application.Json;
await context.Response.WriteAsync(result);
}
diff --git a/tests/FunctionalTests/Web/Controllers/ApiCatalogControllerList.cs b/tests/FunctionalTests/Web/Controllers/ApiCatalogControllerList.cs
index a0e813469..c457a54c1 100644
--- a/tests/FunctionalTests/Web/Controllers/ApiCatalogControllerList.cs
+++ b/tests/FunctionalTests/Web/Controllers/ApiCatalogControllerList.cs
@@ -25,7 +25,7 @@ public async Task ReturnsFirst10CatalogItems()
var response = await Client.GetAsync("/api/catalog/list");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
- var model = JsonSerializer.Deserialize(stringResponse, _jsonOptions);
+ var model = stringResponse.FromJson();
Assert.Equal(10, model.CatalogItems.Count());
}
@@ -36,7 +36,7 @@ public async Task ReturnsLast2CatalogItemsGivenPageIndex1()
var response = await Client.GetAsync("/api/catalog/list?page=1");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
- var model = JsonSerializer.Deserialize(stringResponse, _jsonOptions);
+ var model = stringResponse.FromJson();
Assert.Equal(2, model.CatalogItems.Count());
}
diff --git a/tests/UnitTests/ApplicationCore/Extensions/JsonExtensions.cs b/tests/UnitTests/ApplicationCore/Extensions/JsonExtensions.cs
new file mode 100644
index 000000000..c9842243e
--- /dev/null
+++ b/tests/UnitTests/ApplicationCore/Extensions/JsonExtensions.cs
@@ -0,0 +1,36 @@
+using Xunit;
+
+namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Extensions
+{
+ public class JsonExtensions
+ {
+ [Fact]
+ public void CorrectlySerializesAndDeserializesObject()
+ {
+ var testParent = new TestParent
+ {
+ Id = 7,
+ Name = "Test name",
+ Children = new[]
+ {
+ new TestChild(),
+ new TestChild(),
+ new TestChild()
+ }
+ };
+
+ var json = testParent.ToJson();
+ var result = json.FromJson();
+ Assert.Equal(testParent, result);
+ }
+
+ [
+ Theory,
+ InlineData("{ \"id\": 9, \"name\": \"Another test\" }", 9, "Another test"),
+ InlineData("{ \"id\": 3124, \"name\": \"Test Value 1\" }", 3124, "Test Value 1"),
+ ]
+ public void CorrectlyDeserializesJson(string json, int expectedId, string expectedName) =>
+ Assert.Equal(new TestParent { Id = expectedId, Name = expectedName }, json.FromJson());
+
+ }
+}
\ No newline at end of file
diff --git a/tests/UnitTests/ApplicationCore/Extensions/TestChild.cs b/tests/UnitTests/ApplicationCore/Extensions/TestChild.cs
new file mode 100644
index 000000000..bd07bf3aa
--- /dev/null
+++ b/tests/UnitTests/ApplicationCore/Extensions/TestChild.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
+
+namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Extensions
+{
+ [DebuggerDisplay("Id={Id}, Date={Date}")]
+ public class TestChild : IEquatable
+ {
+ public Guid Id { get; set; } = Guid.NewGuid();
+
+ public DateTime Date { get; set; } = DateTime.UtcNow;
+
+ public bool Equals([AllowNull] TestChild other) =>
+ other?.Date == Date && other?.Id == Id;
+ }
+}
diff --git a/tests/UnitTests/ApplicationCore/Extensions/TestParent.cs b/tests/UnitTests/ApplicationCore/Extensions/TestParent.cs
new file mode 100644
index 000000000..eb5541e28
--- /dev/null
+++ b/tests/UnitTests/ApplicationCore/Extensions/TestParent.cs
@@ -0,0 +1,21 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Linq;
+
+namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Extensions
+{
+ public class TestParent : IEquatable
+ {
+ public int Id { get; set; }
+
+ public string Name { get; set; }
+
+ public IEnumerable Children { get; set; }
+
+ public bool Equals([AllowNull] TestParent other) =>
+ other?.Id == Id && other?.Name == Name &&
+ (other?.Children is null && Children is null ||
+ (other?.Children?.Zip(Children)?.All(t => t.First?.Equals(t.Second) ?? false) ?? false));
+ }
+}
\ No newline at end of file