microsoft / RulesEngine

A Json based Rules Engine with extensive Dynamic expression support
https://microsoft.github.io/RulesEngine/
MIT License
3.47k stars 528 forks source link

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

Open n-rowe opened 5 months ago

n-rowe commented 5 months ago

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.

abbasc52 commented 5 months ago

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

n-rowe commented 5 months ago

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

asulwer commented 1 week ago

Newtonsoft should infer the type from json data