代码之家  ›  专栏  ›  技术社区  ›  David

SwaggerRequestExample在使用列表时引发错误

  •  1
  • David  · 技术社区  · 7 年前

    我已经跟踪了 this

    下面是一个代码示例

    using Swashbuckle.Examples;
    
    [HttpPost]
    [SwaggerRequestExample(typeof(List<TestModel>), typeof(TestExample))]     
    public IHttpActionResult ListTest([FromBody] List<TestModel> tests)
    {
        return Ok(tests);
    }
    
    public class TestExample : IExamplesProvider
    {
        public object GetExamples()
        {
            return new List<TestModel>() {
                new TestModel()
                {
                    Id = 1,
                    Text = "First object"
                },
                new TestModel()
                {
                    Id = 2,
                    Text = "Second object"
                },
            };
        }
    }
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   David    7 年前

    在我的昂首阔步中。反恐精英

    c.SchemaFilter<ApplySchemaVendorExtensions>();
    

    using Swashbuckle.Swagger;    
    public class ApplySchemaVendorExtensions : ISchemaFilter
    {
        public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
        {
            if (schema.properties != null)
            {
                if (type == typeof(TestModel))
                {
                    schema.example = new TestModel()
                    {
                        Id = 1,
                        Text = "Custom text value"
                    };
                }
            }
        }
    }
    

    如果有人有更好的解决方案,我还不会标为答案。