Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Exception while de-serializing Uris #1630

Closed
nik-net opened this issue Apr 13, 2020 · 3 comments
Closed

[Bug] Exception while de-serializing Uris #1630

nik-net opened this issue Apr 13, 2020 · 3 comments

Comments

@nik-net
Copy link

nik-net commented Apr 13, 2020

If the stored value in a document is URI, then this code statement throws error (class BsonMapper.cs)

private void DeserializeDictionary(Type K, Type T, IDictionary dict, BsonDocument value)
        {
            var isKEnum = K.GetTypeInfo().IsEnum;
            foreach (var el in value.GetElements())
            {
               
                    var k = isKEnum ? Enum.Parse(K, el.Key) : Convert.ChangeType(el.Key, K); //Convert.ChangeType will throw error if K is Uri and el.Key is a string such as https://dev.azure.com
                    var v = this.Deserialize(T, el.Value);
                    dict.Add(k, v);
               }
        }

Modifying this line to

var k = isKEnum ? Enum.Parse(K, el.Key) : K == typeof(Uri) ? new Uri(el.Key) : Convert.ChangeType(el.Key, K);

works.

@lbnascimento
Copy link
Collaborator

@nik-net This is not a bug in LiteDB, but a limitation in Convert.ChangeType: all this method does is try to cast an object to another type, and there is no valid cast from string to Uri (LiteDB serializes Uri as their string representation). It is possible to create a Uri from a string with the Uri constructor, and that is what your second example does, but ChangeType is not aware of that.

@nik-net
Copy link
Author

nik-net commented Apr 14, 2020

@nik-net This is not a bug in LiteDB, but a limitation in Convert.ChangeType: all this method does is try to cast an object to another type, and there is no valid cast from string to Uri (LiteDB serializes Uri as their string representation). It is possible to create a Uri from a string with the Uri constructor, and that is what your second example does, but ChangeType is not aware of that.

Thank you, yes I understand that this is due to ChangeType's inability to convert a string to it's URI representation. Given this known limitation would you be looking to handle this edge case in the code?

@lbnascimento
Copy link
Collaborator

@nik-net Sorry, for whatever reason I didn't realize the DeserializeDictionary was in our BsonMapper, I thought it was some code of yours. I'll add your fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants