Skip to content

Commit

Permalink
Fix build after rebasing, with a refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
raman-m committed Nov 11, 2024
1 parent 4eedbae commit 1b2198c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 97 deletions.
59 changes: 28 additions & 31 deletions src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ private static string GetMergedOcelotJson(string folder, IWebHostEnvironment env
!fi.FullName.Equals(environmentFileInfo.FullName, StringComparison.OrdinalIgnoreCase))
.ToArray();

dynamic fileConfigurationMerged = fileConfiguration != null ? JObject.FromObject(fileConfiguration) : new JObject();
fileConfigurationMerged.GlobalConfiguration ??= new JObject();
fileConfigurationMerged.Aggregates ??= new JArray();
fileConfigurationMerged.Routes ??= new JArray();
fileConfiguration ??= new FileConfiguration();
dynamic fcMerged = JObject.FromObject(fileConfiguration);
fcMerged.GlobalConfiguration ??= new JObject();
fcMerged.Aggregates ??= new JArray();
fcMerged.Routes ??= new JArray();

fileConfiguration ??= new FileConfiguration();
primaryFile ??= Path.Join(folder, PrimaryConfigFile);
globalFile ??= Path.Join(folder, GlobalConfigFile);
var primaryFileInfo = new FileInfo(primaryFile);
Expand All @@ -137,17 +137,14 @@ private static string GetMergedOcelotJson(string folder, IWebHostEnvironment env
continue;
}

//var lines = File.ReadAllText(file.FullName);
//var config = JsonConvert.DeserializeObject<FileConfiguration>(lines);
var lines = File.ReadAllText(file.FullName);
dynamic config = JToken.Parse(lines);
var isGlobal = file.Name.Equals(globalFileInfo.Name, StringComparison.OrdinalIgnoreCase) &&
bool isGlobal = file.Name.Equals(globalFileInfo.Name, StringComparison.OrdinalIgnoreCase) &&
file.FullName.Equals(globalFileInfo.FullName, StringComparison.OrdinalIgnoreCase);

MergeConfig(fileConfigurationMerged, config, isGlobal);
MergeConfig(fcMerged, config, isGlobal);
}

return ((JObject)fileConfigurationMerged).ToString();
return ((JObject)fcMerged).ToString();
}

public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, JObject fileConfiguration)
Expand All @@ -164,9 +161,8 @@ public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder
/// <param name="optional">The 2nd argument of the AddJsonFile.</param>
/// <param name="reloadOnChange">The 3rd argument of the AddJsonFile.</param>
/// <returns>An <see cref="IConfigurationBuilder"/> object.</returns>
//public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, FileConfiguration fileConfiguration,
// string primaryConfigFile = null, bool? optional = null, bool? reloadOnChange = null) // optional injections
public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, FileConfiguration fileConfiguration)
public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, FileConfiguration fileConfiguration,
string primaryConfigFile = null, bool? optional = null, bool? reloadOnChange = null) // optional injections
=> SerializeToFile(builder, fileConfiguration);

private static IConfigurationBuilder SerializeToFile(IConfigurationBuilder builder, object fileConfiguration, bool? optional = null, bool? reloadOnChange = null)
Expand Down Expand Up @@ -215,33 +211,34 @@ private static IConfigurationBuilder AddOcelotJsonFile(IConfigurationBuilder bui
return builder?.AddJsonFile(primary, optional ?? false, reloadOnChange ?? false);
}

private static void MergeConfig(JToken destConfig, JToken srcConfig, bool isGlobal)
private static void MergeConfig(JToken to, JToken from, bool isGlobal)
{
if (isGlobal)
{
MergeConfigSection(destConfig, srcConfig, nameof(FileConfiguration.GlobalConfiguration));
MergeConfigSection(to, from, nameof(FileConfiguration.GlobalConfiguration));
}

MergeConfigSection(destConfig, srcConfig, nameof(FileConfiguration.Aggregates));
MergeConfigSection(destConfig, srcConfig, nameof(FileConfiguration.Routes));
MergeConfigSection(to, from, nameof(FileConfiguration.Aggregates));
MergeConfigSection(to, from, nameof(FileConfiguration.Routes));
}

private static void MergeConfigSection(JToken destConfig, JToken srcConfig, string sectionName)
private static void MergeConfigSection(JToken to, JToken from, string sectionName)
{
var destConfigSection = destConfig[sectionName];
var srcConfigSection = srcConfig[sectionName];
var destination = to[sectionName];
var source = from[sectionName];
if (source == null || destination == null)
{
return;
}

if (srcConfigSection != null)
if (source is JObject)
{
if (srcConfigSection is JObject)
{
destConfig[sectionName] = srcConfigSection;
}
else if (srcConfigSection is JArray)
{
(destConfigSection as JArray).Merge(srcConfigSection);
}
}
to[sectionName] = source;
}
else if (source is JArray)
{
(destination as JArray).Merge(source);
}
}
}
}
70 changes: 18 additions & 52 deletions test/Ocelot.AcceptanceTests/ConfigurationBuilderExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,40 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Linq;
using Ocelot.Configuration.File;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

namespace Ocelot.AcceptanceTests
{
public class ConfigurationBuilderExtensionsTests
{
private IWebHostBuilder _webHostBuilder;
private TestServer _ocelotServer;
public class ConfigurationBuilderExtensionsTests : Steps
{
private JObject _config;

[Fact]
public void Should_merge_routes_custom_properties()
{
this.Given(x => GivenOcelotIsRunningWithMultipleConfigs())
.When(x => x.WhenICreateClient())
.Then(x => ThenConfigContentShouldHaveThreeRoutes())
var folder = "MergeConfiguration"; // TODO Convert to dynamic temp test folder instead of static one
this.Given(x => GivenOcelotIsRunningWithMultipleConfigs(folder))
.Then(x => ThenConfigContentShouldHaveThreeRoutes(folder))
.And(x => ShouldMergeWithCustomPropertyInXservices())
.And(x => ShouldMergeWithCustomGlobalProperty())
.And(x => ShouldMergeWithCustomPropertyInYservices())
.BDDfy();
}

private void GivenOcelotIsRunningWithMultipleConfigs()
{
_webHostBuilder = new WebHostBuilder();

_webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false);
config.AddOcelot("MergeConfiguration", hostingContext.HostingEnvironment);
config.AddEnvironmentVariables();
})
.ConfigureServices(s =>
{
s.AddOcelot();
})
.Configure(app =>
{
app.UseOcelot().Wait();
});
}

private void WhenICreateClient()
{
_ocelotServer = new TestServer(_webHostBuilder);
_ = _ocelotServer.CreateClient();
}
private void GivenOcelotIsRunningWithMultipleConfigs(string folder) => StartOcelot(
(context, config) => config.AddOcelot(folder, context.HostingEnvironment),
"Env");

private void ThenConfigContentShouldHaveThreeRoutes()
private async Task ThenConfigContentShouldHaveThreeRoutes(string folder)
{
var mergedConfigFileName = "ocelot.json";
File.Exists(mergedConfigFileName).ShouldBeTrue();
var lines = File.ReadAllText(mergedConfigFileName);
_config = JObject.Parse(lines);

_config[nameof(FileConfiguration.Routes)].ShouldNotBeNull()
.Children().Count().ShouldBe(3);
const int three = 3;
var mergedConfigFile = Path.Combine(folder, ConfigurationBuilderExtensions.PrimaryConfigFile);
File.Exists(mergedConfigFile).ShouldBeTrue();
var lines = await File.ReadAllTextAsync(mergedConfigFile);
_config = JObject.Parse(lines).ShouldNotBeNull();
var routes = _config[nameof(FileConfiguration.Routes)].ShouldNotBeNull();
routes.Children().Count().ShouldBe(three);
}

private JObject _config;

private void ShouldMergeWithCustomPropertyInXservices()
{
var customPropertyX = PropertyShouldExist("CustomStrategyProperty");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,8 @@ private void GivenTheEnvironmentNameIsUnavailable()

private async Task WhenISetTheConfiguration(FileConfiguration fileConfiguration)
{
await _repo.Set(fileConfiguration);
var response = await _repo.Get();
_result = response.Data;
}

private void WhenISetTheConfiguration()
{
_repo.SetAsync(_fileConfiguration);
_result = _repo.GetAsync().Result;
await _repo.SetAsync(fileConfiguration);
_result = await _repo.GetAsync();
}

private void ThenTheConfigurationIsStoredAs(FileConfiguration expecteds)
Expand Down Expand Up @@ -189,11 +182,7 @@ private void ThenTheConfigurationJsonIsIndented(FileConfiguration expecteds, [Ca
_files.Add(environmentSpecific);
}

private async Task WhenIGetTheRoutes()
{
var response = await _repo.Get();
_result = response.Data;
}
private async Task WhenIGetTheRoutes() => _result = await _repo.GetAsync();

private void ThenTheFollowingIsReturned(FileConfiguration expecteds)
{
Expand Down

0 comments on commit 1b2198c

Please sign in to comment.