Swashbuckle Swagger generate an actual guid
Asked Answered
Y

2

2

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?

Yuma answered 16/8, 2019 at 17:3 Comment(1)
How is the GUID property defined in your C# code?Madura
A
4

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);
        });
Aldaaldan answered 16/8, 2019 at 17:35 Comment(0)
I
3

With the latest version of Swashbuckle.AspNetCore, you can use DefaultValue attribute. Ex:

[DefaultValue("00000000-0000-0000-0000-000000000000")]
public string Guid { get; set; }

It has been implemented recently: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/575

Inotropic answered 11/3, 2021 at 10:2 Comment(1)
how to add example to a route based input param ? /// <summary> /// Get basket based on id /// </summary> /// <param name="id">should be guid / uuid format</param> /// <returns></returns> [Route("{id}")] [SwaggerOperation("BasketGetById")] [SwaggerResponse(HttpStatusCode.OK, Type = typeof(BasketModel))] [SwaggerResponse(HttpStatusCode.NotFound)] public IHttpActionResult GetById(string id)Mouthwash

© 2022 - 2024 — McMap. All rights reserved.