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

[RedisIdField] issue when using type different then string, GUID or Ulid #286

Closed
Zlejsi opened this issue Jan 9, 2023 · 7 comments
Closed

Comments

@Zlejsi
Copy link

Zlejsi commented Jan 9, 2023

When using different type then string, GUID or Ulid at [RedisIdField] attribute, program throw an exception.
C# System.ArgumentException: 'Object of type 'System.String' cannot be converted to type 'System.Int32'.'
As example i used System.Int32 as type.

Problem is at converting Id generated by GenerateId() method.

class RedisObjectHandler.cs

internal static string SetId(this object obj)
    {
      Type type = obj.GetType();
      DocumentAttribute customAttribute = Attribute.GetCustomAttribute((MemberInfo) type, typeof (DocumentAttribute)) as DocumentAttribute;
      PropertyInfo propertyInfo = ((IEnumerable<PropertyInfo>) type.GetProperties()).FirstOrDefault<PropertyInfo>((Func<PropertyInfo, bool>) (x => Attribute.GetCustomAttribute((MemberInfo) x, typeof (RedisIdFieldAttribute)) != null));
      string str = customAttribute != null ? customAttribute.IdGenerationStrategy.GenerateId() : throw new MissingMemberException("Missing Document Attribute decoration");
      if (propertyInfo != (PropertyInfo) null)
      {
        Type propertyType = propertyInfo.PropertyType;
        if (!((IEnumerable<Type>) new Type[3]
        {
          typeof (string),
          typeof (Guid),
          typeof (Ulid)
        }).Contains<Type>(propertyType) && !propertyType.IsValueType)
          throw new InvalidOperationException("Software Defined Ids on objects must either be a string, ULID, Guid, or some other value type.");
        object obj1 = propertyInfo.GetValue(obj);
        if (!RedisObjectHandler.TypeDefaultCache.ContainsKey(propertyType))
          RedisObjectHandler.TypeDefaultCache.Add(propertyType, Activator.CreateInstance(propertyType));
        if (obj1?.ToString() != RedisObjectHandler.TypeDefaultCache[propertyType]?.ToString())
          str = propertyInfo.GetValue(obj).ToString();
        else if (propertyType == typeof (Guid))
          propertyInfo.SetValue(obj, (object) Guid.Parse(str));
        else if (propertyType == typeof (Ulid))
          propertyInfo.SetValue(obj, (object) Ulid.Parse(str));
        else
         propertyInfo.SetValue(obj, (object) str);
      }
      return customAttribute.Prefixes == null || string.IsNullOrEmpty(((IEnumerable<string>) customAttribute.Prefixes).FirstOrDefault<string>()) ? type.FullName + ":" + str : ((IEnumerable<string>) customAttribute.Prefixes).First<string>() + ":" + str;
    }

FIXED by using propertyInfo.SetValue(obj, Convert.ChangeType(str,info.PropertyType),null);

internal static string SetId(this object obj)
    {
      Type type = obj.GetType();
      DocumentAttribute customAttribute = Attribute.GetCustomAttribute((MemberInfo) type, typeof (DocumentAttribute)) as DocumentAttribute;
      PropertyInfo propertyInfo = ((IEnumerable<PropertyInfo>) type.GetProperties()).FirstOrDefault<PropertyInfo>((Func<PropertyInfo, bool>) (x => Attribute.GetCustomAttribute((MemberInfo) x, typeof (RedisIdFieldAttribute)) != null));
      string str = customAttribute != null ? customAttribute.IdGenerationStrategy.GenerateId() : throw new MissingMemberException("Missing Document Attribute decoration");
      if (propertyInfo != (PropertyInfo) null)
      {
        Type propertyType = propertyInfo.PropertyType;
        if (!((IEnumerable<Type>) new Type[3]
        {
          typeof (string),
          typeof (Guid),
          typeof (Ulid)
        }).Contains<Type>(propertyType) && !propertyType.IsValueType)
          throw new InvalidOperationException("Software Defined Ids on objects must either be a string, ULID, Guid, or some other value type.");
        object obj1 = propertyInfo.GetValue(obj);
        if (!RedisObjectHandler.TypeDefaultCache.ContainsKey(propertyType))
          RedisObjectHandler.TypeDefaultCache.Add(propertyType, Activator.CreateInstance(propertyType));
        if (obj1?.ToString() != RedisObjectHandler.TypeDefaultCache[propertyType]?.ToString())
          str = propertyInfo.GetValue(obj).ToString();
        else if (propertyType == typeof (Guid))
          propertyInfo.SetValue(obj, (object) Guid.Parse(str));
        else if (propertyType == typeof (Ulid))
          propertyInfo.SetValue(obj, (object) Ulid.Parse(str));
        else
          propertyInfo.SetValue(obj, Convert.ChangeType(str,info.PropertyType),null);
      }
      return customAttribute.Prefixes == null || string.IsNullOrEmpty(((IEnumerable<string>) customAttribute.Prefixes).FirstOrDefault<string>()) ? type.FullName + ":" + str : ((IEnumerable<string>) customAttribute.Prefixes).First<string>() + ":" + str;
    }
@slorello89
Copy link
Member

@Zlejsi - can you share some code that reproduces this error?

Thanks,

@Zlejsi
Copy link
Author

Zlejsi commented Jan 10, 2023

@slorello89 - here is TestRepo with error
https://github.com/Zlejsi/TestRedisOM.git

@Zlejsi Zlejsi closed this as completed Jan 10, 2023
@Zlejsi Zlejsi reopened this Jan 11, 2023
@zulander1
Copy link
Contributor

zulander1 commented Jan 12, 2023

@Zlejsi , try changing the Id from int to String! GenerateId is a string and your Id is int..

@slorello89
Copy link
Member

@zulander1 put their finger on it, the IdGenerationStratagey API is an advanced use-case but it's intended to provide a string id to use in the key.

@Zlejsi
Copy link
Author

Zlejsi commented Feb 2, 2023

Ok. I understand that it is advanced use case, but as i wrote before, you provide a way to set RedisIdField not only as string, but as different types also. I only paste my way to fix the issue to use field with integer value in it.

@zulander1
Copy link
Contributor

@slorello89 maybe we should do a generic IIdGenerationStrategy and return as T, this should resolve this issue

zulander1 added a commit to zulander1/redis-om-dotnet that referenced this issue Feb 5, 2023
Fixed type casting issue for redis#286
@slorello89
Copy link
Member

Should be fixed by #302

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

Successfully merging a pull request may close this issue.

3 participants