Skip to content

Fix/Improve TruncatedCollectionOfT #1492

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

WanjohiSammy
Copy link
Member

No description provided.

@WanjohiSammy WanjohiSammy marked this pull request as draft June 25, 2025 17:39

bool isTruncated = false;
int count = 0;
await foreach (var item in source.Take(checked(capacity + 1)).WithCancellation(cancellationToken).ConfigureAwait(false))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand. Isn't it strange to enumerate an IAsyncENumerable to put it in a list and then put it back in a new IAsyncEnumerable?

Shouldn't we stream the data completely? From what I understood, the only real problem is that the count was accessed at the beginning, but this is no longer the case since #1393

Shouldn't we introduce a TrucatedEnumerable instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rpallares for pointing this out. I have updated it. Could you take another look.

@WanjohiSammy WanjohiSammy requested a review from Copilot June 26, 2025 08:25
Copilot

This comment was marked as outdated.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors TruncatedCollection<T> to use composition instead of inheriting from List<T>, adds support for asynchronous truncation via IAsyncEnumerable<T>, and introduces corresponding static Create methods and tests.

  • Refactored TruncatedCollection<T> to implement IReadOnlyList<T> and IAsyncEnumerable<T>, with internal factory methods
  • Introduced TruncatedAsyncEnumerable<T> and TruncationState to support async enumeration and truncation tracking
  • Expanded unit tests to cover the new sync, queryable, and async creation methods
  • Updated project file to include System.Linq.AsyncEnumerable package for async LINQ extensions

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
test/.../TruncatedCollectionOfTTest.cs Added tests for Create overloads (sync, queryable, async)
src/.../Query/Container/TruncatedCollectionOfT.cs Refactored class to use composition, added async support
src/.../Query/Container/TruncatedAsyncEnumerableOfT.cs New async enumerable type for truncation
src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.csproj Added reference to System.Linq.AsyncEnumerable preview package
Comments suppressed due to low confidence (2)

test/Microsoft.AspNetCore.OData.Tests/Query/Container/TruncatedCollectionOfTTest.cs:55

  • [nitpick] Test method naming is inconsistent with the existing CtorTruncatedCollection_SetsProperties pattern. Consider standardizing on a Create_*_SetsProperties or Ctor_*_SetsProperties convention for clarity.
    public void TruncatedCollectionCreateForIEnumerable_SetsProperties()

src/Microsoft.AspNetCore.OData/Query/Container/TruncatedCollectionOfT.cs:189

  • The XML doc comment has a duplicated /// ///. It should be corrected to a single /// <param...> for proper tooling support.
    /// /// <param name="totalCount">The total count. Default null.</param>

/// <summary>
/// Used to track the truncation state of an async enumerable.
/// </summary>
public class TruncationState
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have this a separate class instead of a boolean field of the TruncatedAsyncEnumerable class?

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

Successfully merging this pull request may close these issues.

3 participants