如何仅使用实际应发送的字段显示 Swashbuckle 参数对象?
How to display Swashbuckle parameter object only with fields that should actually be sent?
我开始使用 AspNetCore 的 Swagger using the Swashbuckle 库。
并且在我的 API 中放入一个带有引用的对象时,它显示好像有必要发送引用的所有字段,而只有标识符 (Id)
这是一个例子:
模型结构:
public class Cidade
{
public long Id { get; set; }
public string Nome { get; set; }
public Uf Uf { get; set; }
}
public class Uf
{
public long Id { get; set; }
public string Nome { get; set; }
public Pais Pais { get; set; }
}
public class Pais
{
public long Id { get; set; }
public string Nome { get; set; }
}
以及以下 API:
[Produces("application/json")]
[Route("api/Cidade")]
public class CidadeController : Controller
{
// POST: api/Cidade
[HttpPost]
public void Post([FromBody]Cidade value)
{
}
}
Swagger中的结果如下:
我想要的是以下内容(最多 uf.id
):
我怎样才能得到这个结果?
我正在查看我的样品,我想我找到了一些你可以使用的东西:
http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=P#/PolygonVolume/PolygonVolume_Post
就我而言,我正在添加更多,您需要的更少,但您仍然需要的只是一个自定义示例...
JSON 看起来像这样:
"PolygonVolumeInsideParameter": {
"properties": {
"Points": {
"items": {
"$ref": "#/definitions/Location"
},
"xml": {
"name": "Location",
"wrapped": true
},
"example": [
{
"Lat": 1.0,
"Lon": 2.0
},
{
"Lat": 5.0,
"Lon": 6.0
}
],
"type": "array"
},
"PlanId": {
"type": "string"
}
},
"xml": {
"name": "PolygonVolumeInsideParameter"
},
"type": "object"
},
并且在 swashbuckle 上我添加了示例 ISchemaFilter
我的代码在这里:
https://github.com/heldersepu/Swagger-Net-Test/blob/master/Swagger_Test/App_Start/SwaggerConfig.cs#L891
我按照 的逻辑得到了我的解决方案,如下所示:
我构建了一个架构过滤器以将示例添加到引用属性 (Ref
),它有一个名为“Id”的 属性:
public class ApplySchemaRefIdExtensions : ISchemaFilter
{
public void Apply(Schema schema, SchemaFilterContext context)
{
if (schema.Properties != null)
{
foreach (var p in schema.Properties)
{
if (p.Value.Example == null && p.Value.Ref != null)
{
var reference = context.SystemType.GetProperty(p.Value.Ref.Split("/").LastOrDefault());
if (reference != null)
{
var id = reference.PropertyType.GetProperty("Id");
if (id != null)
{
p.Value.Example = new
{
Id = 123
};
p.Value.Ref = null;
}
}
}
}
}
}
}
在 Startup.cs
:
services.AddSwaggerGen(c =>
{
// ...
c.SchemaFilter<ApplySchemaRefIdExtensions>();
});
相同 的结果:
我开始使用 AspNetCore 的 Swagger using the Swashbuckle 库。
并且在我的 API 中放入一个带有引用的对象时,它显示好像有必要发送引用的所有字段,而只有标识符 (Id)
这是一个例子:
模型结构:
public class Cidade
{
public long Id { get; set; }
public string Nome { get; set; }
public Uf Uf { get; set; }
}
public class Uf
{
public long Id { get; set; }
public string Nome { get; set; }
public Pais Pais { get; set; }
}
public class Pais
{
public long Id { get; set; }
public string Nome { get; set; }
}
以及以下 API:
[Produces("application/json")]
[Route("api/Cidade")]
public class CidadeController : Controller
{
// POST: api/Cidade
[HttpPost]
public void Post([FromBody]Cidade value)
{
}
}
Swagger中的结果如下:
我想要的是以下内容(最多 uf.id
):
我怎样才能得到这个结果?
我正在查看我的样品,我想我找到了一些你可以使用的东西:
http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=P#/PolygonVolume/PolygonVolume_Post
就我而言,我正在添加更多,您需要的更少,但您仍然需要的只是一个自定义示例...
JSON 看起来像这样:
"PolygonVolumeInsideParameter": {
"properties": {
"Points": {
"items": {
"$ref": "#/definitions/Location"
},
"xml": {
"name": "Location",
"wrapped": true
},
"example": [
{
"Lat": 1.0,
"Lon": 2.0
},
{
"Lat": 5.0,
"Lon": 6.0
}
],
"type": "array"
},
"PlanId": {
"type": "string"
}
},
"xml": {
"name": "PolygonVolumeInsideParameter"
},
"type": "object"
},
并且在 swashbuckle 上我添加了示例 ISchemaFilter
我的代码在这里:
https://github.com/heldersepu/Swagger-Net-Test/blob/master/Swagger_Test/App_Start/SwaggerConfig.cs#L891
我按照
我构建了一个架构过滤器以将示例添加到引用属性 (Ref
),它有一个名为“Id”的 属性:
public class ApplySchemaRefIdExtensions : ISchemaFilter
{
public void Apply(Schema schema, SchemaFilterContext context)
{
if (schema.Properties != null)
{
foreach (var p in schema.Properties)
{
if (p.Value.Example == null && p.Value.Ref != null)
{
var reference = context.SystemType.GetProperty(p.Value.Ref.Split("/").LastOrDefault());
if (reference != null)
{
var id = reference.PropertyType.GetProperty("Id");
if (id != null)
{
p.Value.Example = new
{
Id = 123
};
p.Value.Ref = null;
}
}
}
}
}
}
}
在 Startup.cs
:
services.AddSwaggerGen(c =>
{
// ...
c.SchemaFilter<ApplySchemaRefIdExtensions>();
});