我試圖在我的 Swagger/OpenApi 中插入我自己的值,目前在模型示例值中我有以下值:

想要的情況如下圖:

我已經研究過它并嘗試了多種方法來實作這一點,例如我嘗試添加這樣的 XMLComments:

但是,這不起作用。然后我嘗試使用提供此功能的 MapType 函式,我已使用以下代碼完成此操作:
c.MapType<Student>(() => new Schema()
{
example = new Student()
{
StudentName = "John Doe",
Age = 28
}
});
但是,當我以這種方式嘗試時,得到以下結果:

有任何解決這個問題的方法嗎 ?
uj5u.com熱心網友回復:
使用 NuGet 包Swashbuckle.AspNetCore.Filters如下:
添加您的默認模型(您打算在 swagger 中顯示的默認值),如下所示:
public class StudentDtoDefault : IExamplesProvider<StudentDto>
{
public StudentDto GetExamples()
{
return new StudentDto
{
StudentName = "John Doe",
Age = 28
};
}
}
請注意,它正在實作我發送到 API 控制器的主模型IExamplesProvider<StudentDto>在哪里StudentDto。
這就是我們應該如何將它應用于我們的控制器操作:
[SwaggerRequestExample(typeof(StudentDto), typeof(StudentDtoDefault))]
[HttpPost]
public ActionResult Post([FromBody] StudentDto student)
{
...
}
有關更多資訊和示例,請訪問該專案的GitHub 存盤庫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/381825.html
上一篇:函式未被呼叫指定次數
