swagger-api / swagger-api/swagger-codegen

[Powershell] Bug with "New-<ClassName>" cmdlets in Model folder.

Open
#8,066 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

We have used the Swagger Codegen to generate the Powershell SDK and C# SDK, C# SDK is placed inside the Powershell SDK and the generated powershell cmdlets internally invokes the C# methods.

We have observed few issues with the generated cmdlets

  1. When "New-<ClassName>" cmdlets are used we get errors, while digging deeper we have observed that there is a mismatch between the number of arguments in the constructor of the class (<ClassName>) defined in C# SDK and the number of arguments with which the Powershell SDK is trying to call the same constructor inside "New-<ClassName>" cmdlets.

  2. In "New-<ClassName>" cmdlets in Model folder, some of the parameter types are defined as "System.Nullable", so when we run the cmdlet getting the error message as "'System.Nullable`1[T]' violates the constraint of type 'T'". If we remove the "System.Nullable" then we are able to proceed.

  3. Powershell by default converts all Null into [String]::Empty for String Parameters, So when a object is returned by running the Cmdlets, then the string parameters are set to [String]::Empty instead of NULL value. So we had to manually add the following code in the cmdlet file to overcome the problem for all the string parameters.

if($Moid -eq [String]::Empty)
{
$Moid = [NullString]::Value
}

SDK Link:
https://cdn.intersight.com/components/an-apidocs/1.0.5-99/model/IntersightPowerShellSDK.tar.gz

Swagger-codegen version

Version: 2.2.3

Swagger declaration file content or url

https://cdn.intersight.com/components/an-apidocs/1.0.5-99/model/restapi.json

Command line used for generation

Powershell SDK Generation
java -jar /usr/local/bin/swagger-codegen-cli.jar generate -i ../doc/rest/api/restapi-v2.yaml -DapiTests=false,apiDocs=false,modelTests=false,modelDocs=false -c codegen/config_powershell.json -l powershell -o ../sdk/PowerShellSDK

C# SDK Generation
java -jar /usr/local/bin/swagger-codegen-cli.jar generate -i ../doc/rest/api/restapi-v2.yaml -DapiTests=false,apiDocs=false,modelTests=false,modelDocs=false -c codegen/config_powershell.json -l csharp -o ../sdk/PowerShellSDK/csharp/swaggerClient/

Content of config_powershell.json file

{
"packageName" : "intersight",
"packageVersion" : "1.0.0"
}

Steps to reproduce
  1. Download the SDK from the link given above.
  2. Open Powershell Window
  3. run "./build.ps1" from the SDK folder.
  4. Run "Import-Module .\src\intersight\intersight.psd1"
  5. Run "New-ComputeBlade"
Related issues/PRs

None

Suggest a fix/enhancement

None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing config_powershell.json and reproducing the failures with build.ps1, Import-Module .\src\intersight\intersight.psd1, and New-ComputeBlade. Trace the generated New- model cmdlets and their C# constructors; done means the reported argument, nullable-type, and null-string problems are reproducibly resolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, powershell
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.