simple-odata-client / Simple.OData.Client

MIT License
331 stars 197 forks source link

Dynamics 365 Function Parameters (need to remove quote) #931

Open Slackter opened 8 months ago

Slackter commented 8 months ago

Hello,

We are utilizing the latest version of this library very heavily without any issues at all so it's been great. We are in need of executing a function and need to specify a function parameter in a very specific format.

The function requires a parameter of Target=@tgt (@tgt can be anything) but there must be NO quotes at all.

If I run use the following code: client.For<SystemUser>.Key(userGuid).Function("RetrievePrincipalAccess").Set(new { Target = "@tgt })

the library will generate a URL as follows: https://server/api/data/systemusers(userGuid)/Microsoft.Dynamics.CRM.RetrievePrincipalAccess(Target='@tgt')

when it needs to be: https://server/api/data/systemusers(userGuid)/Microsoft.Dynamics.CRM.RetrievePrincipalAccess(Target=@tgt)

Is there any way to remove the single quotes the Set() is adding to my URL?

I was able to get this working by using the BuildRequestFor() and modifying the RequestUri directly, but I am unable to write proper unit tests since the ODataRequest has only internal constructors and is un-mockable.

ExpDev07 commented 2 weeks ago

I ran into the same issue, the following solved it for me (though it would be nice of they supported a SerializationOption for the key to control this behavior):

public class AbstractApi(HttpClient httpClient, Uri relativeUri) : ODataClient(new ODataClientSettings(httpClient, relativeUri)
{
    IgnoreUnmappedProperties = true,
    BeforeRequest = m => m.RequestUri = m.RequestUri.RemoveUnsupportedQuotes()
});

public static class AbstractApiExtensions
{
    public static Uri? RemoveUnsupportedQuotes(this Uri? uri)
    {
        if (uri == null)
        {
            return uri;
        }

        var newUri = uri.ToString();
        newUri = newUri.Replace("(%27", "(");
        newUri = newUri.Replace("%27)", ")");

        return new Uri(newUri);
    }
}