你有没有找到答案?今天我想做同样的事情,所以我使用了来自 MSFT 的 AutoRest 开源项目,https://github.com/Azure/autorest。虽然它看起来像是为生成客户端代码而设计的(使用 swagger 文档记录的 API 的代码),但在生成此代码的过程中,它必须完全按照您在问题中提出的要求 - 解析 Swagger 文件并了解 API 支持的操作、输入和输出。
事实上,我们可以得到这些信息——AutoRest 公开地公开了这些信息。
所以使用 nuget 来安装 AutoRest。然后添加对 AutoRest.core 和 AutoRest.Model.Swagger 的引用。到目前为止,我只是简单地去了:
using Microsoft.Rest.Generator;
using Microsoft.Rest.Generator.Utilities;
using System.IO;
...
var settings = new Settings();
settings.Modeler = "Swagger";
var mfs = new MemoryFileSystem();
mfs.WriteFile("AutoRest.json", File.ReadAllText("AutoRest.json"));
mfs.WriteFile("Swagger.json", File.ReadAllText("Swagger.json"));
settings.FileSystem = mfs;
var b = System.IO.File.Exists("AutoRest.json");
settings.Input = "Swagger.json";
Modeler modeler = Microsoft.Rest.Generator.Extensibility.ExtensionsLoader.GetModeler(settings);
Microsoft.Rest.Generator.ClientModel.ServiceClient serviceClient;
try
{
serviceClient = modeler.Build();
}
catch (Exception exception)
{
throw new Exception(String.Format("Something nasty hit the fan: {0}", exception.Message));
}
您要解析的 swagger 文档名为 Swagger.json,位于您的 bin 目录中。您可以从他们的 GitHub (https://github.com/Azure/autorest/tree/master/AutoRest/AutoRest.Core.Tests/Resource) 获取 AutoRest.json 文件。我不是 100% 确定它是如何使用的,但似乎需要告知该工具什么是支持的。两个 JSON 文件都需要在您的 bin 中。
serviceClient 对象就是你想要的。它将包含有关方法、模型类型、方法组的信息
让我知道这是否有效。你可以用他们的资源文件试试。我在玩耍时使用了他们的 ExtensionLoaderTests 作为参考(https://github.com/Azure/autorest/blob/master/AutoRest/AutoRest.Core.Tests/ExtensionsLoaderTests.cs)。
(也感谢 AutoRest 的作者 Denis)