Swashbuckle rename Data Type in Model(Swashbuckle 重命名模型中的数据类型)
问题描述
我正在组合一个需要匹配外部源 XML 格式的 Web API,并希望在 swagger 输出中重命名数据类型对象.
I'm putting together a web API that needs to match an external sources XML format and was looking to rename the Data Type objects in the swagger output.
它在类成员上运行良好,但我想知道是否也可以覆盖类名.
It's working fine on the members of the class but I was wondering if it was possible to override the class name as well.
例子:
[DataContract(Name="OVERRIDECLASSNAME")]
public class TestItem
{
[DataMember(Name="OVERRIDETHIS")]
public string toOverride {get; set;}
}
在生成的输出中,我最终看到型号:
In the generated output I end up seeing Model:
TestItem {
OVERRIDETHIS (string, optional)
}
我希望看到
覆盖类名 {OVERRIDETHIS(字符串,可选)}
OVERRIDECLASSNAME { OVERRIDETHIS (string, optional) }
这可能吗?
谢谢,
推荐答案
我有同样的问题,我想我现在解决了.
I had the same problem and I think I solved it now.
首先在 Swagger 配置中添加 SchemaId(从版本 5.2.2 见 https://github.com/domaindrivendev/Swashbuckle/issues/457):
First of all add SchemaId in Swagger Configuration (from version 5.2.2 see 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 重命名模型中的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swashbuckle 重命名模型中的数据类型
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01