Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
casually-creative opened this issue Mar 20, 2023 · 2 comments

Comments

@casually-creative
Copy link

casually-creative commented Mar 20, 2023

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
Copy link

tiggris commented May 28, 2024

HI!

It looks like ODataQueryExpand should 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
Copy link

tiggris commented May 29, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants