When I want to generate PowerShell module from my swagger
I try to run Generate-PowerShellClient.ps1
and see below logs
BSN.IpTables.Cli Feature/22-add-cli≡ ~1 .\Generate-PowerShellClient.ps1
changed 1 package in 222ms
AutoRest code generation utility [cli version: 3.6.3; node: v18.15.0]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
info | Loading AutoRest core 'C:\Users\sooro\.autorest\@[email protected]\nodemodules\@autorest\core\dist' (3.9.7)
info |
The default version of @autorest/powershell has been bumped from 2.1+ to 3.0+.
> If you still want to use 2.1+ version, please specify it with --use:@autorest/[email protected].{x}, e.g 2.1.401.
info | Loading AutoRest extension '@autorest/powershell' (4.0.648->4.0.648)
info | Loading AutoRest extension '@autorest/modelerfour' (~4.26.0->4.26.2)
verbose | [1.47 s] Reading OpenAPI 3.0 file file:///D:/Source/Repos/Resa/IpTables.Api/Source/BSN.IpTables.Api/swagger.json
warning | PreCheck/CheckDuplicateSchemas | Checking for duplicate schemas, this could take a (long) while. Run with --verbose for more detail.
verbose | [1.63 s] Found and removed 0 duplicate schema
warning | UsingTemporaryFlag | modelerfour.treat-type-object-as-anything options is a temporary flag. It WILL be removed in the future.
verbose | [1.78 s] ModelerFour/api-version-mode:client
verbose | [1.78 s] ModelerFour/api-version-parameter:constant
verbose | [1.78 s] ModelerFour/use-model-namespace:false
info | Autorest completed in 2.54s. 211 files generated.
as you can see above, autorest PowerShell extension generate my module without any errors
then When I try to build this module with build-module.ps1
, I got below errors
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(63,54): error CS1525: Invalid expression term '' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(63,54): error CS1073: Unexpected token '$' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(63,54): error CS1056: Unexpected character '$' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(116,54): error CS1525: Invalid expression term '' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(116,54): error CS1073: Unexpected token '$' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(116,54): error CS1056: Unexpected character '$' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(179,54): error CS1525: Invalid expression term '' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(179,54): error CS1073: Unexpected token '$' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(179,54): error CS1056: Unexpected character '$' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(242,54): error CS1525: Invalid expression term '' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(242,54): error CS1073: Unexpected token '$' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(242,54): error CS1056: Unexpected character '$' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(295,54): error CS1525: Invalid expression term '' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(295,54): error CS1073: Unexpected token '$' [D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\BsnIPTablesCli.csproj]
D:\Source\Repos\Resa\IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs(295,54): error CS1056: Unexpected character '$'
After I some investigation, I found my errors exist because autorest powershell module use $host
variable in IpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs
, like below
var _url = new global::System.Uri($"{$host}{pathAndQuery}");
and this form of variable does not exist and invalid format in C#
BsnIPTablesCli.cs
is generated by autorest.powershell
so I want to correct this problem with some way by autorest.powershell and I do not how to do it?
My code exist in https://github.com/BSVN/IpTables.Api/pull/22
As you can see in build module failed because invalid variable generated GitHub ticket on autorest.powershell, dolauli says:
@soroshsabz since you did not provide the swagger, I guess the root is you did not specify host in the swagger. And here is an example.
So if you want to correct this problem, you need to add host
into the swagger.json
and for doing this as you can see in How to add host variable in swagger json file at C# you need to add below code in swagger generation options on initializing swagger like below
// Add host to generated swagger file (swagger.json)
options.AddServer(new OpenApiServer {
Url = "http://{serverAddress}",
Description = "IpTables Test Server",
Variables = new Dictionary<string, OpenApiServerVariable>
{
{ "serverAddress", new OpenApiServerVariable {
Default = "192.168.21.56:8080",
Description = "Target Server Address" }
}
}
});