2

I have an ODATA query that pulls the first record from the Dynamics AX table and ignores the $filter clause when the $top parameter is the first parameter. However, it pulls the correct record for the $filter clause when the $top parameter is at the end of the query.

Question: How do I make the OData server honor the full query no matter where the $top parameter is placed?

the following two cases return identical results:

Case 1: https://url/data/Users?$top=1

{
     "@odata.context":"https://url/data/$metadata#Users","value":[
     {
        "@odata.etag":"W/\"J5zEx4NDg33OD2YyM4TEs5NTY6zNz7E0ND7U3Nic=\"",
        "Id":"first_user_in_the_table",
        "Alias":"first_user_in_the_table@domain.com",
        "Name":"first_user_in_the_table"
     }
   ]
 }

Case 2: https://url/data/Users?$top=1&$filter=Alias%20eq%20%27specific_user@domain.com%27

{
    "@odata.context":"https://url/data/$metadata#Users","value":[
    {
        "@odata.etag":"W/\"J5zEx4NDg33OD2YyM4TEs5NTY6zNz7E0ND7U3Nic=\"",
        "Id":"first_user_in_the_table",
        "Alias":"first_user_in_the_table@domain.com",
        "Name":"first_user_in_the_table"
    }
  ]
}

However, when I move the $top parameter to the end of the query string I get the correct result.

Case 3: https://url/data/Users?$filter=Alias%20eq%20%27specific_user@domain.com%27&$top=1

{
    "@odata.context":"https://url/data/$metadata#Users","value":[
    {
        "@odata.etag":"W/\"Jz3kz4Nj5g2N6zcz7MCw81Nj9M3M2TU3w4NT4c54Jw==\"",
        "Id":"specific_user",
        "Alias":"specific_user@domain.com",
        "Name":"The Specific User I Actually Want"
    }
  ]
}
sucrerey
  • 21
  • 3

1 Answers1

2

As far as I understand the OData specification, this is incorrect behavior. I base this on 11.2 Requesting Data which states that the $top query option should be evaluated last and after the $filter query option. Also section 2. Query Options of the OData ABNF construction rules seems to indicate that the query options may appear in any order in the URL.

This indicates that this particular behavior may have been incorrectly implemented. You will probably not be able to solve this short of implementing a custom service that wraps the existing one to reorder the query options as needed. I would also assume that this impacts the behavior of all Odata services.

I suggest you create a support request with Microsoft so they can adress this.

FH-Inway
  • 4,432
  • 1
  • 20
  • 37