当前位置: 首页 > 知识库问答 >
问题:

在 Swagger 中使用属性 XML 注释作为参数说明

徐文彬
2023-03-14

我使用ASP.NETCore创建了一个Web API,并使用swagger创建留档。我使用APIendpoint上的XML注释在留档中提供附加信息。swagger配置是:

services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });

            // Set the comments path for the Swagger JSON and UI.
            var basePath = AppContext.BaseDirectory;
            var xmlPath = Path.Combine(basePath, "MyAPI.xml");
            c.IncludeXmlComments(xmlPath);
        });

我的一个APIendpoint及其XML注释是:

    /// <summary>
    /// Find an existing appointment using the visitor information: First name, last name, email, phone.
    /// </summary>
    /// <url>http://apiurl/api/appointments/appointmentsByVisitor</url>
    /// <param name="criteria">consists of one or more of:  Firstname, lastname, email, phone</param>
    /// <returns>Existing appointment data in an Appointment object or a business error.</returns>
    /// <response code="200">Returns the existing appointment event.</response>
    /// <response code="400">Returns if no parameters are specified.</response>            
    /// <response code="204">Returns if there's no matching appointment.</response>
    /// <response code="500">Returns if there's an unhandled exception.</response>
    [Authorize]
    [HttpGet("appointmentsByVisitor")]
    [ProducesResponseType(typeof(Appointment), 200)]
    [ProducesResponseType(typeof(BusinessError), 404)]
    public IActionResult AppointmentsByVisitor([FromQuery] VisitorSearchCriteria criteria) {}

VisitorSearchCriteria是一个单独的类,它是APIendpoint所需参数的包装器。

public class VisitorSearchCriteria
{
    /// <summary>
    /// Visitor first name.
    /// </summary>
    public string FirstName { get; set; }

    /// <summary>
    /// Visitor last name.
    /// </summary>
    public string LastName { get; set; }

    // several other properties....
}

此 API 终结点的 swagger 文档将 VisitorSearchCriteria 的所有属性显示为参数,但它不会选取 XML 注释。请参阅下面的屏幕截图。

如您所见,缺少参数的描述。如何告诉swagger使用外部类的XML注释来创建参数描述?

共有2个答案

邓赤岩
2023-03-14

http://wmpratt.com/swagger-and-asp-net-web-api-part-1/

首先,在生成期间启用 XML 文档文件创建。在“解决方案资源管理器”中,右键单击 Web API 项目,然后单击“属性”。单击构建选项卡并导航到输出。确保选中 XML 文档文件。您可以保留默认文件路径。在我的例子中,它的bin\SwaggerDemoApi.XML

国俊艾
2023-03-14

如果您在其他项目中定义了模型类,则需要转到此项目的属性,然后在“生成/输出”下检查“XML文档文件:”选项。然后你需要更新 swagger 配置文件添加。

c.IncludeXmlComments($@"{System.AppDomain.CurrentDomain.BaseDirectory}\YourProjectName.xml");

YourProjectName.xml应该包含xml格式的模型类字段的描述。

如果您想从不同的项目导入注释,则需要执行与上述相同的操作,添加

c.IncludeXmlComments($@"{System.AppDomain.CurrentDomain.BaseDirectory}\YourProjectName2.xml");

到招摇的配置文件。

请记住,每个项目都可以生成一个XML文件,您可以将所有这些文件的注释添加到您的swagger UI中。当您运行Swagger UI时,注释应出现在模型部分。

 类似资料:
  • 我有以下代码,这是我的API的艺术 如果我有@ApiParam注释,@PathVariable就变成非必需的,所以如果我没有输入userId并通过Swagger UI发出请求,请求仍然会转到服务器,这会导致不必要的服务器错误。默认情况下,@Path变量的参数“必需”为true(因此,默认值为@PathVariable(name=“userId”,必需=true)),并且在该参数上没有@ApiPar

  • 我正在使用从以下依赖项导入的Swagger/OpenAPIV3注释创建应用程序的API描述: 其中一个批注是批注,它接受名为的属性,该属性允许字符串数组: 现在,我想使用在枚举类上构造的自定义方法,该方法返回允许的字符串数组,因此不需要在每次向枚举添加类型时添加该方法。以便我们可以这样使用它: 现在这是无法编译的,因为在执行注释时不知道该方法。是否有这样的解决方案允许在SwaggerV3注释属性值

  • 我对这条规则有两个问题: > 在注释函数的最后一个参数中,我找不到正确的语法来将文本字符串与捕获组变量组合在一起。上面的“操作”字段导致以下异常: ParseException:在第34行第72列遇到“+”“+”。我期待其中之一:“)”...“,”... 显然,当我使用“+”将字符串追加到一起时,最后一个参数没有正确地强制转换为字符串。 将最后一个参数中的字符串组合到注释函数中的正确方法是什么?

  • 我试图向自定义注释中注入一个值,但Spring似乎没有进行评估。

  • 我在我的一个项目中有这个注释类。 注释类由我定义。 问题:我可以代替String使用一些属性 ,其值在编译时未知,但仅在运行时未知吗? 作为参考,这里是我的注释类型的定义。

  • 问题内容: 我刚刚开始在Java 8中使用注释,并得到了一些意外的结果。 我有这样的方法: 我编写了一个JUnit测试,为参数searchList传递了空值。我原以为会发生某种类型的错误,但好像没有注释就通过了。这是预期的行为吗?据我了解,这是允许您跳过编写样板空检查代码。 对于@NotNull应该做什么的解释将不胜感激。 问题答案: 和自己做什么。它们应该充当文档工具。 该注释提醒您在以下情况下