Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unit test support to system text json version #64

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<ProjectReference Include="..\6.0-JsonMergePatch.Document\6.0-JsonMergePatch.Document.csproj" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<ProjectReference Include="..\6.0-JsonMergePatch.NewtonsoftJson\6.0-JsonMergePatch.NewtonsoftJson.csproj" />
caleidon marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

</Project>
50 changes: 49 additions & 1 deletion src/6.0-JsonMergePatch.SystemText/Builders/PatchBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,46 @@
using System;
using System.Reflection;
using System.Text.Json;
using Morcatko.AspNetCore.JsonMergePatch.NewtonsoftJson.Builders;
using Newtonsoft.Json.Linq;

namespace Morcatko.AspNetCore.JsonMergePatch.SystemText.Builders
{
public class PatchBuilder<TModel> where TModel : class
{
public static JsonMergePatchDocument<TModel> Build(TModel original, TModel patched, JsonMergePatchOptions options = null)
{
var diff = DiffBuilder.Build(original, patched);
var jsonElement = diff != null ? JsonElementFromJObject(diff) : JsonDocument.Parse("{}").RootElement;
return PatchBuilder.CreatePatchDocument<TModel>(jsonElement, options);
}

public static JsonMergePatchDocument<TModel> Build(string jsonObjectPatch, JsonSerializerOptions jsonOptions = null, JsonMergePatchOptions options = null)
{
var jsonElement = JsonDocument.Parse(jsonObjectPatch).RootElement;
return PatchBuilder.CreatePatchDocument<TModel>(jsonElement, jsonOptions ?? new JsonSerializerOptions(), options ?? new JsonMergePatchOptions());
}

public static JsonMergePatchDocument<TModel> Build(object jsonObjectPatch, JsonMergePatchOptions options = null)
{
var json = JsonSerializer.Serialize(jsonObjectPatch);
var jsonElement = JsonDocument.Parse(json).RootElement;
return PatchBuilder.CreatePatchDocument<TModel>(jsonElement, options);
}

public static JsonMergePatchDocument<TModel> Build(JsonElement jsonObjectPatch, JsonMergePatchOptions options = null)
{
return PatchBuilder.CreatePatchDocument<TModel>(jsonObjectPatch, options);
}

private static JsonElement JsonElementFromJObject(JObject jObject)
{
var jsonString = jObject.ToString();
using var doc = JsonDocument.Parse(jsonString);
return doc.RootElement.Clone();
}
}

public static class PatchBuilder
{
private static object ToObject(this JsonElement jsonElement)
Expand Down Expand Up @@ -67,6 +104,17 @@ private static void AddOperation(IInternalJsonMergePatchDocument jsonMergePatchD
}

static readonly Type internalJsonMergePatchDocumentType = typeof(InternalJsonMergePatchDocument<>);

internal static JsonMergePatchDocument<TModel> CreatePatchDocument<TModel>(JsonElement patchObject, JsonMergePatchOptions options = null) where TModel : class
{
return CreatePatchDocument(typeof(TModel), patchObject, new JsonSerializerOptions(), options ?? new JsonMergePatchOptions()) as JsonMergePatchDocument<TModel>;
}

internal static JsonMergePatchDocument<TModel> CreatePatchDocument<TModel>(JsonElement patchObject, JsonSerializerOptions jsonOptions, JsonMergePatchOptions mergePatchOptions) where TModel : class
{
return CreatePatchDocument(typeof(TModel), patchObject, jsonOptions, mergePatchOptions) as JsonMergePatchDocument<TModel>;
}

internal static IInternalJsonMergePatchDocument CreatePatchDocument(Type modelType, JsonElement jsonElement, JsonSerializerOptions jsonOptions, JsonMergePatchOptions mergePatchOptions)
{
var jsonMergePatchType = internalJsonMergePatchDocumentType.MakeGenericType(modelType);
Expand All @@ -78,4 +126,4 @@ internal static IInternalJsonMergePatchDocument CreatePatchDocument(Type modelTy
return jsonMergePatchDocument;
}
}
}
}
64 changes: 64 additions & 0 deletions src/6.0-JsonMergePatch.Tests/PatchBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Text.Json;
using Morcatko.AspNetCore.JsonMergePatch.SystemText.Builders;
using Xunit;

namespace Morcatko.AspNetCore.JsonMergePatch.Tests;

public class PatchBuilderTests
{
private class TestModel
{
public string Name { get; set; }
public int Age { get; set; }
}

[Fact]
public void Build_FromOriginalAndPatched_ShouldCreatePatchDocument()
{
var original = new TestModel { Name = "John", Age = 30 };
var patched = new TestModel { Name = "John", Age = 31 };

var patchDocument = PatchBuilder<TestModel>.Build(original, patched);

Assert.NotNull(patchDocument);
Assert.Single(patchDocument.Operations);
Assert.Equal("/Age", patchDocument.Operations[0].path);
Assert.Equal(31, patchDocument.Operations[0].value);
}

[Fact]
public void Build_FromJsonString_ShouldCreatePatchDocument()
{
var jsonPatch = "{\"Age\":31}";
var patchDocument = PatchBuilder<TestModel>.Build(jsonPatch);

Assert.NotNull(patchDocument);
Assert.Single(patchDocument.Operations);
Assert.Equal("/Age", patchDocument.Operations[0].path);
Assert.Equal(31, patchDocument.Operations[0].value);
}

[Fact]
public void Build_FromObject_ShouldCreatePatchDocument()
{
var patchObject = new { Age = 31 };
var patchDocument = PatchBuilder<TestModel>.Build(patchObject);

Assert.NotNull(patchDocument);
Assert.Single(patchDocument.Operations);
Assert.Equal("/Age", patchDocument.Operations[0].path);
Assert.Equal(31, patchDocument.Operations[0].value);
}

[Fact]
public void Build_FromJsonElement_ShouldCreatePatchDocument()
{
var jsonElement = JsonDocument.Parse("{\"Age\":31}").RootElement;
var patchDocument = PatchBuilder<TestModel>.Build(jsonElement);

Assert.NotNull(patchDocument);
Assert.Single(patchDocument.Operations);
Assert.Equal("/Age", patchDocument.Operations[0].path);
Assert.Equal(31, patchDocument.Operations[0].value);
}
}