You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PSUseOutputTypeCorrectly generates false warning for PSObjects with custom ETS type names #195
I have many functions that generate PSObjects. Whenever I create a PSObject, I add a type name to it. This is done either by invoking .PSTypeNames.Add(...), .PSTypeNames.Insert(...), or if I'm creating the object using [pscustomobject]@{...} then I just apply a PSTypeName value to the hashtable with the custom name for the object I am creating.
In scenarios like these, PSUseOutputTypeCorrectly identifies that I'm returning a PSObject and then complains because my OutputType is a string, set to the actual PSTypeName of the object that is returned. I'm following the best practice, but PSScriptAnalyzer is incorrectly assuming I'm doing the wrong thing. The PSUseOutputTypeCorrectly rule should not generate a warning on functions that return PSObjects when the OutputType is set to a string (the ETS type name for those PSObjects).
I just found another scenario where this occurs. I have a command that returns an array of PSObjects. This warning indicates that my command returns an object of type System.Object[], but that type is not declared in the OutputType attribute. My OutputType properly identifies the ETS type name here as well, as a single quoted string. Please make sure this is addressed for collections as well.
Drop that into a file, save it, and run Invoke-ScriptAnalyzer against it and you'll see the false positives. This rule needs to allow for custom ETS type names in the OutputType attribute, which are defined as string since they are not actually instances of System.Type.
So I just checked and it doesn't get the type in this case because for some reason, System.Type.GetType("System.Management.Automation.PSObject") returns null (really weird).
Activity
KirkMunro commentedon May 22, 2015
I just found another scenario where this occurs. I have a command that returns an array of PSObjects. This warning indicates that my command returns an object of type System.Object[], but that type is not declared in the OutputType attribute. My OutputType properly identifies the ETS type name here as well, as a single quoted string. Please make sure this is addressed for collections as well.
quoctruong commentedon Jun 1, 2015
Hi Kirk. Thanks for identifying the bugs. Do you mind sharing a snippet of the code for this bug?
KirkMunro commentedon Jun 1, 2015
Sure thing. Here you go:
Drop that into a file, save it, and run Invoke-ScriptAnalyzer against it and you'll see the false positives. This rule needs to allow for custom ETS type names in the OutputType attribute, which are defined as string since they are not actually instances of System.Type.
quoctruong commentedon Jun 3, 2015
So does this case only apply to PSCustomObject with the property PSTypeName? Are there any other similar cases as well?
KirkMunro commentedon Jun 3, 2015
Yes, it seems so. Here are two more cases where it is working as expected and not generating false positives (whether intentional or not):
Based on those results, it seems this is specific to when PSTypeName is used to define the custom type name.
quoctruong commentedon Jun 3, 2015
In these cases, it doesn't generate warning because the VariableAnalysis was not able to detect the type of the value returned.
KirkMunro commentedon Jun 3, 2015
How about this one?
The type of that object should be identifiable, so is it properly checking the type and giving it a green light or failing to detect the type?
quoctruong commentedon Jun 3, 2015
So I just checked and it doesn't get the type in this case because for some reason, System.Type.GetType("System.Management.Automation.PSObject") returns null (really weird).
quoctruong commentedon Jun 15, 2015
Closed with #243