0
我正在使用Swagger作为Web API文档。 在我的Web API,我有实体如下:使用SWAGGER在Web API文档中不调用ShouldSerialize *方法
public class BaseEntity
{
public string BaseEntProp1{get;set;}
public string BaseEntProp2{get;set;}
public virtual bool ShouldSerializeBaseEntProp1()
{
return true;
}
public virtual bool ShouldSerializeBaseEntProp1()
{
return true;
}
}
public class DerivedEntity1 : BaseEntity
{
[JsonIgnore]
public string DerEntProp1{get;set;}
public string DerEntProp2{get;set;}
public override bool ShouldSerializeBaseEntProp1()
{
return false;
}
public override bool ShouldSerializeBaseEntProp1()
{
return false;
}
}
我用DerivedEntity1作为Web API方法的输入参数和产生的招摇文档。
直到这是好的,但问题是,该文档中的DerivedEntity1 JSON字符串显示BaseEntProp1 & BaseEntProp2这应该被排除在外。有人可以帮助我如何排除这些?
注意: 1. DerivedEntity1的DerEntProp1属性被正确排除。 2.只是为了确认,我的启动方法生成的文档后,我已经硬编码了以下内容:
var temp = new DerivedEntity1 {
BaseEntProp1 = "should be ignored",
BaseEntProp2 = "this also should be ignored"};
string tempJson = JsonConvert.SerializeObject(temp);
以上测试通过,即tempJson不同时BaseEntProp1 & BaseEntProp2。所以,我怀疑SWAGGER未能调用适当的ShouldSerialize *方法。任何帮助,高度赞赏。
感谢