rajanadar / VaultSharp

A comprehensive cross-platform .NET Library for HashiCorp's Vault, a secret management tool
http://rajanadar.github.io/VaultSharp
Apache License 2.0
493 stars 134 forks source link

Deserialization options for ReadSecretAsync<T> #327

Closed Velociraptor45 closed 1 year ago

Velociraptor45 commented 1 year ago

Hey there,

let's say I have a key "username" in my secret and want to retrieve it

var result = client.V1.Secrets.KeyValue.V2.ReadSecretAsync<MyConfig>("path", "mountpoint");

private class MyConfig
{
    public string Username { get; set; } = string.Empty;
}

This won't fill the Username property in result.Data.Data because the secret's key is "username" (lower case). Making the C# property also lowercase works fine. However, it would be great to have some deserialization options where one could define e.g. case-insensitivity for property mapping.

konidev20 commented 1 year ago

Hey @Velociraptor45,

For this issue, you can use the package System.Text.Json.Serialization and the C# Attribute from that package [JsonPropertyName("<propery-name>") for all the properties of your class.

Updatin your MyConfig class to the following should fix the issue for you:

using System.Text.Json.Serialization;
private class MyConfig
{
    [JsonPropertyName("username")]
    public string Username { get; set; } = string.Empty;
}

Thank you, @konidev20

Velociraptor45 commented 1 year ago

No idea why I didn't think of that. Thanks.