├── .gitignore ├── LICENSE.txt ├── README.md └── src ├── DiffSync.sln ├── DiffSync ├── DiffSync.csproj └── Extensions.cs └── DiffSyncTest ├── DiffSyncTest.csproj ├── DiffSyncTests.cs └── JsonPatchTests.cs /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Daniel Paulino 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DiffSync 2 | A .NET Standard 2.0 library to help with data synchronization, such as determining object differences, applying patches, and merging changes. 3 | 4 | Install from nuget: https://www.nuget.org/packages/DiffSync 5 | 6 | ## Diff and Patch 7 | ```csharp 8 | using DiffSync; 9 | using JeniusApps.Nightingale.Data.Models; 10 | 11 | var w1 = new Workspace 12 | { 13 | Name = "test", 14 | Id = "1", 15 | Items = new List 16 | { 17 | new Item 18 | { 19 | Name = "item1" 20 | } 21 | } 22 | }; 23 | 24 | var w2 = new Workspace 25 | { 26 | Name = "test", 27 | Id = "1", 28 | Items = new List 29 | { 30 | new Item 31 | { 32 | Name = "item1new" 33 | } 34 | } 35 | }; 36 | 37 | var diff = w1.Diff(w2); 38 | var w3 = w1.Patch(diff); 39 | var newResult = w2.Diff(w3); 40 | Assert.Null(newResult); 41 | ``` 42 | 43 | ## Merge 44 | ```csharp 45 | var wBase = new Workspace 46 | { 47 | Name = "test", 48 | Id = "1", 49 | Items = new List 50 | { 51 | new Item 52 | { 53 | Name = "item1" 54 | } 55 | } 56 | }; 57 | 58 | var wFork1 = new Workspace 59 | { 60 | Name = "test", 61 | Id = "1ConflictFork1", 62 | Items = new List 63 | { 64 | new Item 65 | { 66 | Name = "fork 1" 67 | } 68 | } 69 | }; 70 | 71 | var wFork2 = new Workspace 72 | { 73 | Name = "fork 2", 74 | Id = "1ConflictFork2", 75 | Items = new List 76 | { 77 | new Item 78 | { 79 | Name = "item1" 80 | }, 81 | new Item 82 | { 83 | Name = "foobar" 84 | } 85 | } 86 | }; 87 | 88 | var expectedMergeResult = new Workspace 89 | { 90 | Name = "fork 2", 91 | Id = "1ConflictFork2", 92 | Items = new List 93 | { 94 | new Item 95 | { 96 | Name = "fork 1" 97 | }, 98 | new Item 99 | { 100 | Name = "foobar" 101 | } 102 | } 103 | }; 104 | 105 | var mergeResult = wBase.Merge(wFork1, wFork2); 106 | var resultDiff = expectedMergeResult.Diff(mergeResult); 107 | Assert.Null(resultDiff); 108 | ``` 109 | 110 | # Attributions 111 | 112 | - [jsondiffpatch.net](https://github.com/wbish/jsondiffpatch.net). License: https://github.com/wbish/jsondiffpatch.net/blob/master/LICENSE. 113 | - [Newtonsoft.Json](https://github.com/JamesNK/Newtonsoft.Json). License: https://github.com/JamesNK/Newtonsoft.Json/blob/master/LICENSE.md 114 | -------------------------------------------------------------------------------- /src/DiffSync.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30503.244 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiffSync", "DiffSync\DiffSync.csproj", "{7BBEF44D-CA3D-4F32-891A-EB02FE2288A4}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiffSyncTest", "DiffSyncTest\DiffSyncTest.csproj", "{FDC7600A-7DF1-42D9-9DB2-724E854AA7F5}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {7BBEF44D-CA3D-4F32-891A-EB02FE2288A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {7BBEF44D-CA3D-4F32-891A-EB02FE2288A4}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {7BBEF44D-CA3D-4F32-891A-EB02FE2288A4}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {7BBEF44D-CA3D-4F32-891A-EB02FE2288A4}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {FDC7600A-7DF1-42D9-9DB2-724E854AA7F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {FDC7600A-7DF1-42D9-9DB2-724E854AA7F5}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {FDC7600A-7DF1-42D9-9DB2-724E854AA7F5}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {FDC7600A-7DF1-42D9-9DB2-724E854AA7F5}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {01DE7438-3654-4037-B567-090D51C0C0F2} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /src/DiffSync/DiffSync.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | true 6 | true 7 | DiffSync 8 | DiffSync 9 | 2.0.0-preview 10 | Daniel Paulino 11 | 12 | A .NET Standard 2.0 library to help with object synchronization, such as determining object differences, applying patches, and merging changes. 13 | © Daniel Paulino 14 | true 15 | true 16 | snupkg 17 | LICENSE.txt 18 | https://github.com/dpaulino/DiffSync 19 | 2.0.0 - breaking changes. Switched diff operation to use a different library. 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | True 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/DiffSync/Extensions.cs: -------------------------------------------------------------------------------- 1 | using JsonDiffPatchDotNet; 2 | using Newtonsoft.Json.Linq; 3 | using System; 4 | using System.Linq; 5 | 6 | namespace DiffSync 7 | { 8 | public static class Extensions 9 | { 10 | /// 11 | /// Returns null if objects are identical. 12 | /// 13 | public static JToken Diff(this T objOld, T objNew) 14 | { 15 | if (objOld == null) throw new ArgumentNullException(nameof(objOld)); 16 | if (objNew == null) throw new ArgumentNullException(nameof(objNew)); 17 | 18 | var jdp = new JsonDiffPatch(); 19 | var left = JToken.FromObject(objOld); 20 | var right = JToken.FromObject(objNew); 21 | return jdp.Diff(left, right); 22 | } 23 | 24 | public static T Patch(this T objOld, JToken patch) 25 | { 26 | var jdp = new JsonDiffPatch(); 27 | JToken left = JToken.FromObject(objOld); 28 | var result = jdp.Patch(left, patch); 29 | return result.ToObject(); 30 | } 31 | 32 | /// 33 | /// Merges the forks onto the common base. Latter 34 | /// forks will overwrite any conflicts from former 35 | /// forks. 36 | /// 37 | /// The most recent ancestor of the forks. 38 | /// 39 | /// The different forks that will be merged. 40 | /// Merge conflicts are resolved by taking the later fork in the list. 41 | /// 42 | /// A merged object. 43 | public static T Merge(this T commonObjBase, params T[] forks) 44 | { 45 | T rollingResult = commonObjBase; 46 | foreach (JToken diff in forks.Select(x => commonObjBase.Diff(x))) 47 | { 48 | rollingResult = rollingResult.Patch(diff); 49 | } 50 | return rollingResult; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/DiffSyncTest/DiffSyncTest.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | all 16 | runtime; build; native; contentfiles; analyzers; buildtransitive 17 | 18 | 19 | all 20 | runtime; build; native; contentfiles; analyzers; buildtransitive 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/DiffSyncTest/DiffSyncTests.cs: -------------------------------------------------------------------------------- 1 | using DiffSync; 2 | using JeniusApps.Nightingale.Data.Models; 3 | using Newtonsoft.Json; 4 | using System.Collections.Generic; 5 | using Xunit; 6 | 7 | namespace DiffSyncTest 8 | { 9 | public class DiffSyncTests 10 | { 11 | [Fact] 12 | public void DiffTest() 13 | { 14 | var w1 = new Workspace 15 | { 16 | Name = "test", 17 | Id = "1", 18 | Items = new List 19 | { 20 | new Item 21 | { 22 | Name = "item1" 23 | } 24 | } 25 | }; 26 | 27 | var w2 = new Workspace 28 | { 29 | Name = "test", 30 | Id = "1", 31 | Items = new List 32 | { 33 | new Item 34 | { 35 | Name = "item1new" 36 | }, 37 | new Item 38 | { 39 | Name = "item2" 40 | } 41 | } 42 | }; 43 | 44 | 45 | var diff = w1.Diff(w2); 46 | string expectedString = "{\r\n \"Items\": {\r\n \"_t\": \"a\",\r\n \"0\": {\r\n \"Name\": [\r\n \"item1\",\r\n \"item1new\"\r\n ]\r\n },\r\n \"1\": [\r\n {\r\n \"id\": null,\r\n \"parentId\": null,\r\n \"IsTemporary\": false,\r\n \"Properties\": null,\r\n \"Url\": null,\r\n \"Auth\": null,\r\n \"Body\": null,\r\n \"MockData\": null,\r\n \"Children\": null,\r\n \"Headers\": null,\r\n \"ChainingRules\": null,\r\n \"Type\": 0,\r\n \"Name\": \"item2\",\r\n \"IsExpanded\": false,\r\n \"Method\": null,\r\n \"Response\": null\r\n }\r\n ]\r\n }\r\n}"; 47 | 48 | Assert.Equal(expectedString, diff.ToString()); 49 | } 50 | 51 | [Fact] 52 | public void PatchTest() 53 | { 54 | var w1 = new Workspace 55 | { 56 | Name = "test", 57 | Id = "1", 58 | Items = new List 59 | { 60 | new Item 61 | { 62 | Name = "item1" 63 | } 64 | } 65 | }; 66 | 67 | var w2 = new Workspace 68 | { 69 | Name = "test", 70 | Id = "1", 71 | Items = new List 72 | { 73 | new Item 74 | { 75 | Name = "item1new" 76 | } 77 | } 78 | }; 79 | 80 | var diff = w1.Diff(w2); 81 | var w3 = w1.Patch(diff); 82 | var newResult = w2.Diff(w3); 83 | Assert.Null(newResult); 84 | } 85 | 86 | [Fact] 87 | public void PatchDictionaryTest() 88 | { 89 | var a1 = new Authentication 90 | { 91 | AuthProperties = new Dictionary 92 | { 93 | { "Test", "value" } 94 | } 95 | }; 96 | 97 | var a2 = new Authentication 98 | { 99 | AuthProperties = new Dictionary 100 | { 101 | { "Test", "value2" } 102 | } 103 | }; 104 | 105 | var diff = a1.Diff(a2); 106 | var a3 = a1.Patch(diff); 107 | var newResult = a2.Diff(a3); 108 | Assert.Null(newResult); 109 | } 110 | 111 | [Fact] 112 | public void MergeTest() 113 | { 114 | var wBase = new Workspace 115 | { 116 | Name = "test", 117 | Id = "1", 118 | Items = new List 119 | { 120 | new Item 121 | { 122 | Name = "item1" 123 | } 124 | } 125 | }; 126 | 127 | var wFork1 = new Workspace 128 | { 129 | Name = "test", 130 | Id = "1ConflictFork1", 131 | Items = new List 132 | { 133 | new Item 134 | { 135 | Name = "fork 1" 136 | } 137 | } 138 | }; 139 | 140 | var wFork2 = new Workspace 141 | { 142 | Name = "fork 2", 143 | Id = "1ConflictFork2", 144 | Items = new List 145 | { 146 | new Item 147 | { 148 | Name = "item1" 149 | }, 150 | new Item 151 | { 152 | Name = "foobar" 153 | } 154 | } 155 | }; 156 | 157 | var expectedMergeResult = new Workspace 158 | { 159 | Name = "fork 2", 160 | Id = "1ConflictFork2", 161 | Items = new List 162 | { 163 | new Item 164 | { 165 | Name = "fork 1" 166 | }, 167 | new Item 168 | { 169 | Name = "foobar" 170 | } 171 | } 172 | }; 173 | 174 | var mergeResult = wBase.Merge(wFork1, wFork2); 175 | var resultDiff = expectedMergeResult.Diff(mergeResult); 176 | Assert.Null(resultDiff); 177 | } 178 | 179 | [Fact] 180 | public void ComplexObj1ChangeOnlyMergeTest() 181 | { 182 | var wBase = JsonConvert.DeserializeObject("{\"id\":\"d89c9279-1e45-41a7-9330-c35429509856\",\"parentId\":\"root\",\"Name\":\"magic\",\"Methods\":[\"GET\",\"POST\",\"PUT\",\"DELETE\",\"HEAD\",\"OPTIONS\",\"PATCH\",\"MERGE\",\"COPY\"],\"OpenItemIds\":[\"62b0aa85-9d5c-4fc2-b4cd-d2e80558e08e\",\"7c4a8648-24e1-48e0-b2f9-2e30ab8f2b36\"],\"TempItems\":[],\"Items\":[{\"id\":\"7c4a8648-24e1-48e0-b2f9-2e30ab8f2b36\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{\"RequestPivotIndex\":0},\"Url\":{\"Base\":\"\",\"Queries\":[]},\"Auth\":{\"AuthType\":0,\"AuthProperties\":{}},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":null,\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[],\"Headers\":[],\"ChainingRules\":[],\"Type\":1,\"Name\":\"My request\",\"IsExpanded\":false,\"Method\":\"GET\",\"Response\":null},{\"id\":\"fa796649-43a2-4dfb-8a87-01b46382ab14\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{},\"Url\":{\"Base\":null,\"Queries\":[]},\"Auth\":{\"AuthType\":0,\"AuthProperties\":null},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":null,\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[{\"id\":\"62b0aa85-9d5c-4fc2-b4cd-d2e80558e08e\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{\"RequestPivotIndex\":0},\"Url\":{\"Base\":\"44444444444444444444444444444444444?\",\"Queries\":[{\"Key\":\"3333\",\"Value\":\"232424\",\"Enabled\":true,\"Private\":false,\"Type\":0}]},\"Auth\":{\"AuthType\":2,\"AuthProperties\":{\"BasicUsername\":\"asdf\",\"BasicPassword\":\"asdf\",\"DigestUsername\":\"asdf\",\"DigestPassword\":\"asdf\",\"BearerToken\":\"asdf\",\"OAuth2GrantType\":\"authorization_code\",\"OAuth2AccessTokenUrl\":\"asf\",\"OAuth2ClientSecret\":\"asdf\",\"OAuth1ConsumerSecret\":\"asdf\",\"OAuth1ConsumerKey\":\"asdf\"}},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":\"{salfaslkdfj}\",\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[],\"Headers\":[],\"ChainingRules\":[],\"Type\":1,\"Name\":\"234234\",\"IsExpanded\":false,\"Method\":\"GET\",\"Response\":null}],\"Headers\":[],\"ChainingRules\":[],\"Type\":2,\"Name\":\"Untitled\",\"IsExpanded\":true,\"Method\":\"GET\",\"Response\":null}],\"HistoryItems\":[],\"Environments\":[{\"id\":null,\"parentId\":null,\"Name\":\"Base\",\"EnvironmentType\":1,\"Variables\":[{\"Key\":\"env\",\"Value\":\"value\",\"Enabled\":true,\"Private\":false,\"Type\":3}]}],\"Cookies\":null}"); 183 | var wFork2 = JsonConvert.DeserializeObject("{\"id\":\"d89c9279-1e45-41a7-9330-c35429509856\",\"parentId\":\"root\",\"Name\":\"magic\",\"Methods\":[\"GET\",\"POST\",\"PUT\",\"DELETE\",\"HEAD\",\"OPTIONS\",\"PATCH\",\"MERGE\",\"COPY\"],\"OpenItemIds\":[\"62b0aa85-9d5c-4fc2-b4cd-d2e80558e08e\",\"7c4a8648-24e1-48e0-b2f9-2e30ab8f2b36\"],\"TempItems\":[],\"Items\":[{\"id\":\"7c4a8648-24e1-48e0-b2f9-2e30ab8f2b36\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{\"RequestPivotIndex\":0},\"Url\":{\"Base\":\"\",\"Queries\":[]},\"Auth\":{\"AuthType\":0,\"AuthProperties\":{}},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":null,\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[],\"Headers\":[],\"ChainingRules\":[],\"Type\":1,\"Name\":\"My request\",\"IsExpanded\":false,\"Method\":\"GET\",\"Response\":null},{\"id\":\"fa796649-43a2-4dfb-8a87-01b46382ab14\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{},\"Url\":{\"Base\":null,\"Queries\":[]},\"Auth\":{\"AuthType\":0,\"AuthProperties\":null},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":null,\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[{\"id\":\"62b0aa85-9d5c-4fc2-b4cd-d2e80558e08e\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{\"RequestPivotIndex\":0},\"Url\":{\"Base\":\"44444444444444444444444444444444444?\",\"Queries\":[{\"Key\":\"3333\",\"Value\":\"232424\",\"Enabled\":true,\"Private\":false,\"Type\":0}]},\"Auth\":{\"AuthType\":2,\"AuthProperties\":{\"BasicUsername\":\"asdf\",\"BasicPassword\":\"asdf\",\"DigestUsername\":\"asdf\",\"DigestPassword\":\"asdf\",\"BearerToken\":\"asdf\",\"OAuth2GrantType\":\"authorization_code\",\"OAuth2AccessTokenUrl\":\"asf\",\"OAuth2ClientSecret\":\"asdf\",\"OAuth1ConsumerSecret\":\"asdf\",\"OAuth1ConsumerKey\":\"asdf\"}},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":\"{salfaslkdfj}\",\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[],\"Headers\":[],\"ChainingRules\":[],\"Type\":1,\"Name\":\"234234\",\"IsExpanded\":false,\"Method\":\"GET\",\"Response\":null}],\"Headers\":[],\"ChainingRules\":[],\"Type\":2,\"Name\":\"Untitled\",\"IsExpanded\":true,\"Method\":\"GET\",\"Response\":null}],\"HistoryItems\":[],\"Environments\":[{\"id\":null,\"parentId\":null,\"Name\":\"Base\",\"EnvironmentType\":1,\"Variables\":[{\"Key\":\"env\",\"Value\":\"value\",\"Enabled\":true,\"Private\":false,\"Type\":3}]}],\"Cookies\":null}"); 184 | 185 | var wFork1 = JsonConvert.DeserializeObject("{\"id\":\"d89c9279-1e45-41a7-9330-c35429509856\",\"parentId\":\"daniel\",\"Name\":\"magic\",\"Methods\":[\"GET\",\"POST\",\"PUT\",\"DELETE\",\"HEAD\",\"OPTIONS\",\"PATCH\",\"MERGE\",\"COPY\"],\"OpenItemIds\":[\"62b0aa85-9d5c-4fc2-b4cd-d2e80558e08e\",\"7c4a8648-24e1-48e0-b2f9-2e30ab8f2b36\"],\"TempItems\":[],\"Items\":[{\"id\":\"7c4a8648-24e1-48e0-b2f9-2e30ab8f2b36\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{\"RequestPivotIndex\":1},\"Url\":{\"Base\":\"\",\"Queries\":[]},\"Auth\":{\"AuthType\":0,\"AuthProperties\":{}},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":null,\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[],\"Headers\":[],\"ChainingRules\":[],\"Type\":1,\"Name\":\"My request\",\"IsExpanded\":false,\"Method\":\"GET\",\"Response\":null},{\"id\":\"fa796649-43a2-4dfb-8a87-01b46382ab14\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{},\"Url\":{\"Base\":null,\"Queries\":[]},\"Auth\":{\"AuthType\":0,\"AuthProperties\":null},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":null,\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[{\"id\":\"62b0aa85-9d5c-4fc2-b4cd-d2e80558e08e\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{\"RequestPivotIndex\":0},\"Url\":{\"Base\":\"44444444444444444444444444444444444?\",\"Queries\":[{\"Key\":\"123456\",\"Value\":\"alllllll\",\"Enabled\":false,\"Private\":false,\"Type\":0}]},\"Auth\":{\"AuthType\":2,\"AuthProperties\":{\"BasicUsername\":\"asdf\",\"BasicPassword\":\"asdf\",\"DigestUsername\":\"asdf\",\"DigestPassword\":\"asdf\",\"BearerToken\":\"asdf\",\"OAuth2GrantType\":\"authorization_code\",\"OAuth2AccessTokenUrl\":\"asf\",\"OAuth2ClientSecret\":\"asdf\",\"OAuth1ConsumerSecret\":\"asdf\",\"OAuth1ConsumerKey\":\"asdf\"}},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":\"{salfaslkdfj}\",\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[],\"Headers\":[],\"ChainingRules\":[],\"Type\":1,\"Name\":\"234234\",\"IsExpanded\":false,\"Method\":\"GET\",\"Response\":null}],\"Headers\":[],\"ChainingRules\":[],\"Type\":2,\"Name\":\"Untitled\",\"IsExpanded\":true,\"Method\":\"GET\",\"Response\":null}],\"HistoryItems\":[],\"Environments\":[{\"id\":null,\"parentId\":null,\"Name\":\"Base\",\"EnvironmentType\":1,\"Variables\":[{\"Key\":\"env\",\"Value\":\"value\",\"Enabled\":true,\"Private\":false,\"Type\":3}]}],\"Cookies\":null}"); 186 | 187 | var expectedMergeResult = wFork1; 188 | 189 | var mergeResult = wBase.Merge(wFork1, wFork2); 190 | var resultDiff = expectedMergeResult.Diff(mergeResult); 191 | Assert.Null(resultDiff); 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /src/DiffSyncTest/JsonPatchTests.cs: -------------------------------------------------------------------------------- 1 | using JeniusApps.Nightingale.Data.Models; 2 | using JsonDiffPatchDotNet; 3 | using Newtonsoft.Json; 4 | using Newtonsoft.Json.Linq; 5 | using System.Collections.Generic; 6 | using Xunit; 7 | 8 | namespace DiffSyncTest 9 | { 10 | public class JsonPatchTests 11 | { 12 | [Fact] 13 | public void SimplePatchTest() 14 | { 15 | var a1 = new Authentication 16 | { 17 | AuthProperties = new Dictionary 18 | { 19 | { "Test", "value" } 20 | } 21 | }; 22 | 23 | var a2 = new Authentication 24 | { 25 | AuthProperties = new Dictionary 26 | { 27 | { "Test", "value2" } 28 | } 29 | }; 30 | 31 | var jdp = new JsonDiffPatch(); 32 | var left = JToken.FromObject(a1); 33 | var right = JToken.FromObject(a2); 34 | JToken patch = jdp.Diff(left, right); 35 | 36 | var output = jdp.Patch(left, patch); 37 | 38 | Assert.Equal(output.ToString(), right.ToString()); 39 | } 40 | 41 | [Fact] 42 | public void ComplexPatchTest() 43 | { 44 | var obj1 = JsonConvert.DeserializeObject("{\"id\":\"d89c9279-1e45-41a7-9330-c35429509856\",\"parentId\":\"root\",\"Name\":\"magic\",\"Methods\":[\"GET\",\"POST\",\"PUT\",\"DELETE\",\"HEAD\",\"OPTIONS\",\"PATCH\",\"MERGE\",\"COPY\"],\"OpenItemIds\":[\"62b0aa85-9d5c-4fc2-b4cd-d2e80558e08e\",\"7c4a8648-24e1-48e0-b2f9-2e30ab8f2b36\"],\"TempItems\":[],\"Items\":[{\"id\":\"7c4a8648-24e1-48e0-b2f9-2e30ab8f2b36\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{\"RequestPivotIndex\":0},\"Url\":{\"Base\":\"\",\"Queries\":[]},\"Auth\":{\"AuthType\":0,\"AuthProperties\":{}},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":null,\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[],\"Headers\":[],\"ChainingRules\":[],\"Type\":1,\"Name\":\"My request\",\"IsExpanded\":false,\"Method\":\"GET\",\"Response\":null},{\"id\":\"fa796649-43a2-4dfb-8a87-01b46382ab14\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{},\"Url\":{\"Base\":null,\"Queries\":[]},\"Auth\":{\"AuthType\":0,\"AuthProperties\":null},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":null,\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[{\"id\":\"62b0aa85-9d5c-4fc2-b4cd-d2e80558e08e\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{\"RequestPivotIndex\":0},\"Url\":{\"Base\":\"44444444444444444444444444444444444?\",\"Queries\":[{\"Key\":\"3333\",\"Value\":\"232424\",\"Enabled\":true,\"Private\":false,\"Type\":0}]},\"Auth\":{\"AuthType\":2,\"AuthProperties\":{\"BasicUsername\":\"asdf\",\"BasicPassword\":\"asdf\",\"DigestUsername\":\"asdf\",\"DigestPassword\":\"asdf\",\"BearerToken\":\"asdf\",\"OAuth2GrantType\":\"authorization_code\",\"OAuth2AccessTokenUrl\":\"asf\",\"OAuth2ClientSecret\":\"asdf\",\"OAuth1ConsumerSecret\":\"asdf\",\"OAuth1ConsumerKey\":\"asdf\"}},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":\"{salfaslkdfj}\",\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[],\"Headers\":[],\"ChainingRules\":[],\"Type\":1,\"Name\":\"234234\",\"IsExpanded\":false,\"Method\":\"GET\",\"Response\":null}],\"Headers\":[],\"ChainingRules\":[],\"Type\":2,\"Name\":\"Untitled\",\"IsExpanded\":true,\"Method\":\"GET\",\"Response\":null}],\"HistoryItems\":[],\"Environments\":[{\"id\":null,\"parentId\":null,\"Name\":\"Base\",\"EnvironmentType\":1,\"Variables\":[{\"Key\":\"env\",\"Value\":\"value\",\"Enabled\":true,\"Private\":false,\"Type\":3}]}],\"Cookies\":null}"); 45 | var obj2 = JsonConvert.DeserializeObject("{\"id\":\"d89c9279-1e45-41a7-9330-c35429509856\",\"parentId\":\"daniel\",\"Name\":\"magic\",\"Methods\":[\"GET\",\"POST\",\"PUT\",\"DELETE\",\"HEAD\",\"OPTIONS\",\"PATCH\",\"MERGE\",\"COPY\"],\"OpenItemIds\":[\"62b0aa85-9d5c-4fc2-b4cd-d2e80558e08e\",\"7c4a8648-24e1-48e0-b2f9-2e30ab8f2b36\"],\"TempItems\":[],\"Items\":[{\"id\":\"7c4a8648-24e1-48e0-b2f9-2e30ab8f2b36\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{\"RequestPivotIndex\":1},\"Url\":{\"Base\":\"\",\"Queries\":[]},\"Auth\":{\"AuthType\":0,\"AuthProperties\":{}},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":null,\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[],\"Headers\":[],\"ChainingRules\":[],\"Type\":1,\"Name\":\"My request\",\"IsExpanded\":false,\"Method\":\"GET\",\"Response\":null},{\"id\":\"fa796649-43a2-4dfb-8a87-01b46382ab14\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{},\"Url\":{\"Base\":null,\"Queries\":[]},\"Auth\":{\"AuthType\":0,\"AuthProperties\":null},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":null,\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[{\"id\":\"62b0aa85-9d5c-4fc2-b4cd-d2e80558e08e\",\"parentId\":null,\"IsTemporary\":false,\"Properties\":{\"RequestPivotIndex\":0},\"Url\":{\"Base\":\"44444444444444444444444444444444444?\",\"Queries\":[{\"Key\":\"123456\",\"Value\":\"alllllll\",\"Enabled\":false,\"Private\":false,\"Type\":0}]},\"Auth\":{\"AuthType\":2,\"AuthProperties\":{\"BasicUsername\":\"asdf\",\"BasicPassword\":\"asdf\",\"DigestUsername\":\"asdf\",\"DigestPassword\":\"asdf\",\"BearerToken\":\"asdf\",\"OAuth2GrantType\":\"authorization_code\",\"OAuth2AccessTokenUrl\":\"asf\",\"OAuth2ClientSecret\":\"asdf\",\"OAuth1ConsumerSecret\":\"asdf\",\"OAuth1ConsumerKey\":\"asdf\"}},\"Body\":{\"BodyType\":0,\"JsonBody\":null,\"XmlBody\":null,\"TextBody\":null,\"FormEncodedData\":[],\"FormDataList\":[],\"BinaryFilePath\":null},\"MockData\":{\"Body\":\"{salfaslkdfj}\",\"StatusCode\":200,\"ContentType\":\"application/json\"},\"Children\":[],\"Headers\":[],\"ChainingRules\":[],\"Type\":1,\"Name\":\"234234\",\"IsExpanded\":false,\"Method\":\"GET\",\"Response\":null}],\"Headers\":[],\"ChainingRules\":[],\"Type\":2,\"Name\":\"Untitled\",\"IsExpanded\":true,\"Method\":\"GET\",\"Response\":null}],\"HistoryItems\":[],\"Environments\":[{\"id\":null,\"parentId\":null,\"Name\":\"Base\",\"EnvironmentType\":1,\"Variables\":[{\"Key\":\"env\",\"Value\":\"value\",\"Enabled\":true,\"Private\":false,\"Type\":3}]}],\"Cookies\":null}"); 46 | 47 | var jdp = new JsonDiffPatch(); 48 | var left = JToken.FromObject(obj1); 49 | var right = JToken.FromObject(obj2); 50 | JToken patch = jdp.Diff(left, right); 51 | var output = jdp.Patch(left, patch); 52 | Assert.Equal(output.ToString(), right.ToString()); 53 | } 54 | 55 | } 56 | } 57 | --------------------------------------------------------------------------------