Skip to content

Commit

Permalink
Added case-insensitive deserialization for LauncherConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelvds committed Oct 5, 2023
1 parent 6d0a766 commit 6a61848
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
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 6a61848

Please sign in to comment.