Share via

Azure APIM - Advanced Policies - Control Flow - Unexpected Result

juni dev 361 Reputation points
2022-12-19T11:10:51.193+00:00

Hi,

Hi have this expression evaluating as False with a json PATCH payload having {"accountEnabled" : false}
I really expect it to evaluate as True

What is wrong in this expression?

<when condition="@(context.Request.MatchedParameters.ContainsKey("accountEnabled") && (context.Request.Body.As<JObject>(true)["accountEnabled"].Value<bool>() == false))">

Note: The accountEnabled parameter is optional and I need to consider the possibility of it not being present in the request

TRACE:
choose (0.020 ms)
{
"message": "Expression was successfully evaluated.",
"expression": "context.Request.MatchedParameters.ContainsKey(\"accountEnabled\") && (context.Request.Body.As<JObject>(true)[\"accountEnabled\"].Value<bool>() == false)",
"value": false
}

Thanks,
JD

Azure API Management
Azure API Management

An Azure service that provides a hybrid, multi-cloud management platform for APIs.

0 comments No comments
{count} votes

Answer accepted by question author
  1. JananiRamesh-MSFT 29,441 Reputation points Moderator
    2022-12-20T04:23:12.39+00:00

    Hi @juni dev As mentioned in the previous thread the logic seems to be incorrect
    <set-variable name="IsAccountDisabling" value="@(context.Request.Body.As<JObject>(true)["accountEnabled"].Value<bool>() == false)" /> This will set false if founds true as the value of accountEnabled.
    You're comparing the value of accountEnabled (true) with false, that will return false and will set value false in the variable IsAccountDisabling That's why the condition never met:

    <when condition="@(((bool)context.Variables["IsAccounPresent"]) && ((bool)context.Variables["IsAccountDisabling"]))"> where ((bool)context.Variables["IsAccountDisabling"]) will always return false.

    please find the working policy which satisfies all the condition as per your requirement,

    272304-image.png

    Hope this helps! Try and do let me know if you have any queries.

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.