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

Fix the way configuration was being bound, to make it work for complex objects. #471

Merged
merged 2 commits into from
Oct 6, 2023
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
4 changes: 4 additions & 0 deletions COMET.Web.Common.Tests/COMET.Web.Common.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<None Remove="Data\server_configuration_tests.json" />
<Content Include="Data\server_configuration_tests.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions COMET.Web.Common.Tests/Data/server_configuration_tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"ServerConfiguration": {
"ServerAddress": "https://a.b.c",
"BookInputConfiguration": {
"ShowName": true,
"ShowShortName": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

namespace COMET.Web.Common.Tests.Server.Services.ConfigurationService
{
using System.Text.Json;

using COMET.Web.Common.Model.Configuration;
using COMET.Web.Common.Server.Services.ConfigurationService;

Expand All @@ -50,8 +48,7 @@ public async Task VerifyInitializeServiceWithEmptyConfiguration()
Assert.Multiple(() =>
{
configuration.Verify(x => x.GetSection(ConfigurationService.ServerConfigurationSection), Times.Once);
Assert.That(service.ServerConfiguration.ServerAddress, Is.Null);
Assert.That(service.ServerConfiguration.BookInputConfiguration, Is.Null);
Assert.That(service.ServerConfiguration, Is.Null);
});

await service.InitializeService();
Expand All @@ -65,8 +62,6 @@ public async Task VerifyInitializeServiceWithEmptyConfiguration()
[Test]
public async Task VerifyInitializeServiceWithConfiguration()
{
var serverAddressMockConfigurationSection = new Mock<IConfigurationSection>();

var serverConfiguration = new ServerConfiguration
{
ServerAddress = "https://a.b.c",
Expand All @@ -76,13 +71,12 @@ public async Task VerifyInitializeServiceWithConfiguration()
ShowShortName = true
}
};

var serverConfigurationJson = JsonSerializer.Serialize(serverConfiguration);
serverAddressMockConfigurationSection.Setup(x => x.Value).Returns(serverConfigurationJson);

var configuration = new Mock<IConfiguration>();
configuration.Setup(x => x.GetSection(ConfigurationService.ServerConfigurationSection)).Returns(serverAddressMockConfigurationSection.Object);
var service = new ConfigurationService(configuration.Object);
var config = new ConfigurationBuilder()
.AddJsonFile("Data/server_configuration_tests.json")
.Build();

var service = new ConfigurationService(config);
await service.InitializeService();

Assert.Multiple(() =>
Expand Down
1 change: 1 addition & 0 deletions COMET.Web.Common/COMET.Web.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="7.0.4" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageReference Include="ReactiveUI" Version="18.4.26" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace COMET.Web.Common.Server.Services.ConfigurationService
using System.Text.Json;

using COMET.Web.Common.Model.Configuration;
using COMET.Web.Common.Model.DTO;
using COMET.Web.Common.Services.ConfigurationService;

using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -68,15 +67,7 @@ public override Task InitializeService()
return Task.CompletedTask;
}

this.ServerConfiguration = new ServerConfiguration();

var serverConfigurationSection = this.configuration.GetSection(ServerConfigurationSection);

if (serverConfigurationSection.Exists())
{
this.ServerConfiguration = JsonSerializer.Deserialize<ServerConfiguration>(serverConfigurationSection.Value);
}

this.ServerConfiguration = this.configuration.GetSection(ServerConfigurationSection).Get<ServerConfiguration>();
this.IsInitialized = true;
return Task.CompletedTask;
}
Expand Down
Loading