2016-08-02 73 views
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 *方法。任何帮助,高度赞赏。

感谢

回答

0

最后我解决它以不同的方式为这个问题是不相关的招摇。

我已经创建了具有虚拟属性的基本抽象类。在派生类中,我重载了这些属性并使用JsonIgnore属性进行了装饰。这解决了我的问题。

相关问题