ZEXSM / OData.QueryBuilder

OData.QueryBuilder - library for creating complex OData queries (OData version 4.01) based on data models with linq syntax.
MIT License
71 stars 31 forks source link

Multiple expands, nested in another expand, produce a wrong query string. #110

Open casually-creative opened 1 year ago

casually-creative commented 1 year ago

When nesting multiple expands inside another expand, the resulting odata query string contains multiple $expand tags in that nest, whilst they should be grouped into one $expand with comma seperated entity references.

Here is an invented code sample:

string oDataQuery = new ODataQueryBuilder()
    .For<RootEntity>("root_entity")
    .ByList()
    .Expand(er => er
        .For<MainRelatedEntity>(root => root.SomeListProperty)
        .Expand(em => em
            .For<SubRelatedEntity1>(main => main.SubProperty1)
            .Select(sub1 => sub1.some_property)
        )
        .Expand(em => em
            .For<SubRelatedEntity2>(main => main.SubProperty2)
            .Select(sub2 => sub2.some_property)
        )
    )
    .ToUri()
    .ToString();

The result of this looks like:

root_entity?$expand=SomeListProperty($expand=SubRelatedEntity1($select=some_property);$expand=SubRelatedEntity2($select=some_property))

while it should be this:

root_entity?$expand=SomeListProperty($expand=SubRelatedEntity1($select=some_property),SubRelatedEntity2($select=some_property))

When sending the first request to my oData endpoint, it obviously ignores the first sub expand, and only takes into account the last one.


When expanding multiple relations on the root level, so without the nesting inside the MainRelatedEntity, then it works as expected. The key here is that they are nested.

tiggris commented 1 month ago

HI!

It looks like ODataQueryExpandshould be implemented with _hasMultyExpands:

public IODataQueryExpand<TEntity> Expand(Expression<Func<TEntity, object>> expandNested)
{
    var query = new ODataOptionExpandExpressionVisitor().ToQuery(expandNested);

    Expand(query);

    return this;
}

public IODataQueryExpand<TEntity> Expand(Action<IODataExpandResource<TEntity>> expandNested)
{
    var builder = new ODataExpandResource<TEntity>(_odataQueryBuilderOptions);

    expandNested(builder);

    Expand(builder.Query);

    return this;
}

...

private IODataQueryExpand<TEntity> Expand<T>(T query) where T : class
{
    if (_hasMultyExpands)
    {
        _stringBuilder.Merge(ODataOptionNames.Expand, QuerySeparators.Nested, $"{QuerySeparators.Comma}{query}");
    }
    else
    {
        _stringBuilder.Append($"{ODataOptionNames.Expand}{QuerySeparators.EqualSign}{query}{QuerySeparators.Nested}");
    }

    _hasMultyExpands = true;

    return this;
}
tiggris commented 1 month ago

@ZEXSM I've created a fix - please let me know if you would like me to share it :)