使用SWAGGER在Web API文档中不调用ShouldSerialize *方法

问题描述 投票:0回答:1

我正在使用Swagger for Web API文档。在我的Web API中,我有如下实体:

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方法的输入参数,并生成了swagger文档。

在此之前它很好,但问题是,该文档中的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 *方法。任何帮助都非常感谢。

谢谢

asp.net-web-api json.net swagger
1个回答
0
投票

最后,我以不同的方式解决了它,因为问题与Swagger无关。

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

© www.soinside.com 2019 - 2024. All rights reserved.