RulesEngine icon indicating copy to clipboard operation
RulesEngine copied to clipboard

Utils.GetTypedObject fails to type lists of objects with nullable values

Open n-rowe opened this issue 1 year ago • 3 comments

The Utils.GetTypedObject used by the RuleParameter seems to fail to properly type my ExpandoObject when I pass it in.

Consider the following models:

public class UserModel {
   public string Name { get; set }

   public string Id { get; set; }
}
public class RoleModel {
   public string Role { get; set }

   public string Id { get; set; }
}

public class WorkflowModel {
   public IEnumerable<UserModel> ResponsibleUsers { get; set; }
  
   public IEnumerable<RoleModel> ResponsibleRoles { get; set; }
}

public class MyModel {
    public ICollection<WorkflowModel> Workflows { get; set; }
}

The workflows collection in MyModel contains several workflows, each of which can either have a list of ResponsibleUsers or ResponsibleRoles.

If the first workflow only has ResponsibleUsers then the remaining workflows cannot have ResponsibleRoles because Utils.GetTypedObject only looks at the first item in a collection for types.

{
   "workflows": [
      {
         "responsibleUsers": [
            { "name": "John", "id": "j-12" }
         ],
         "responsibleRoles": null
      },
      {
         "responsibleUsers": null,
         "responsibleRoles": [
            { "role": "Admin", "id": "adm" }
         ]
      }
   ]
}

The above JSON when put through a RuleParameter and then accessed again would look similar to the below:

{
   "workflows": [
      {
         "responsibleUsers": [
            { "name": "John", "id": "j-12" }
         ],
         "responsibleRoles": null
      },
      {
         "responsibleUsers": null,
         "responsibleRoles": [
            { }
         ]
      }
   ]
}

This is because as explained above, the first workflow does not have any responsible roles, there is no type specified for other workflows from now on.

n-rowe avatar Jan 22 '24 16:01 n-rowe

@n-rowe may i know why are you using ExpandoObject and not directly a typed object?

abbasc52 avatar Jan 26 '24 15:01 abbasc52

The value is gathered from json so we don't have a solid type for it

n-rowe avatar Jan 26 '24 15:01 n-rowe

Newtonsoft should infer the type from json data

asulwer avatar Jun 21 '24 21:06 asulwer