我遵循了this教程,为swagger帮助页面设置了一个使用Swashbuckle的自定义Swagger请求示例。
当API接受单个对象作为主体参数时,它可以很好地工作,但是当使用列表时,我会得到一个"Object not set to an object of an object“异常
下面是一个代码示例
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"
},
};
}
}任何帮助都将不胜感激
发布于 2017-08-28 18:29:09
通过@HelderSepu建议使用ApplySchemaVendorExtensions,我做了以下工作来让它工作:
在我的SwaggerConfig.cs
c.SchemaFilter<ApplySchemaVendorExtensions>();ApplySchemaVendorExtensions.cs
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"
};
}
}
}
}我还不会标记为答案,以防有人有更好的解决方案。
https://stackoverflow.com/questions/45858129
复制相似问题