Skip to content

Commit

Permalink
Merge pull request #106 from fiskaltrust/Ignore-casing-in-config-file…
Browse files Browse the repository at this point in the history
…-#105

Added case-insensitive deserialization for LauncherConfiguration
  • Loading branch information
pawelvds authored Oct 12, 2023
2 parents 6d0a766 + 6da3be5 commit 7beb14c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
2 changes: 1 addition & 1 deletion azure-pipelines/templates/stages/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ stages:
target: linux-arm64

osx-x64:
vmImage: macos-latest
vmImage: macos-11
target: osx-x64

pool:
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines/templates/stages/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ stages:

- job: TestMacOS
pool:
vmImage: macos-latest
vmImage: macos-11

steps:
- template: ../restore.yml
Expand Down
18 changes: 8 additions & 10 deletions src/fiskaltrust.Launcher.Common/Configuration/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,11 @@ public static LauncherConfiguration Deserialize(string text)
var configuration = JsonSerializer.Deserialize(
text,
typeof(LauncherConfiguration),
new SerializerContext(new JsonSerializerOptions
new JsonSerializerOptions
{
Converters = {
new JsonStringEnumConverter()
},
})
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter() },
}
) as LauncherConfiguration ?? throw new Exception($"Could not deserialize {nameof(LauncherConfiguration)}");
configuration.SetAlternateNames(text);
return configuration;
Expand Down Expand Up @@ -339,12 +338,11 @@ public record LauncherConfigurationInCashBoxConfiguration
var configuration = (JsonSerializer.Deserialize(
text,
typeof(LauncherConfigurationInCashBoxConfiguration),
new SerializerContext(new JsonSerializerOptions
new JsonSerializerOptions
{
Converters = {
new JsonStringEnumConverter()
}
})
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter() },
}
) as LauncherConfigurationInCashBoxConfiguration ?? throw new Exception($"Could not deserialize {nameof(LauncherConfigurationInCashBoxConfiguration)}")).LauncherConfiguration;
configuration?.SetAlternateNames(text);
return configuration;
Expand Down
50 changes: 50 additions & 0 deletions test/fiskaltrust.Launcher.UnitTest/Configuration/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,55 @@ public void RandomConfiguration_SerializaAndDeserialize_ShouldPreserveNull()
deserialized.Should().BeEquivalentTo(deserialized);
}
}

[Fact]
public void DifferentCaseInKeys_Deserialize_ShouldPreserveProperties()
{
var json = @"{
""loglevel"": ""Information"",
""LOGLEVEL"": ""Error"",
""LogLevel"": ""Warning""
}";

var deserialized = LauncherConfiguration.Deserialize(json);

deserialized.LogLevel.Should().Be(LogLevel.Warning);
}

[Fact]
public void LowerCaseKeys_Deserialize_ShouldPreserveProperties()
{
var json = @"{
""loglevel"": ""Information""
}";

var deserialized = LauncherConfiguration.Deserialize(json);

deserialized.LogLevel.Should().Be(LogLevel.Information);
}

[Fact]
public void UpperCaseKeys_Deserialize_ShouldPreserveProperties()
{
var json = @"{
""LOGLEVEL"": ""Error""
}";

var deserialized = LauncherConfiguration.Deserialize(json);

deserialized.LogLevel.Should().Be(LogLevel.Error);
}

[Fact]
public void MixedCaseKeys_Deserialize_ShouldPreserveProperties()
{
var json = @"{
""logLevel"": ""Warning""
}";

var deserialized = LauncherConfiguration.Deserialize(json);

deserialized.LogLevel.Should().Be(LogLevel.Warning);
}
}
}

0 comments on commit 7beb14c

Please sign in to comment.