首页 文章

Swagger描述 endpoints 返回枚举时的响应

提问于
浏览
1

我的ASP.NET Core Web API中有一个 endpoints ,标有以下标记 [ProducesResponseType(typeof(SomeEnum), 200)] . 其中 SomeEnum 是具有两个或更多值的枚举 . 我使用Swagger生成文档 . 当我运行项目并导航到api文档页面时,问题就来了 . 对于此 endpoints ,我所拥有的唯一文档是:

enter image description here

但是我想要这样的东西:
enter image description here

从广义上讲,我希望我的枚举能够描述他们可以采取的 Value 观 .

这是我的招摇配置:

services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "API", Version = "v1" });

                var fileName = this.GetType().GetTypeInfo().Module.Name.Replace(".dll", ".xml");

                c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, fileName));

            c.DescribeAllEnumsAsStrings();

            c.OperationFilter<AddAuthorizationHeader>();
        });

要添加的枚举定义的示例:

public enum SomeSampleEnum 
{
  Success,
  Fail,
  OperationResult
}

1 回答

  • 0

    尝试将您的枚举更改为:

    public enum SomeSampleEnum 
    {
      Success = 1,
      Fail = 2,
      OperationResult = 3
    }
    

    在我的情况下工作 . 您可能还需要更改其他一些代码......

相关问题