我正在将一个旧版Web应用程序中的SOAP API转换为REST。
我从一个webapi项目(C#)开始--用swagger和swashbuckle设置它。我已经向它添加了端点,用XML注解进行了注解。这是使用完整的框架,而不是.NET核心。
所有注解都按预期显示在swagger-ui中(XML文档生成工作正常,文件由swagger通过config拾取)。
这个项目引用了旧的SOAP API类(用VB.NET编写的)。我尝试过用RequiredAttribute
(例如)注解这些类,并向它们添加XML注解,但这些注解根本没有在swagger-ui中显示。它们设置在一个非常浅的类层次结构中(它们都继承自基类-为了完整起见,我包括了这些信息)。
这些其他VB.NET项目确实启用了XML文档,我看到了XML文件,其中包含我在webapi项目bin目录中添加的文档,正如预期的那样。我已经将这些文档添加到了swagger配置中,我相信它们被正确加载了(因为加载页面时没有异常)。
但是,swagger-ui中的模型都没有显示我添加的任何DataAnnotation
属性,也没有显示这些VB .NET项目中的任何XML文档。
我想让他们出现,以及有他们在swagger-ui的模块部分列出(这是不存在的,在这个时候)。
我错过了什么?
更新:
我已经用一个外部库(C#,完整的框架)做了一些测试,使用了一个测试模型。
这正如预期的那样工作-发现XML文档和DataAnnotation
属性并显示在swagger-ui上,没有问题。
我现在怀疑VB .NET,虽然这可能是一个问题与VB .NET项目是WCF项目。
更新:
对一个新的VB.NET类库也做了同样的工作,一切都正常工作™,所以......可能是类层次结构或其他VB项目的一些东西(可能不是简单的类库或其他东西)。
更新:
这不是继承权。
2条答案
按热度按时间svmlkihl1#
原来VB .NET类都用好的旧的
SerializableAttribute
标记。这会导致模型在swagger-ui中看起来完全不同,因此没有一个注解(
DataAnnotation
属性和XML注解)能够正常工作。这也导致了所有属性在它们的名称前面都出现了下划线(我也很好奇,忘记在我的问题中提到)。
为了解决我的实际问题,我在this GitHub issue上找到了一个解决方案。
也就是说,确保使用正确的JSON格式化程序(在
Global.asax.cs
中):ibrsph3r2#
这个问题有点晚了,但希望这能帮助那些遇到这个问题的人。答案适用于使用Swagger(NSwag)的.NET Framework +4. 5。
使用WebAPI 2时,您需要在WebApiConfof.cs文件中启用UseXmlSerializer:
然后,您将需要向SwaggerConfig.cs文件中添加一个架构过滤器
然后创建架构过滤器,该过滤器遍历所有架构元素,以查找所有已使用[XmlAttribute]属性进行注解的属性。在本例中,我们还设置了[JsonProperty]属性:
这些都可以在SwaggerConfig类中完成。