Custom ordering error when using lookup IDs

If you have a modeled API endpoint that has the AllowCustomFilters option set to true and you send an ordering with a lookup Id along with the filters, you will get an error.

This is what the endpoint setup looks like.

And this is what is sent in the payload.

{
    "filter": "[...]",
    "ordering": [
        {
            "columnId": "refSomeField",
            "direction": "ASC",
            "lookupId": "d1dbee29-66a5-42b3-a7e2-e38bacc22b5c"
        }
    ]
}

If you don’t send a lookup ID, everything works fine.

The error on the server is this:

System.ArgumentException: An item with the same key has already been added. Key: 0 (Parameter 'key')
   at System.Collections.Generic.SortedList2.Add(TKey key, TValue value)
   at Origam.DA.Service.AbstractSqlCommandGenerator.RenderDataStructureColumn(DataStructure ds, DataStructureEntity entity, Hashtable replaceParameterTexts, Hashtable dynamicParameters, DataStructureSortSet sortSet, Hashtable selectParameterReferences, Boolean isInRecursion, Boolean forceDatabaseCalculation, String& group, SortedList2 order, Boolean& groupByNeeded, ColumnsInfo columnsInfo, DataStructureColumn column, LookupOrderingInfo orderingInfo, FilterCommandParser filterCommandParser, OrderByCommandParser orderByCommandParser, Nullable1 rowOffset) in C:\Users\jindr\Repos\origam\backend\Origam.DA.Service\Generators\AbstractSqlCommandGenerator.cs:line 3000
   at Origam.DA.Service.AbstractSqlCommandGenerator.RenderSelectColumns(SelectParameters selectParameters, StringBuilder sqlExpression, StringBuilder orderByBuilder, StringBuilder groupByBuilder, Hashtable replaceParameterTexts, Hashtable selectParameterReferences, Boolean isInRecursion, Boolean concatScalarColumns, Boolean forceDatabaseCalculation, FilterCommandParser filterCommandParser, OrderByCommandParser orderByCommandParser) in C:\Users\jindr\Repos\origam\backend\Origam.DA.Service\Generators\AbstractSqlCommandGenerator.cs:line 2422
   at Origam.DA.Service.AbstractSqlCommandGenerator.SelectSql(SelectParameters selectParameters, Hashtable replaceParameterTexts, Hashtable selectParameterReferences, Boolean restrictScalarToTop1, Boolean isInRecursion, Boolean forceDatabaseCalculation, FilterCommandParser filterCommandParser, OrderByCommandParser orderByCommandParser) in C:\Users\jindr\Repos\origam\backend\Origam.DA.Service\Generators\AbstractSqlCommandGenerator.cs:line 1272
   at Origam.DA.Service.AbstractSqlCommandGenerator.BuildCommands(IDbDataAdapter adapter, SelectParameters selectParameters, Boolean forceDatabaseCalculation) in C:\Users\jindr\Repos\origam\backend\Origam.DA.Service\Generators\AbstractSqlCommandGenerator.cs:line 396
   at Origam.DA.Service.AbstractSqlCommandGenerator.CreateDataAdapter(SelectParameters adParameters, Boolean forceDatabaseCalculation) in C:\Users\jindr\Repos\origam\backend\Origam.DA.Service\Generators\AbstractSqlCommandGenerator.cs:line 183
   at Origam.DA.Service.AbstractDataService.GetAdapterNonCached(SelectParameters adParameters) in C:\Users\jindr\Repos\origam\backend\Origam.DA.Service\AbstractDataService.cs:line 211
   at Origam.DA.Service.AbstractDataService.GetAdapter(SelectParameters selectParameters, UserProfile userProfile) in C:\Users\jindr\Repos\origam\backend\Origam.DA.Service\AbstractDataService.cs:line 140
   at Origam.DA.Service.AbstractSqlDataService.ExecuteDataReader(DataStructureQuery query, IPrincipal principal, String transactionId) in C:\Users\jindr\Repos\origam\backend\Origam.DA.Service\AbstractSqlDataService.cs:line 1836
   at Origam.DA.Service.AbstractSqlDataService.ExecuteDataReaderInternal(DataStructureQuery query)+MoveNext() in C:\Users\jindr\Repos\origam\backend\Origam.DA.Service\AbstractSqlDataService.cs:line 1939
   at System.Linq.Enumerable.SelectEnumerableIterator2.MoveNext()
   at Origam.Server.Pages.XsltPageRequestHandler.LoadWithFilters(XsltDataPage xsltPage, Dictionary2 parameters, IRequestWrapper request) in C:\Users\jindr\Repos\origam\backend\Origam.Server\Pages\XsltPageRequestHandler.cs:line 333
   at Origam.Server.Pages.XsltPageRequestHandler.Execute(AbstractPage page, Dictionary2 parameters, IRequestWrapper request, IResponseWrapper response) in C:\Users\jindr\Repos\origam\backend\Origam.Server\Pages\XsltPageRequestHandler.cs:line 99
   at Origam.Server.Pages.UserApiProcessor.Process(IHttpContextWrapper context) in C:\Users\jindr\Repos\origam\backend\Origam.Server\Pages\UserApiProcessor.cs:line 143

It happens here:

So the problem is that the order number of the default sort set is 0 and so is the order number of the custom ordering.

Workaround

To work around this issue, you can set the sort order of the default sort set to a high number, like 1000.


The solution

Maybe we could solve this by adding a model rule forcing the sort order of the default sort sets to be higher than zero. Maybe 10000?

Further investigation

The same code is also executed when interacting with the filters in the standard Origam screen section in the grid view. But this problem doesn’t happen in that case. And that is because we actually add a 1000 to the custom order. This seems suspicious and could be a bug. We should investigate whether the behavior of the filters is actually correct.

1 Like