问题
I am using Swashbuckle to generate Swagger in my .Net Core WebAPI project. As you can see below, it generates a GUID of type string. I would like to generate a random Guid and replace "string" with "" or an empty guid "00000000-0000-0000-0000-000000000000". This would allow my example to actually work when I post it.
{
"payload": [
{
"GUID": "string",
"status": "string"
}
]
}
while I am at it, would it be possible to the same with any string so that JSON is different each time?
回答1:
Decorate your GUID property in your payload class like this
public class Payload
{
/// <summary>
/// The GUID
/// </summary>
/// <example>00000000-0000-0000-0000-000000000000</example>
public string Guid { get; set; }
}
This should change the example from "string" to "00000000-0000-0000-0000-000000000000"
EDIT: Forgot to add. In your startup.cs you might need to add the following code
// 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);
});
来源:https://stackoverflow.com/questions/57528575/swashbuckle-swagger-generate-an-actual-guid