TemplateMerger.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
 
namespace APIManagementTemplate
{
    public class TemplateMerger
    {
        /// <summary>
        /// Merges the changes in the newObject into the oldObject.
        /// Please not that the contents of oldObject is changed!
        /// </summary>
        /// <param name="oldObject">The old object. The contents of this parameter is changed by this function</param>
        /// <param name="newObject">The new object.</param>
        /// <returns></returns>
        public static JObject Merge(JObject oldObject, JObject newObject)
        {
            foreach (KeyValuePair<string, JToken> pair in newObject)
            {
                if (oldObject[pair.Key] != null)
                {
                    if (pair.Value is JArray newArray && oldObject[pair.Key] is JArray oldArray)
                    {
                        foreach (var item in newArray)
                        {
                            if (item is JValue)
                            {
                                if (oldArray.All(x => !JToken.EqualityComparer.Equals(x, item)))
                                    oldArray.Add(item);
                            }
                            else if (item is JObject)
                            {
                                var oldItem = oldArray.OfType<JObject>().FirstOrDefault(x => SameIdentity(x, item, pair.Key));
                                if (oldItem == null)
                                {
                                    if (oldArray.OfType<JObject>().Any(x => JToken.EqualityComparer.Equals(x, item)))
                                        continue;
                                }
                                if (oldItem == null)
                                    oldArray.Add(item);
                                else
                                {
                                    Merge(oldItem, (JObject)item);
                                }
                            }
                        }
                    }
                    else if (pair.Value is JObject && oldObject[pair.Key] is JObject)
                    {
                        Merge((JObject)oldObject[pair.Key], (JObject)pair.Value);
                    }
                    else if(pair.Value is JValue && oldObject[pair.Key] is JValue)
                    {
                        oldObject[pair.Key] = pair.Value;
                    }
                }
                else
                    oldObject[pair.Key] = pair.Value;
            }
            return oldObject;
        }
 
        public static bool SameIdentity(JToken x, JToken y, string parent)
        {
            if (x == null || y == null)
                return false;
            switch (parent.ToLower())
            {
                case "resources":
                    return JTokenEqualsOnProperties(x, y, "name", "type");
                case "responses":
                    return JTokenEqualsOnProperties(x, y, "statusCode");
                case "representations":
                    return JTokenEqualsOnProperties(x, y, "contentType");
                case "templateparameters":
                    return JTokenEqualsOnProperties(x, y, "name");
                default:
                    return JToken.EqualityComparer.Equals(x, y);
            }
        }
 
        private static bool JTokenEqualsOnProperties(JToken x, JToken y, params string[] properties)
        {
            if (properties.Any(p => x.Value<string>(p) == null || y.Value<string>(p) == null))
                return false;
            return properties.All(p => x.Value<string>(p) == y.Value<string>(p));
        }
    }
 
 
}