Enable more nullable annotations in WebCmdlets#19359
Enable more nullable annotations in WebCmdlets#19359iSazonov merged 42 commits intoPowerShell:masterfrom
Conversation
...hell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs
Outdated
Show resolved
Hide resolved
...hell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs
Show resolved
Hide resolved
...hell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs
Outdated
Show resolved
Hide resolved
...hell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs
Outdated
Show resolved
Hide resolved
...rosoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs
Outdated
Show resolved
Hide resolved
…Cmdlet/Common/ContentHelper.Common.cs Co-authored-by: Ilya <darpa@yandex.ru>
...rosoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs
Outdated
Show resolved
Hide resolved
...erShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs
Outdated
Show resolved
Hide resolved
...erShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs
Outdated
Show resolved
Hide resolved
| bool result = false; | ||
| try | ||
| { | ||
| ArgumentException.ThrowIfNullOrEmpty(characterSet); |
There was a problem hiding this comment.
Hmm, I think it is wrong. Encoding.GetEncoding(null) returns OS default but Encoding.GetEncoding(string.Empty) throw. We should investigate the last case and if it is a bug we should fix in another PR.
There was a problem hiding this comment.
I think the desired behaviour is that both cases (characterSet == null; characterSet == string.Empty) should should return result = false and encoding = UTF8 (the default). Do you agree?
There was a problem hiding this comment.
The code is in try-catch. So it makes no sense. Please remove.
There was a problem hiding this comment.
I know it doesn't make sense, I added it to avoid a possible null reference error for characterSet, could you point me to a better solution?
There was a problem hiding this comment.
Null reference error? In TryGetEncoding? It is impossible since Encoding.GetEncoding(null) returns OS default, for string.Empty try-catch will catch and set default too. Also it will set default if .Net doesn't know a value in characterSet.
There was a problem hiding this comment.
The warning before the last 2 commits was Possible null reference argument for parameter 'name' in 'Encoding Encoding.GetEncoding(string name)'., and the build fails on warnings
There was a problem hiding this comment.
Since [System.Text.Encoding]::GetEncoding($null) return OS default without exception I suggest to open new issue in .Net.
Today we could use a workaround with ! operator and comment with reference to the new .Net issue.
There was a problem hiding this comment.
I looked a bit more into it
Test:
$source = @"
using System;
using System.Text;
public class EncodingTest
{
public static Encoding GetEncoding1(string name)
{
return Encoding.GetEncoding(name);
}
public static Encoding GetEncoding2(int name)
{
return Encoding.GetEncoding(name);
}
}
"@
Add-Type -TypeDefinition $source -Language CSharp -ReferencedAssemblies netstandard, System.Text.Encoding
[EncodingTest]::GetEncoding1($null)
#--> ERROR
[EncodingTest]::GetEncoding2($null)
#--> windows-1252
[EncodingTest]::GetEncoding1("")
#--> ERROR
[EncodingTest]::GetEncoding2("")
#--> windows-1252
[EncodingTest]::GetEncoding2(0)
#--> windows-1252In our case we are using Encoding.GetEncoding(string name) so it throws an error with null or empty that gets caught, we don't have a bug everything works as planned. Unfortunalely Encoding.GetEncoding(string name) with null gives us a warning, as you said we could use ! operator.
If Encoding.GetEncoding(string name) with name == null returned windows-1252 that wouldn't be a desired behaviour for us in this case, because we want to default to UTF-8
....PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebResponseHelper.CoreClr.cs
Outdated
Show resolved
Hide resolved
|
Unrelated error |
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
|
@iSazonov can you merge this PR? |
|
@CarloToso Please look test fails. |
|
@iSazonov all tests pass |
|
🎉 Handy links: |
PR Summary
Enable more nullable comments in WebCmdlets, please review carefully.
PR Context
Follow up to #19291
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).