Autorest.PowerShell:生成模块失败,因为生成的变量无效

Autorest.PowerShell: build module failed because invalid variable generated

提问人:sorosh_sabz 提问时间:9/22/2023 最后编辑:sorosh_sabz 更新时间:11/2/2023 访问量:41

问:

当我想从我的 swagger 生成 PowerShell 模块时

我尝试运行并查看下面的日志Generate-PowerShellClient.ps1

   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.

如上所述,autorest PowerShell 扩展生成我的模块没有任何错误

然后当我尝试用 构建这个模块时,我得到了以下错误build-module.ps1

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 '$' 

经过一些调查,我发现我的错误存在,因为 autorest powershell 模块在 中使用变量,如下所示$hostIpTables.Api\Source\BSN.IpTables.Cli\generated\generated\api\BsnIPTablesCli.cs

                var _url = new global::System.Uri($"{$host}{pathAndQuery}");

而这种形式的变量在C中不存在,格式无效#

BsnIPTablesCli.cs 是由 autorest.powershell 生成的,所以我想通过 autorest.powershell 以某种方式纠正这个问题,但我不知道该怎么做?

我的代码存在于 https://github.com/BSVN/IpTables.Api/pull/22

C# PowerShell Swagger 生成 自动休息

评论

0赞 filimonic 9/22/2023
所以。。为什么不在任何文本编辑器中使用 replace 替换为空值之类的其他内容呢?{$host}
0赞 sorosh_sabz 9/22/2023
@filimonic是由 所以我想通过 autorest.powershell 以某种方式纠正这个问题BsnIPTablesCli.csautorest.powershell

答:

0赞 sorosh_sabz 11/2/2023 #1

正如您在构建模块中所看到的,由于无效变量在 autorest.powershell 上生成了 GitHub 票证,dolauli 说:

@soroshsabz既然您没有提供 swagger,我想根源是您没有在 swagger 中指定主机。这是一个例子。

因此,如果您想纠正此问题,您需要将 和 为此添加,如您在 如何在 C# 的 swagger json 文件中添加主机变量中所示,您需要在初始化 swagger 的 swagger 生成选项中添加以下代码,如下所示hostswagger.json

            // 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" }
                    }
                }
            });