0
我在使用windows' tutorial的Web API項目中獲取json
文件時沒有問題。使用Swashbuckle生成yaml swagger
由於某些要求,我希望能夠檢索yaml
文件。但問題是我沒有找到能夠實現這一目標的機會。
有沒有人知道任何解決方法這個問題?
我在使用windows' tutorial的Web API項目中獲取json
文件時沒有問題。使用Swashbuckle生成yaml swagger
由於某些要求,我希望能夠檢索yaml
文件。但問題是我沒有找到能夠實現這一目標的機會。
有沒有人知道任何解決方法這個問題?
的選擇將是一個IDocumentFilter添加到您的項目,這裏有幾個樣本:
private class YamlDocumentFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
string file = AppDomain.CurrentDomain.BaseDirectory + "swagger_yaml.txt";
if (!File.Exists(file))
{
var serializer = new YamlSerializer();
serializer.SerializeToFile(file, swaggerDoc);
}
}
}
...
private class YamlDocumentFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
string file = AppDomain.CurrentDomain.BaseDirectory + "swagger.yaml";
if (!File.Exists(file))
{
var serializer = new YamlDotNet.Serialization.Serializer();
using (var writer = new StringWriter())
{
serializer.Serialize(writer, swaggerDoc);
var stream = new StreamWriter(file);
stream.WriteLine(writer.ToString());
}
}
}
}
但如果加上取決於你的項目對YamlSerializer或YamlDotNet的附加參考是可以接受的。
這似乎是正確的路要走,我還沒有能夠驗證它。但這可能是因爲我正在部署到服務結構。 – Manny42
arghhh! (***服務結構***)是的,你可能需要修改該例子或嘗試不同的依賴... – HelderSepu
我添加了一個使用YamlSerializer的示例,代碼看起來更簡單,我驗證了它它適用於Azure:http://swashbuckletest.azurewebsites.net/swagger_yaml.txt – HelderSepu