1

I am using Refit as my rest client but i am facing a problem with this rest api, which has a array of complex objects inside a QueryString parameter.

public async Task<IActionResult> GetHierarchy([FromQuery] SearchParameter<RestHierarchy>[]? searchParameters, int pageNumber = 1, int pageSize = 50)

the SearchParameter class is defined as follow

public class SearchParameter<T>
{
    public string FieldName { get; set; }
    public LogicalOperators LogicalOperator { get; set; }
    public string? Value1 { get; set; }
    public string? Value2 { get; set; }

    [JsonIgnore]
    private Type? FieldType
    {
        get
        {
            Type ClassType = typeof(T);

            var member = ClassType.GetMembers().FirstOrDefault(a => a.Name == FieldName);
            Type type = member is PropertyInfo info ? info.PropertyType : ((FieldInfo)member).FieldType;

            return type;
        }
    }

    public override string ToString()
    {
         ...
    }
}

I am trying to configure Refit client to get a request like this

/hierarchies/GetHierarchy?pageNumber=1&pageSize=10&searchParameters[0].FieldName=Id&searchParameters[0].LogicalOperators=Equals&searchParameters[0].Value1=56334850-7940-4D89-6394-08DB036BBCE0

which is working properly from Postman.

I tried configuring the Refit call as follow

[Get("/Hierarchies/GetHierarchy")]
[Headers("Authorization: Bearer")]
Task<List<RestHierarchy>> GetHierarchyAsync([Query(CollectionFormat.Multi)] SearchParameter<RestHierarchy>[]? searchParameters = null);

but the request is not being constructed properly, as I obtain from Refit a request like this

[89efdeb8-3098-4c33-888a-97f974784823 -   Request]========Start==========
[89efdeb8-3098-4c33-888a-97f974784823 -   Request] GET /api/onestorebackoffice.businessRules/Hierarchies/GetHierarchy?Length=1&LongLength=1&Rank=1&SyncRoot=Id%20%3D%3D%20%228efee3e4-e187-4ec4-04d7-08db039b4f5b%22&IsReadOnly=False&IsFixedSize=True&IsSynchronized=False https/1.1
[89efdeb8-3098-4c33-888a-97f974784823 -   Request] Host: https://localhost
[89efdeb8-3098-4c33-888a-97f974784823 -   Request] Authorization: Bearer ...
[89efdeb8-3098-4c33-888a-97f974784823 -   Request] Duration: 00:00:05.2867317
[89efdeb8-3098-4c33-888a-97f974784823 -   Request]==========End==========

Note that if I modify the Rest API using a single SearchParameter, all works properly.

Can anyone help me solving this problem? Thanks

0 Answers0