【问题标题】:Swashbuckle rename Data Type in ModelSwashbuckle 重命名模型中的数据类型
【发布时间】:2015-11-07 02:44:00
【问题描述】:

我正在组合一个需要匹配外部源 XML 格式的 Web API,并希望在 swagger 输出中重命名数据类型对象。

它在类成员上运行良好,但我想知道是否也可以覆盖类名。

例子:

[DataContract(Name="OVERRIDECLASSNAME")]
public class TestItem
{
   [DataMember(Name="OVERRIDETHIS")]
   public string toOverride {get; set;}
}

在生成的输出中,我最终看到 型号:

   TestItem {
      OVERRIDETHIS (string, optional)
   }

我希望看到

覆盖类名 { OVERRIDETHIS(字符串,可选) }

这可能吗?

谢谢,

【问题讨论】:

    标签: asp.net asp.net-web-api swagger swagger-ui swashbuckle


    【解决方案1】:

    我有同样的问题,我想我现在解决了。

    首先在 Swagger 配置中添加 SchemaId(从版本 5.2.2 见https://github.com/domaindrivendev/Swashbuckle/issues/457):

    GlobalConfiguration.Configuration
        .EnableSwagger(c =>
        {
            c.SchemaId(schemaIdStrategy);
            [...]
        }
    

    然后添加这个方法:

    private static string schemaIdStrategy(Type currentClass)
    {
        string returnedValue = currentClass.Name;
        foreach (var customAttributeData in currentClass.CustomAttributes)
        {
            if (customAttributeData.AttributeType.Name.ToLower() == "datacontractattribute")
            {
                foreach (var argument in customAttributeData.NamedArguments)
                {
                    if (argument.MemberName.ToLower() == "name")
                    {
                        returnedValue = argument.TypedValue.Value.ToString();
                    }
                }
            }
        }
        return returnedValue;
    }
    

    希望对你有帮助。

    【讨论】:

    • 有什么方法可以用 Swashbuckle 6 for ASP.NET Core 做到这一点?
    【解决方案2】:

    相当老的问题,但是当我在寻找类似的解决方案时,我遇到了这个问题。 我认为文森特答案中的代码可能不起作用。 这是我的看法:

        private static string schemaIdStrategy(Type currentClass)
        {
            var dataContractAttribute = currentClass.GetCustomAttribute<DataContractAttribute>();
            return dataContractAttribute != null && dataContractAttribute.Name != null ? dataContractAttribute.Name : currentClass.Name;
        }
    

    【讨论】:

      【解决方案3】:

      添加到线程中,因为我无法将答案与 Swashbukle 一起用于 AspNetCore。 我正在这样做。但是,我并不完全高兴,好像该对象包含在另一个显示其原始名称的对象中。例如,如果您有一个分页的结果集,则结果显示不正确。所以这不是最终答案,但可能适用于简单的用例。

      我正在使用模式过滤器。当我获得数据类型的 Title 属性时,该对象只有 [JsonObject(Title="CustomName")] 。 首先定义一个这样的类:

      public class CustomNameSchema : ISchemaFilter
          {
              public void Apply(Schema schema, SchemaFilterContext context)
              {
                  if (schema?.Properties == null)
                  {
                      return;
                  }
      
                  var objAttribute = context.SystemType.GetCustomAttribute<JsonObjectAttribute>();
                  if( objAttribute!= default && objAttribute?.Title?.Length > 0)
                  {
                      schema.Title = objAttribute.Title;
                  }
              }
          }
      

      在启动时你必须配置一个 SchemaFilter

      c.SchemaFilter<CustomNameSchema>();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-08
        • 2013-01-23
        • 2011-07-23
        • 2015-11-10
        相关资源
        最近更新 更多