Swagger未从其他项目中找到XML和DataAnnotation

k4ymrczo  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(173)

我正在将一个旧版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项目的一些东西(可能不是简单的类库或其他东西)。
更新:
这不是继承权。

svmlkihl

svmlkihl1#

原来VB .NET类都用好的旧的SerializableAttribute标记。
这会导致模型在swagger-ui中看起来完全不同,因此没有一个注解(DataAnnotation属性和XML注解)能够正常工作。
这也导致了所有属性在它们的名称前面都出现了下划线(我也很好奇,忘记在我的问题中提到)。
为了解决我的实际问题,我在this GitHub issue上找到了一个解决方案。
也就是说,确保使用正确的JSON格式化程序(在Global.asax.cs中):

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new DefaultContractResolver();
ibrsph3r

ibrsph3r2#

这个问题有点晚了,但希望这能帮助那些遇到这个问题的人。答案适用于使用Swagger(NSwag)的.NET Framework +4. 5。
使用WebAPI 2时,您需要在WebApiConfof.cs文件中启用UseXmlSerializer:

ublic static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        // ...

        // Web API routes
        //...

        // You will need this to get the pipeline to produce XML attributes 
        // instead of XML elements
        config.Formatters.XmlFormatter.UseXmlSerializer = true;
    }
}

然后,您将需要向SwaggerConfig.cs文件中添加一个架构过滤器

// If you want to post-modify "complex" Schemas once they've been 
  // generated, the board or for a
  // specific type, you can wire up one or more Schema filters.
  //
  c.SchemaFilter<XmlAttributeFilter>();

然后创建架构过滤器,该过滤器遍历所有架构元素,以查找所有已使用[XmlAttribute]属性进行注解的属性。在本例中,我们还设置了[JsonProperty]属性:

public class XmlAttributeFilter : ISchemaFilter
{
    public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
    {
        if (schema == null || schema.properties == null || type == null)
            return;

        IEnumerable<PropertyInfo> xmlAttrProperties = type.GetProperties()
                                 .Where(t => t.GetCustomAttribute<XmlAttributeAttribute>() != null);

        foreach (var xmlAttrProperty in xmlAttrProperties)
        {
            var xmlAttribute = xmlAttrProperty.GetCustomAttribute<XmlAttributeAttribute>();
            if (xmlAttribute != null && schema.properties.ContainsKey(xmlAttribute.AttributeName))
            {
                var foundSchema = schema.properties[xmlAttribute.AttributeName];
                foundSchema.xml = new Xml { attribute = true };
            }
        }
    }
}

这些都可以在SwaggerConfig类中完成。

相关问题