Swashbuckle Swagger generate an actual guid

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?

Answer

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);
        });

Leave a Reply

Your email address will not be published. Required fields are marked *