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

ArgumentException when fetching db item with missing Enum value #2514

Open
ionite34 opened this issue Jun 24, 2024 · 2 comments
Open

ArgumentException when fetching db item with missing Enum value #2514

ionite34 opened this issue Jun 24, 2024 · 2 comments
Labels

Comments

@ionite34
Copy link

Version
LiteDB version: 5.0.19
.NET: 8.0
OS: Occurs across our users in Win 11 / macOS / Linux

Describe the bug
ArgumentException occurs when fetching litedb entries with properties of enums that have names that no longer exist or were changed. Is there a way for the bson deserializer to return 0 or some default enum value when this happens?

Code to Reproduce

  1. First saving value:
using LiteDB;

var tempDir = Path.Combine(Path.GetTempPath(), "litedb-test-e31c667160d8");
Directory.CreateDirectory(tempDir);

using var db = new LiteDatabase(Path.Combine(tempDir, "test.db"));

var collection = db.GetCollection<Cat>("Cat");

collection.Upsert(new Cat { Id = 100, Color = Color.Green });

public enum Color
{
    Red = 0,
    Green = 1,
    Blue = 2
}

public class Cat
{
    public int Id { get; set; }
    public Color Color { get; set; }
}
  1. Finding item with missing enum value:
using LiteDB;

var tempDir = Path.Combine(Path.GetTempPath(), "litedb-test-e31c667160d8");
Directory.CreateDirectory(tempDir);

using var db = new LiteDatabase(Path.Combine(tempDir, "test.db"));

var collection = db.GetCollection<Cat>("Cat");

var cat = collection.FindById(100); // Error here
Console.WriteLine(cat?.Color);

public enum Color
{
    Red = 0,
    // Green = 1,
    Blue = 2
}

public class Cat
{
    public int Id { get; set; }
    public Color Color { get; set; }
}

Exception:

System.ArgumentException: Requested value 'Green' was not found.
   at System.Enum.TryParseByName[TStorage](RuntimeType enumType, ReadOnlySpan`1 value, Boolean ignoreCase, Boolean throwOnFailure, TStorage& result)
   at System.Enum.TryParseByValueOrName[TUnderlying,TStorage](RuntimeType enumType, ReadOnlySpan`1 value, Boolean ignoreCase, Boolean throwOnFailure, TUnderlying& result)
   at System.Enum.TryParse(Type enumType, ReadOnlySpan`1 value, Boolean ignoreCase, Boolean throwOnFailure, Object& result)
   at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
   at LiteDB.BsonMapper.Deserialize(Type type, BsonValue value)
   at LiteDB.BsonMapper.DeserializeObject(EntityMapper entity, Object obj, BsonDocument value)
   at LiteDB.BsonMapper.Deserialize(Type type, BsonValue value)
   at LiteDB.LiteQueryable`1.<ToEnumerable>b__27_2(BsonDocument x)
   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   at LiteDB.LiteCollection`1.FindById(BsonValue id)
   ...
@ionite34 ionite34 added the bug label Jun 24, 2024
@JKamsker
Copy link
Collaborator

JKamsker commented Jun 24, 2024

What, in your opinion, would be the expected result?
As far as I can see, LiteDB serializes Enums as string and when you are trying to parse the string into an enum value, that doesnt exist, it fails.

@ionite34
Copy link
Author

What, in your opinion, would be the expected result?
As far as I can see, LiteDB serializes Enums as string and when you are trying to parse the string into an enum value, that doesnt exist, it fails.

I agree the error by default seems to be standard since I think behavior in System.Text.Json is the same. I'm wondering if a config option for UnknownEnumAsDefault like EnumAsInteger for BsonMapper is available to deal with this globally? Or can I implement that myself with BsonMapper.Global.RegisterType ?

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

No branches or pull requests

2 participants