Skip to content

Commit

Permalink
fix: use lower case values during json serialization (#704)
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Dmytrenko <[email protected]>
  • Loading branch information
erka authored Feb 8, 2025
1 parent 50065c1 commit 51cdfa4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
23 changes: 20 additions & 3 deletions flipt-client-csharp/src/FliptClient/Models/ClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ public class ClientOptions
public ErrorStrategy ErrorStrategy { get; set; } = ErrorStrategy.Fail;
}

[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonConverter(typeof(LowercaseEnumConverter<ErrorStrategy>))]
public enum ErrorStrategy
{
[JsonPropertyName("fail")]
Fail,
[JsonPropertyName("fallback")]
Fallback,
}

Expand All @@ -56,4 +54,23 @@ public class Authentication
[JsonPropertyName("jwt_token")]
public string JwtToken { get; set; }
}


public class LowercaseEnumConverter<T> : JsonConverter<T> where T : struct, Enum
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var enumString = reader.GetString();
if (Enum.TryParse<T>(enumString, true, out var value))
{
return value;
}
throw new JsonException($"Unable to convert \"{enumString}\" to {typeof(T)}.");
}

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString().ToLower()); // Convert enum name to lowercase
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using Xunit;
using Xunit.Abstractions;
using FliptClient;
Expand Down Expand Up @@ -164,5 +165,14 @@ public void TestListFlags()
Assert.NotEmpty(flags);
Assert.Equal(2, flags.Length);
}

[Fact]
public void TestClientOptions()
{
var options = new ClientOptions{ErrorStrategy=ErrorStrategy.Fallback};
string optsJson = JsonSerializer.Serialize(options);
string expectedJson = """{"url":"http://localhost:8080","update_interval":120,"authentication":null,"error_strategy":"fallback"}""";
Assert.Equal(expectedJson, optsJson);
}
}
}
11 changes: 11 additions & 0 deletions flipt-engine-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,14 @@ pub unsafe extern "C" fn destroy_engine(engine_ptr: *mut c_void) {
pub unsafe extern "C" fn destroy_string(ptr: *mut c_char) {
let _ = CString::from_raw(ptr);
}

#[cfg(test)]
mod tests {
use crate::{http::ErrorStrategy, EngineOpts};
#[test]
fn test_engine_ops_with_error_strategy() {
let input = r#"{"url":"http://localhost:8080","update_interval":120,"authentication":null,"error_strategy":"fallback"}"#;
let engine_opts: EngineOpts = serde_json::from_str(input).unwrap_or_default();
assert_eq!(ErrorStrategy::Fallback, engine_opts.error_strategy.unwrap());
}
}

0 comments on commit 51cdfa4

Please sign in to comment.