From 8e4a64d82e7cd4103451cdceab885f4202dd0a33 Mon Sep 17 00:00:00 2001 From: jlukawska Date: Wed, 1 Apr 2020 20:51:16 +0200 Subject: [PATCH] #651 merge custom properties --- .../ConfigurationBuilderExtensions.cs | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs b/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs index 4649ecd0b3..feda99e347 100644 --- a/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs +++ b/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs @@ -70,7 +70,10 @@ public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder .Where(fi => reg.IsMatch(fi.Name) && fi.Name != excludeConfigName) .ToArray(); - var fileConfiguration = new FileConfiguration(); + dynamic fileConfiguration = new ExpandoObject(); + fileConfiguration.GlobalConfiguration = new ExpandoObject(); + fileConfiguration.Aggregates = new List(); + fileConfiguration.ReRoutes = new List(); foreach (var file in files) { @@ -81,15 +84,15 @@ public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder var lines = File.ReadAllText(file.FullName); - var config = JsonConvert.DeserializeObject(lines); + dynamic config = JsonConvert.DeserializeObject(lines); if (file.Name.Equals(GlobalConfigFile, StringComparison.OrdinalIgnoreCase)) { - fileConfiguration.GlobalConfiguration = config.GlobalConfiguration; + TryAddSection(fileConfiguration, config, nameof(FileConfiguration.GlobalConfiguration)); } - fileConfiguration.Aggregates.AddRange(config.Aggregates); - fileConfiguration.Routes.AddRange(config.Routes); + TryAddSection(fileConfiguration, config, nameof(FileConfiguration.Aggregates)); + TryAddSection(fileConfiguration, config, nameof(FileConfiguration.ReRoutes)); } return builder.AddOcelot(fileConfiguration); @@ -110,5 +113,26 @@ public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder return builder.AddJsonFile(PrimaryConfigFile, false, false); } + + private static void TryAddSection(ExpandoObject mergedConfig, ExpandoObject config, string sectionName) + { + var configAsDict = config as IDictionary; + var mergedConfigAsDict = mergedConfig as IDictionary; + if (configAsDict.ContainsKey(sectionName) && mergedConfigAsDict.ContainsKey(sectionName)) + { + var mergedSectionAsExpando = mergedConfigAsDict[sectionName] as ExpandoObject; + if (mergedSectionAsExpando != null) + { + mergedConfigAsDict[sectionName] = configAsDict[sectionName]; + } + else + { + var mergedSectionAsList = mergedConfigAsDict[sectionName] as List; + var sectionAsList = configAsDict[sectionName] as List; + + mergedSectionAsList.AddRange(sectionAsList); + } + } + } } }