Swashbuckle Swagger generate an actual guid

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 06:47:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!