auth0/auth0.net

Retrieve User.AppMetadata as specific type instead of dynamic

MichaelPruefer opened this issue · 3 comments

Checklist

  • I have looked into the Readme and have not found a suitable solution or answer.
  • I have looked into the API documentation and have not found a suitable solution or answer.
  • I have searched the issues and have not found a suitable solution or answer.
  • I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • I agree to the terms within the Auth0 Code of Conduct.

Describe the problem you'd like to have solved

I'm always frustrated when... (interesting placeholder...)

i try to read Auth0.ManagementApi.Models.UserBase.AppMetadata as it's type is dynamic.
I can only see what the real type is when debugging.

And then i have to cast to a type which i can't control and could be changed at any time (for now it's a Newtonsoft.Json.Linq.JObject).

Describe the ideal solution

I'd like to have a method get the AppMetadata converted into my own type. e.g.

user.GetAppMetadata<MyAppMetadataType>()

So the Auth0 implementation can convert AppMetadata possibly via Newtonsoft.Json.Linq.JObject.ToObject into my desired type without me knowing how AppMetadata was de-/serialized along the way.

Alternatives and current workarounds

Currently i have to check which type AppMetadata is via e.g. if (user.AppMetadata is Newtonsoft.Json.Linq.JObject metadataObject) ... and then use a cast

Additional context

The same principle should be applied to user.UserMetadata

Thanks for reaching out. AppMetadata and UserMetadata are both dynamic because they are in fact dynamic.

You should be able to convert the JObject to your own type pretty easily in your own code.

public class MyUserMetadata
{
    public string Color { get; set; }
}

public static class UserExtensions
{
    public static MyUserMetadata GetUserMetadata(this User user)
    {
        return (user.UserMetadata as JObject).ToObject<MyUserMetadata>();
    }
}

The above allows you to call GetUserMetdata() on our User class and have it return your own type. This avoids having to work with dynamics or JObjects through the rest of your code.

You can do the same for AppMetdata.

Closing, as this can be solved in user-land with minimal code and doesn't require changes to our SDK.

I diagree, they are not dynamic.
In fact in Auth0 i can only add JSON, so in AppMetadata should be a JSON structure of some kind.
But as the Type is dynamic i don't know which current type it is.
For now it is a Newtonsoft.Json.Linq.JObject but if you somehow decide to switch to JSON.net or anything else my code breaks at runtime because i relied on AppMetadata being Newtonsoft.Json.Linq.JObject.

If you would provide the .ToObject method on the User class i don't need to know which json implementation you use and can rely that i get my T-instance no matter what.

If you want to open a PR, I am happy to review a PR to implement both GetUserMetadata and GetAppMetadata that does the JSON parsing.