Skip to content

Commit

Permalink
feat: allow @odata.count to be written after enumeration OData#2594
Browse files Browse the repository at this point in the history
  • Loading branch information
uffelauesen committed Jan 13, 2023
1 parent 31712cb commit 89145de
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/Microsoft.OData.Core/JsonLight/ODataJsonLightWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,10 @@ await this.instanceAnnotationWriter.WriteInstanceAnnotationsAsync(

if (this.writingResponse)
{
// Write the count if it's available.
await this.WriteResourceSetCountAsync(resourceSet.Count, null)
.ConfigureAwait(false);

// Write the next link if it's available.
await this.WriteResourceSetNextLinkAsync(resourceSet.NextPageLink, /*propertyName*/null)
.ConfigureAwait(false);
Expand Down Expand Up @@ -1704,6 +1708,9 @@ await this.asynchronousJsonWriter.EndArrayScopeAsync()
// Write the next link if it's available.
await this.WriteResourceSetNextLinkAsync(resourceSet.NextPageLink, propertyName)
.ConfigureAwait(false);

await this.WriteResourceSetCountAsync(resourceSet.Count, propertyName)
.ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -2736,7 +2743,11 @@ await this.asynchronousJsonWriter.EndObjectScopeAsync()
/// <returns>A task that represents the asynchronous write operation.</returns>
private Task WriteResourceSetCountAsync(long? count, string propertyName)
{
if (count.HasValue)
bool countWritten = this.State == WriterState.ResourceSet ?
this.CurrentResourceSetScope.CountWritten :
this.CurrentDeltaResourceSetScope.CountWritten;

if (count.HasValue && !countWritten)
{
return WriteResourceSetCountInnerAsync(count.Value, propertyName);

Expand All @@ -2756,6 +2767,15 @@ await this.asynchronousODataAnnotationWriter.WritePropertyAnnotationNameAsync(

await this.asynchronousJsonWriter.WriteValueAsync(innerCount)
.ConfigureAwait(false);

if (this.State == WriterState.ResourceSet)
{
this.CurrentResourceSetScope.CountWritten = true;
}
else
{
this.CurrentDeltaResourceSetScope.CountWritten = true;
}
}
}

Expand Down Expand Up @@ -3060,6 +3080,9 @@ await this.asynchronousJsonWriter.WriteValueAsync(UriUtils.UriToString(link.Targ
/// </summary>
private sealed class JsonLightResourceSetScope : ResourceSetScope
{
/// <summary>true if the odata.count was already written, false otherwise.</summary>
private bool countWritten;

/// <summary>true if the odata.nextLink was already written, false otherwise.</summary>
private bool nextLinkWritten;

Expand All @@ -3085,6 +3108,22 @@ internal JsonLightResourceSetScope(ODataResourceSet resourceSet, IEdmNavigationS
this.isUndeclared = isUndeclared;
}

/// <summary>
/// true if the odata.count annotation was already written, false otherwise.
/// </summary>
internal bool CountWritten
{
get
{
return this.countWritten;
}

set
{
this.countWritten = value;
}
}

/// <summary>
/// true if the odata.nextLink annotation was already written, false otherwise.
/// </summary>
Expand Down Expand Up @@ -3373,6 +3412,9 @@ public JsonLightDeltaLinkScope(WriterState state, ODataItem link, ODataResourceS
/// </summary>
private sealed class JsonLightDeltaResourceSetScope : DeltaResourceSetScope
{
/// <summary>true if the odata.count was already written, false otherwise.</summary>
private bool countWritten;

/// <summary>true if the odata.nextLink was already written, false otherwise.</summary>
private bool nextLinkWritten;

Expand All @@ -3392,6 +3434,22 @@ public JsonLightDeltaResourceSetScope(ODataDeltaResourceSet resourceSet, IEdmNav
{
}

/// <summary>
/// true if the odata.count annotation was already written, false otherwise.
/// </summary>
internal bool CountWritten
{
get
{
return this.countWritten;
}

set
{
this.countWritten = value;
}
}

/// <summary>
/// true if the odata.nextLink annotation was already written, false otherwise.
/// </summary>
Expand Down

0 comments on commit 89145de

Please sign in to comment.