【问题标题】:Swashbuckle Swagger generate an actual guidSwashbuckle Swagger 生成一个实际的 guid
【发布时间】:2019-12-23 00:15:40
【问题描述】:

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

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

当我这样做的时候,是否有可能与任何字符串相同,以便 JSON 每次都不同?

【问题讨论】:

  • 您的 C# 代码中如何定义 GUID 属性?

标签: c# swagger guid swashbuckle


【解决方案1】:

像这样在您的有效负载类中装饰您的 GUID 属性

public class Payload
{
    /// <summary>
    /// The GUID
    /// </summary>
    /// <example>00000000-0000-0000-0000-000000000000</example>
    public string Guid { get; set; }
}

这应该将示例从“字符串”更改为“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);
        });

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 如何将示例添加到基于路由的输入参数中? /// /// 根据id获取购物篮 /// /// 应该是guid/uuid格式 /// 返回> [Route("{id}")] [SwaggerOperation("BasketGetById")] [SwaggerResponse(HttpStatusCode.OK, Type = typeof(BasketModel))] [SwaggerResponse(HttpStatusCode.NotFound)] public IHttpActionResult GetById(string id)
    猜你喜欢
    • 1970-01-01
    • 2021-04-13
    • 2020-10-30
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多