StreamReader reader = File.OpenText("yourswaggerDocfilename");
var doc = reader.ReadToEnd();
var openApiReader = new OpenApiStringReader();
var docDeserialized = openApiReader.ReadFragment<IOpenApiAny>(text, OpenApiSpecVersion.OpenApi2_0, out var _) as Dictionary<string, IOpenApiAny>;
var paths = openApidoc["paths"] as Dictionary<string, IOpenApiAny>;
foreach(var pathItem in paths)
{
var methods = pathItem.Value as Dictionary<string, IOpenApiAny>;
foreach(var methodItem in methods)
{
var operationFields = methodItem.Value as Dictionary<string, IOpenApiAny>;
if (operationFields.ContainsKey("x-vendorExtension"))
{
var moduleNameObject = operationFields["x-vendorExtension"] as OpenApiString;
Console.WriteLine($"{pathItem.Key} : {moduleNameObject.Value}");
}
}
}
你可以用下面的代码来解析swagger Doc
var openApiReader = new OpenApiStringReader();
var docDeserialized = openApiReader.Read(doc,var out _);
1条答案
按热度按时间2ledvvac1#
多亏了@Helen,我才能以如下方式执行此任务:
首先,您应该安装openAPI.NET和openAPI.NET Reader nuget包。
你可以用下面的代码来解析swagger Doc
但是当doc包含任何大于
decimal.MaxValue
的值时,这将抛出OverflowException
,因为这个nugget库在内部使用decimal.Parse()
来解析Doc中的数字。