From df392456b5ae39486728ee693eb927c71f0c3d19 Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Thu, 30 Jun 2022 17:17:38 -0700 Subject: [PATCH 1/2] Default to workload's page size. --- src/readme.graph.md | 3 ++- tools/Custom/ListCmdlet.cs | 36 ++++++++++++++---------------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/readme.graph.md b/src/readme.graph.md index 8d7b62ec83f..94fe7df6792 100644 --- a/src/readme.graph.md +++ b/src/readme.graph.md @@ -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'); diff --git a/tools/Custom/ListCmdlet.cs b/tools/Custom/ListCmdlet.cs index 13660faf15f..5348e22dc2a 100644 --- a/tools/Custom/ListCmdlet.cs +++ b/tools/Custom/ListCmdlet.cs @@ -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; } - /// - /// Default number of items per page. - /// - internal const int DefaultPageSize = 100; - /// /// Maximum number of items per page. /// @@ -101,28 +96,16 @@ public void InitializeCmdlet(ref global::System.Management.Automation.Invocation null)); } - // Move `-Top` parameter to `limit`. - if (invocationInfo.BoundParameters.ContainsKey("Top")) + if (invocationInfo.BoundParameters.ContainsKey("Top") && top > MaxPageSize) { limit = top; } - int currentPageSize = invocationInfo.BoundParameters.ContainsKey("PageSize") ? PageSize : DefaultPageSize; - if (invocationInfo.BoundParameters.ContainsKey("Top") && limit < currentPageSize) - { - currentPageSize = limit; - } - - 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 (limit != default) + if (invocationInfo.BoundParameters.ContainsKey("PageSize") && invocationInfo.BoundParameters.ContainsKey("Top")) { - requiredPages = limit / currentPageSize; - overflowItemsCount = limit % currentPageSize; + limit = top; + invocationInfo.BoundParameters["Top"] = PageSize; + top = PageSize; } if ((!invocationInfo.BoundParameters.ContainsKey("Count")) && invocationInfo.BoundParameters.ContainsKey("CountVariable")) @@ -133,6 +116,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; + } + } + /// /// 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. From 79ab8165806db804e244f228915a045863bb9ae4 Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Fri, 1 Jul 2022 09:16:41 -0700 Subject: [PATCH 2/2] Internally set top when overflow is detected. --- tools/Custom/ListCmdlet.cs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/tools/Custom/ListCmdlet.cs b/tools/Custom/ListCmdlet.cs index 5348e22dc2a..5000e3069b0 100644 --- a/tools/Custom/ListCmdlet.cs +++ b/tools/Custom/ListCmdlet.cs @@ -96,16 +96,25 @@ public void InitializeCmdlet(ref global::System.Management.Automation.Invocation null)); } - if (invocationInfo.BoundParameters.ContainsKey("Top") && top > MaxPageSize) + if (invocationInfo.BoundParameters.ContainsKey("Top")) { - limit = top; - } + if ((top > MaxPageSize) || invocationInfo.BoundParameters.ContainsKey("All") || invocationInfo.BoundParameters.ContainsKey("PageSize")) + { + limit = top; + } - if (invocationInfo.BoundParameters.ContainsKey("PageSize") && invocationInfo.BoundParameters.ContainsKey("Top")) - { - limit = top; - invocationInfo.BoundParameters["Top"] = PageSize; - top = PageSize; + if (top > MaxPageSize) + { + // Remove $top from query parameters, we will handle paging. + top = default; + invocationInfo.BoundParameters.Remove("Top"); + } + + if (invocationInfo.BoundParameters.ContainsKey("PageSize")) + { + invocationInfo.BoundParameters["Top"] = PageSize; + top = PageSize; + } } if ((!invocationInfo.BoundParameters.ContainsKey("Count")) && invocationInfo.BoundParameters.ContainsKey("CountVariable")) @@ -158,7 +167,7 @@ public bool ShouldIteratePages(global::System.Collections.Generic.Dictionary