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

Possible bug in $expand(EntityName($filter)) #32

Open
mgesc opened this issue Feb 27, 2019 · 3 comments
Open

Possible bug in $expand(EntityName($filter)) #32

mgesc opened this issue Feb 27, 2019 · 3 comments

Comments

@mgesc
Copy link

mgesc commented Feb 27, 2019

Hi Ihar,

I've been testing the filter inside the expand and for my use case i can't get it work. It seem that the $filter string is being bypased on nested entities.

In addition, when trying to use the .ToJson() seems that the "NullHandlingValue.Ignore" option is also being ignored

I've setup a .netfiddle for you to replicate those issues and point me in the right direction if i'm doing something wrong.

https://dotnetfiddle.net/KCbGib

Regards

@IharYakimush
Copy link
Owner

Hi @mgesc ,

Filter in expand and expand itself applicable for collections. In your sample you trying to expand and filter single property "URLT" declared as public virtual URLT URLT { get; set; }. It is not a collection, hence can't be filtered in expand. You can add filter to root level if needed e.g. string filter = "(PDAID eq 1 or PDAID eq 3) and isDeleted eq false and DA/URLT/isDeleted eq true";

Regarding ToJson NullHandlingValue.Ignore I need time to check the root cause. Probably this is because of using SelectExpandWrapperConverter

Thanks,
Ihar

@mgesc
Copy link
Author

mgesc commented Feb 27, 2019

Indeed!! I didn't realize that expand filter only work over collections. Thank you very much, Ihar!!

@mgesc
Copy link
Author

mgesc commented Feb 27, 2019

@IharYakimush I have managed to solve the null handling issue with this helper class. I'll leave it here if it's helpful to you.

It's a very dumb solution but as a hot fix it helps.

string list = list1.ToJson().ToString();
JToken token = JsonHelper.RemoveEmptyChildren(JToken.Parse(list));
object result = JsonConvert.DeserializeObject<object>(token.ToString(), settings);

where settings

JsonSerializerSettings settings = new JsonSerializerSettings()
{
   ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
   NullValueHandling = NullValueHandling.Ignore
};

and JsonHelper

public static class JsonHelper
{
    public static JToken RemoveEmptyChildren(JToken token)
    {
        if (token.Type == JTokenType.Object)
        {
            JObject copy = new JObject();
            foreach (JProperty prop in token.Children<JProperty>())
            {
                JToken child = prop.Value;
                if (child.HasValues)
                {
                    child = RemoveEmptyChildren(child);
                }
                if (!IsEmpty(child))
                {
                    copy.Add(prop.Name, child);
                }
            }
            return copy;
        }
        else if (token.Type == JTokenType.Array)
        {
            JArray copy = new JArray();
            foreach (JToken item in token.Children())
            {
                JToken child = item;
                if (child.HasValues)
                {
                    child = RemoveEmptyChildren(child);
                }
                if (!IsEmpty(child))
                {
                    copy.Add(child);
                }
            }
            return copy;
        }
        return token;
    }

    public static bool IsEmpty(JToken token)
    {
        return (token.Type == JTokenType.Null) ||
         (token.Type == JTokenType.Array && !token.HasValues) ||
         (token.Type == JTokenType.Object && !token.HasValues);
    }
}

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

No branches or pull requests

2 participants