Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/readme.graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,8 @@ directive:
let odataNextLinkRegex = /(^\s*)(while\s*\(\s*_nextLink\s*!=\s*null\s*\))/gmi
if($.match(odataNextLinkRegex)) {
// Add custom -PageSize parameter to *_List cmdlets that support Odata next link.
$ = $.replace(odataNextLinkRegex, '$1while (_nextLink != null && this.ShouldIteratePages(this.InvocationInformation.BoundParameters, result.Value.Length))\n$1');
let initializePageCountPlaceholder = 'this.InitializePageCount(result.Value.Length);'
$ = $.replace(odataNextLinkRegex, `$1${initializePageCountPlaceholder}\n$1while (_nextLink != null && this.ShouldIteratePages(this.InvocationInformation.BoundParameters, result.Value.Length))$1`);

let psBaseClassImplementationRegex = /(\s*:\s*)(global::System.Management.Automation.PSCmdlet)/gmi
$ = $.replace(psBaseClassImplementationRegex, '$1Microsoft.Graph.PowerShell.Cmdlets.Custom.ListCmdlet');
Expand Down
49 changes: 25 additions & 24 deletions tools/Custom/ListCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ public partial class ListCmdlet : global::System.Management.Automation.PSCmdlet
[global::System.Management.Automation.Alias("CV")]
public string CountVariable { get => this._countVariable; set => this._countVariable = value; }

/// <summary>
/// Default number of items per page.
/// </summary>
internal const int DefaultPageSize = 100;

/// <summary>
/// Maximum number of items per page.
/// </summary>
Expand Down Expand Up @@ -101,28 +96,25 @@ public void InitializeCmdlet(ref global::System.Management.Automation.Invocation
null));
}

// Move `-Top` parameter to `limit`.
if (invocationInfo.BoundParameters.ContainsKey("Top"))
{
limit = top;
}

int currentPageSize = invocationInfo.BoundParameters.ContainsKey("PageSize") ? PageSize : DefaultPageSize;
if (invocationInfo.BoundParameters.ContainsKey("Top") && limit < currentPageSize)
{
currentPageSize = limit;
}
if ((top > MaxPageSize) || invocationInfo.BoundParameters.ContainsKey("All") || invocationInfo.BoundParameters.ContainsKey("PageSize"))
{
limit = top;
}

if (invocationInfo.BoundParameters.ContainsKey("PageSize") || invocationInfo.BoundParameters.ContainsKey("Top") || invocationInfo.BoundParameters.ContainsKey("All")){
// Explicitly set `-Top` parameter to currentPageSize in order for the generated cmdlets to construct a URL with a `$top` query parameter.
invocationInfo.BoundParameters["Top"] = currentPageSize;
top = currentPageSize;
}
if (top > MaxPageSize)
{
// Remove $top from query parameters, we will handle paging.
top = default;
invocationInfo.BoundParameters.Remove("Top");
}

if (limit != default)
{
requiredPages = limit / currentPageSize;
overflowItemsCount = limit % currentPageSize;
if (invocationInfo.BoundParameters.ContainsKey("PageSize"))
{
invocationInfo.BoundParameters["Top"] = PageSize;
top = PageSize;
}
}

if ((!invocationInfo.BoundParameters.ContainsKey("Count")) && invocationInfo.BoundParameters.ContainsKey("CountVariable"))
Expand All @@ -133,6 +125,15 @@ public void InitializeCmdlet(ref global::System.Management.Automation.Invocation
}
}

public void InitializePageCount(int initialPageSize)
{
if (limit != default && initialPageSize != default)
{
requiredPages = limit / initialPageSize;
overflowItemsCount = limit % initialPageSize;
}
}

/// <summary>
/// Determines whether the cmdlet should follow the OData next link URL.
/// Iteration will only occur when limit/top is not set, or if there a more items to fetch.
Expand Down Expand Up @@ -166,7 +167,7 @@ public bool ShouldIteratePages(global::System.Collections.Generic.Dictionary<str
}
else
{
nextLinkUri.Query += $"$top=" + global::System.Uri.EscapeDataString(overflowItemsCount.ToString());
nextLinkUri.Query += $"&$top={System.Uri.EscapeDataString(overflowItemsCount.ToString())}";
}
}
return nextLinkUri.Uri;
Expand Down