[英]C# Swashbuckle: Obsolete model property shows without change in swagger
我在我的(输入)模型中将一个属性标记为过时
public class MyModel
{
[Obsolete("Use 'OtherProperty'")]
public string SomeProperty {get;set;}
public List<string> OtherProperty {get;set;}
}
但是,swagger 没有显示这两个属性之间的区别,也没有显示消息。
有什么方法可以让我大摇大摆地尊重 Obsolete 属性? 或者我需要自己把它放在属性上方的 xml-comments 中吗?
不幸的是,目前尚不支持 Swashbuckle 上的过时属性......
我们受限于 OpenAPI 规范,Swashbuckle 仍在使用 2.0
最接近的东西已被弃用,但仅适用于方法而不适用于属性:
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#operation-object
一种选择是使用IDocumentFilter
破解某些东西以完全隐藏那些标记为Obsolete
属性,但这将是一条崎岖不平的道路。
另一种选择是创建两个方法和两个模型,这样你就可以标记方法并转换到其中的方法,一切都将被弃用(我认为这有点混乱)但我已经看到这种模式在许多网络中使用-api
我认为您最好/最简单的解决方案是您建议添加一些 xml 注释,指出不应使用该属性。
[Obsolete]
public string Property {get; set;}
services.AddSwaggerGen(x =>
// your other settings...
x.IgnoreObsoleteProperties();
)
这对我有用
它通过简单地用[Obsolete]
属性(来自 System 命名空间)装饰属性并将 Swagger 标志IgnoreObsoleteProperties
设置为 true 对我IgnoreObsoleteProperties
。 我还添加了属性SomePropertySpecified ,如果请求中存在SomeProperty ,序列化程序会自动将其设置为 true (空值并不意味着该属性不存在)。 如果 SomePropertySpecified 为真,我有一个自定义逻辑来返回适当的错误消息。
public class Item
{
[Obsolete]
public string SomeProperty { get; set; }
[JsonIgnore]
public bool SomePropertySpecified { get; set; }
public List<string> OtherProperty { get; set; }
}
类 SwaggerConfig:
public class SwaggerConfig
{
public static void Register()
{
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "Demo");
c.IgnoreObsoleteProperties();
})
.EnableSwaggerUi(c =>
{
c.DocExpansion(DocExpansion.Full);
});
}
}
招摇的用户界面:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.