Swashbuckle Swagger 生成一个实际的 guid

Swashbuckle Swagger generate an actual guid

我正在使用 Swashbuckle 在我的 .Net Core WebAPI 项目中生成 Swagger。如下所示,它生成了一个字符串类型的 GUID。我想生成一个随机 Guid 并将 "string" 替换为“”或空 guid“00000000-0000-0000-0000-000000000000”。这将使我的示例在 post 时实际工作。

{
  "payload": [
    {
      "GUID": "string",
      "status": "string"
    }
  ]
}

当我在做的时候,是否可以对任何字符串进行相同处理,以便每次 JSON 都不同?

像这样在负载 class 中装饰您的 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);
        });

使用最新版本的Swashbuckle.AspNetCore,您可以使用DefaultValue 属性。 例如:

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

最近实施了:https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/575