在我的.Net核心WebAPI项目中,我使用Swashbuckle生成Swagger。如下所示,它生成一个类型为string的GUID。我想要生成一个随机的指南,并将"string“替换为"”或空Guid“00000000-0000-0000-000000000000”。这将允许我的例子在我发布它时实际工作。
{
"payload": [
{
"GUID": "string",
"status": "string"
}
]
}在我这样做的时候,是否可以对任何字符串都使用相同的字符串,以便每次JSON都不同?
发布于 2019-08-16 17:35:39
在有效负载类中装饰GUID属性如下
public class Payload
{
/// <summary>
/// The GUID
/// </summary>
/// <example>00000000-0000-0000-0000-000000000000</example>
public string Guid { get; set; }
}这应该将示例从"string“更改为"00000000-0000-0000-0000-000000000000”。
编辑:忘记添加。在startup.cs中,您可能需要添加以下代码
// Swagger
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "TEST API", Version = "v1" });
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});发布于 2021-03-11 10:02:49
使用最新版本的Swashbuckle.AspNetCore,您可以使用DefaultValue属性。例如:
[DefaultValue("00000000-0000-0000-0000-000000000000")]
public string Guid { get; set; }它是最近实现的:https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/575
https://stackoverflow.com/questions/57528575
复制相似问题