From d9dd8bad6bd397a55fe9656a3dc7b451d709cf8f Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 9 Apr 2023 21:52:41 +0200 Subject: [PATCH 01/10] simplify CheckReturnType --- .../WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index adfb4f1a610..f166bbe8613 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -166,11 +166,8 @@ private static RestReturnType CheckReturnType(HttpResponseMessage response) RestReturnType rt = RestReturnType.Detect; string contentType = ContentHelper.GetContentType(response); - if (string.IsNullOrEmpty(contentType)) - { - rt = RestReturnType.Detect; - } - else if (ContentHelper.IsJson(contentType)) + + if (ContentHelper.IsJson(contentType)) { rt = RestReturnType.Json; } From b5a5601a77d85f27080257d5708378d0f06deb5c Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 9 Apr 2023 21:54:46 +0200 Subject: [PATCH 02/10] remove var --- .../WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index f166bbe8613..76e12a948ce 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -79,7 +79,7 @@ internal override void ProcessResponse(HttpResponseMessage response) if (ShouldWriteToPipeline) { - using var responseStream = new BufferingStreamReader(baseResponseStream); + using BufferingStreamReader responseStream = new(baseResponseStream); // First see if it is an RSS / ATOM feed, in which case we can // stream it - unless the user has overridden it with a return type of "XML" @@ -264,7 +264,7 @@ private static bool TryConvertToXml(string xml, out object doc, ref Exception ex XmlReaderSettings settings = GetSecureXmlReaderSettings(); XmlReader xmlReader = XmlReader.Create(new StringReader(xml), settings); - var xmlDoc = new XmlDocument(); + XmlDocument xmlDoc = new(); xmlDoc.PreserveWhitespace = true; xmlDoc.Load(xmlReader); From 60b148b1e78875708d705e2902c4139fe64a4aa9 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 9 Apr 2023 21:58:57 +0200 Subject: [PATCH 03/10] encoding.HeaderName is never null or empty --- .../Common/InvokeRestMethodCommand.Common.cs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 76e12a948ce..7961ff46df6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -100,18 +100,8 @@ internal override void ProcessResponse(HttpResponseMessage response) object obj = null; Exception ex = null; - string encodingVerboseName; - try - { - encodingVerboseName = string.IsNullOrEmpty(encoding.HeaderName) ? encoding.EncodingName : encoding.HeaderName; - } - catch (NotSupportedException) - { - encodingVerboseName = encoding.EncodingName; - } - // NOTE: Tests use this verbose output to verify the encoding. - WriteVerbose(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"Content encoding: {encodingVerboseName}")); + WriteVerbose(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"Content encoding: {encoding.HeaderName}")); bool convertSuccess = false; From 6122df500202e68d81daf64fcef16bc457ecf53f Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 9 Apr 2023 22:23:30 +0200 Subject: [PATCH 04/10] reorder --- .../Common/InvokeRestMethodCommand.Common.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 7961ff46df6..89a56eaf29a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -89,21 +89,20 @@ internal override void ProcessResponse(HttpResponseMessage response) } else { - // Determine the response type - RestReturnType returnType = CheckReturnType(response); - // Try to get the response encoding from the ContentType header. string charSet = WebResponseHelper.GetCharacterSet(response); string str = StreamHelper.DecodeStream(responseStream, charSet, out Encoding encoding); - object obj = null; - Exception ex = null; - // NOTE: Tests use this verbose output to verify the encoding. WriteVerbose(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"Content encoding: {encoding.HeaderName}")); - + + // Determine the response type + RestReturnType returnType = CheckReturnType(response); + bool convertSuccess = false; + object obj = null; + Exception ex = null; if (returnType == RestReturnType.Json) { From d81b18e13c241b8ca07d7fb9af34db32ab790ed0 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 9 Apr 2023 22:25:12 +0200 Subject: [PATCH 05/10] capitalize comments --- .../WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 89a56eaf29a..720b7aaf0fb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -108,7 +108,7 @@ internal override void ProcessResponse(HttpResponseMessage response) { convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex); } - // default to try xml first since it's more common + // Default to try xml first since it's more common else { convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex); @@ -116,7 +116,7 @@ internal override void ProcessResponse(HttpResponseMessage response) if (!convertSuccess) { - // fallback to string + // Fallback to string obj = str; } From ec6014a28bfcf27aab189283b117ea17049c0d9e Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 9 Apr 2023 22:33:39 +0200 Subject: [PATCH 06/10] out ErrorRecord --- .../utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 720b7aaf0fb..4b54e5e4bbe 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -273,8 +273,7 @@ private static bool TryConvertToJson(string json, out object obj, ref Exception bool converted = false; try { - ErrorRecord error; - obj = JsonObject.ConvertFromJson(json, out error); + obj = JsonObject.ConvertFromJson(json, out ErrorRecord error); if (obj == null) { From 0a4325275b680d22a9c485066f4b1983109b7b1a Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 9 Apr 2023 22:40:14 +0200 Subject: [PATCH 07/10] remove base --- .../WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 4b54e5e4bbe..1a1f3d05905 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -32,9 +32,9 @@ public class InvokeRestMethodCommand : WebRequestPSCmdlet [Alias("FL")] public SwitchParameter FollowRelLink { - get => base._followRelLink; + get => _followRelLink; - set => base._followRelLink = value; + set => _followRelLink = value; } /// @@ -45,9 +45,9 @@ public SwitchParameter FollowRelLink [ValidateRange(1, int.MaxValue)] public int MaximumFollowRelLink { - get => base._maximumFollowRelLink; + get => _maximumFollowRelLink; - set => base._maximumFollowRelLink = value; + set => _maximumFollowRelLink = value; } /// From f2dff3974b4f7ac831f55c2c7266b2b8eabe4d42 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 12 Apr 2023 09:38:29 +0200 Subject: [PATCH 08/10] revert base --- .../WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 8930a7440f7..3bb4654c18e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -33,9 +33,9 @@ public class InvokeRestMethodCommand : WebRequestPSCmdlet [Alias("FL")] public SwitchParameter FollowRelLink { - get => _followRelLink; + get => base._followRelLink; - set => _followRelLink = value; + set => base._followRelLink = value; } /// @@ -46,9 +46,9 @@ public SwitchParameter FollowRelLink [ValidateRange(1, int.MaxValue)] public int MaximumFollowRelLink { - get => _maximumFollowRelLink; + get => base._maximumFollowRelLink; - set => _maximumFollowRelLink = value; + set => base._maximumFollowRelLink = value; } /// From 53c23b02fdd6a839492e6d83d019ca9cbeef88dc Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 8 May 2023 23:06:30 +0200 Subject: [PATCH 09/10] fix nullable --- .../WebCmdlet/Common/InvokeRestMethodCommand.Common.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index f66b84588b7..550511b9d53 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -106,8 +106,8 @@ internal override void ProcessResponse(HttpResponseMessage response) RestReturnType returnType = CheckReturnType(response); bool convertSuccess = false; - object obj = null; - Exception ex = null; + object? obj = null; + Exception? ex = null; if (returnType == RestReturnType.Json) { @@ -159,7 +159,7 @@ private static RestReturnType CheckReturnType(HttpResponseMessage response) ArgumentNullException.ThrowIfNull(response); RestReturnType rt = RestReturnType.Detect; - string contentType = ContentHelper.GetContentType(response); + string? contentType = ContentHelper.GetContentType(response); if (ContentHelper.IsJson(contentType)) { From 45ec2abef49b1d22a793d56a3901c700efb7b54c Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 10 May 2023 14:27:44 +0200 Subject: [PATCH 10/10] update code --- .../Common/InvokeRestMethodCommand.Common.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 550511b9d53..df1ef750529 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -99,8 +99,18 @@ internal override void ProcessResponse(HttpResponseMessage response) string str = StreamHelper.DecodeStream(responseStream, characterSet, out Encoding encoding, _cancelToken.Token); + string encodingVerboseName; + try + { + encodingVerboseName = encoding.HeaderName; + } + catch + { + encodingVerboseName = string.Empty; + } + // NOTE: Tests use this verbose output to verify the encoding. - WriteVerbose(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"Content encoding: {encoding.HeaderName}")); + WriteVerbose(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"Content encoding: {encodingVerboseName}")); // Determine the response type RestReturnType returnType = CheckReturnType(response);