标签: swashbuckle

利用属性版本控制在Swagger中利用MultipleApiVersions

使用属性路由时,是否可以在Swagger UI/Swashbuckle中利用MultipleApiVersions?

具体来说,我实现了版本控制:

using System.Web.Http;

namespace RESTServices.Controllers.v1
{
    [Route("api/v1/Test")]
    public class TestV1Controller : ApiController
    { ... }
Run Code Online (Sandbox Code Playgroud)

版本2将位于v2名称空间中.在名为TestV2Controller的控制器中.路线中有v2.

是否可以传递一个lambda,这将允许这个?我在网上发现了一个编译的lambda样本,但随后Swagger完全停止了工作.无法在浏览器中点击断点或看到Swagger.

attributes swagger swagger-ui asp.net-web-api2 swashbuckle

9
推荐指数
1
解决办法
6788
查看次数

在SwashBuckle中删除带有IOperationFilter的路由

我正在寻找一种方法,以可配置的方式使用SwashBuckle在Swagger文档中显示/隐藏WebAPI路由.添加[ApiExplorerSettings(IgnoreApi = true)]确实会隐藏路由,但每次我想要更改时我都需要重新编译.

我已经研究过创建一个IOperationFilter使用我定义的自定义属性的方法.这样我可以用a装饰路线[SwaggerTag("MobileOnly")]并检查web.config或其他东西,看看是否应该显示路线.属性定义如下:

public class SwaggerTagAttribute : Attribute
{
    public string[] Tags { get; private set; }

    public SwaggerTagAttribute(params string[] tags)
    {
        this.Tags = tags;
    }
}
Run Code Online (Sandbox Code Playgroud)

所述IOperationFilter检测所述属性被定义并且IDocumentFilter去除的路径在这里被定义:

public class RemoveTaggedOperationsFilter : IOperationFilter, IDocumentFilter
{
    private List<string> TagsToHide;

    public RemoveTaggedOperationsFilter()
    {
        TagsToHide = ConfigurationManager.AppSettings["TagsToHide"].Split(',').ToList();
    }

    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        var tags = apiDescription.ActionDescriptor
            .GetCustomAttributes<SwaggerTagAttribute>()
            .Select(t => t.Tags)
            .FirstOrDefault();

        if (tags != null && TagsToHide.Intersect(tags).Any()) …
Run Code Online (Sandbox Code Playgroud)

c# custom-attributes asp.net-web-api swagger swashbuckle

9
推荐指数
1
解决办法
6543
查看次数

标题中的API密钥与swashbuckle

我想在一个带有Swashbuckle的WebAPI项目上进行基于API密钥的身份验证(swagger for .net).

我已将swashbuckle配置如下:

config
    .EnableSwagger(c =>
    {
        c.ApiKey("apiKey")
            .Description("API Key Authentication")
            .Name("X-ApiKey")
            .In("header");
        c.SingleApiVersion("v1", "My API");

    })
    .EnableSwaggerUi();
Run Code Online (Sandbox Code Playgroud)

(参见https://github.com/domaindrivendev/Swashbuckle#describing-securityauthorization-schemes)

它似乎创建了我期望的swagger文件:

   "securityDefinitions": {
      "apiKey": {
        "type": "apiKey",
        "description": "API Key Authentication",
        "name": "X-ApiKey",
        "in": "header"
      }
    }

但是当我转到UI并"试一试"时,它会尝试将API密钥放入查询字符串(我认为是默认行为)而不是标题.

例如:

curl -X POST --header 'Accept: application/json' 'http://localhost:63563/api/MyMethod?api_key=key'

如何使用swigger将API密钥放在标头而不是查询字符串中?

asp.net-web-api swagger swagger-ui swashbuckle

9
推荐指数
1
解决办法
1万
查看次数

Swashbuckle自动向生成的Swagger文件添加200 OK响应

我在WebApi 2项目中使用Swashbuckle构建swagger文档.

我有以下方法的定义:

[HttpPost]
[ResponseType(typeof(Reservation))]
[Route("reservations")]
[SwaggerResponse(HttpStatusCode.Created, Type = typeof(Reservation))]
[SwaggerResponse(HttpStatusCode.BadRequest) ]
[SwaggerResponse(HttpStatusCode.Conflict)]
[SwaggerResponse(HttpStatusCode.NotFound)]
[SwaggerResponse(HttpStatusCode.InternalServerError)]        
public async Task<HttpResponseMessage> ReserveTickets([FromBody] ReserveTicketsRequest reserveTicketRequest)
{
    // ...
    return Request.CreateResponse(HttpStatusCode.Created, response);
}
Run Code Online (Sandbox Code Playgroud)

但是,生成的Swagger文件也包含HTTP 200 OK,尽管它没有在任何地方指定.

/reservations: 
  post: 
    tags: 
      - "Booking"
    operationId: "Booking_ReserveTickets"
    consumes: 
      - "application/json"
      - "text/json"
    produces: 
      - "application/json"
      - "text/json"
    parameters: 
      - 
        name: "reserveTicketRequest"
        in: "body"
        required: true
        schema: 
          $ref: "#/definitions/ReserveTicketsRequest"
    responses: 
      200: 
        description: "OK"
        schema: 
          $ref: "#/definitions/Reservation"
      201: 
        description: "Created"
        schema: 
          $ref: "#/definitions/Reservation"
      400: 
        description: "BadRequest"
      404: 
        description: "NotFound"
      409: 
        description: …
Run Code Online (Sandbox Code Playgroud)

c# swagger swashbuckle

9
推荐指数
1
解决办法
3446
查看次数

是什么导致了swagger已经在Web API 2的路由集合中出现的错误?

我在我的ASP.Net MVC Core项目中安装了Swagger,它正在精美地记录我的API.

我的同事让我在一个完整的框架4.6.1项目中安装它,所以我做了以下工作.

在Package Console Manager中运行:

Install-Package Swashbuckle
Run Code Online (Sandbox Code Playgroud)

要使Test Web API控制器正常工作:1)在WebApi.config中注释掉它:

// config.SuppressDefaultHostAuthentication();
// config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
Run Code Online (Sandbox Code Playgroud)

现在这个URL: http:// localhost:33515/api/Test 带回XML:

<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<string>value1</string>
<string>value2</string>
</ArrayOfstring>
Run Code Online (Sandbox Code Playgroud)

在Global.asax Register()中,我调用:

 SwaggerConfig.Register();
Run Code Online (Sandbox Code Playgroud)

在AppStart.Swagger.Config Register()中我放了:

public class SwaggerConfig
{
    public static void Register()
    {     
       var thisAssembly = typeof(SwaggerConfig).Assembly;
       GlobalConfiguration.Configuration
            .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1.0", "HRSA CHAFS");
                    c.IncludeXmlComments(GetXmlCommentsPath());
                })
              .EnableSwaggerUi(c =>
                {});
    }

    private static string GetXmlCommentsPath()
    {
        var path = String.Format(@"{0}bin\Services.XML", AppDomain.CurrentDomain.BaseDirectory);
        return path;

    }
}
Run Code Online (Sandbox Code Playgroud)

现在我收到这个错误:"路由集合中已经有一条名为'swagger_docsswagger/docs/{apiVersion}'的路由.路由名称必须是唯一的."

我已经坚持了几个小时.你怎么摆脱这个?

swagger asp.net-web-api2 swashbuckle

9
推荐指数
2
解决办法
4208
查看次数

使用 Swashbuckle 生成 yaml swagger

json使用Microsoft 的教程在我的 Web API 项目中获取文件没有问题。

由于某些要求,我希望能够检索yaml文件。但问题是我找不到任何钩子来使这成为可能。

有谁知道这个问题的任何解决方法

c# asp.net-web-api swagger swashbuckle

9
推荐指数
2
解决办法
1万
查看次数

.NET Core 2.0 Web API - 如何在Swagger中添加自定义标头参数

根据标题 - 我已经找到了如何使用常规.NET执行此操作的示例

例如: Web Api如何为Swagger中的所有API添加Header参数

但是,我找不到任何示例或文档,显示如何在使用.NET Core 2.0时完成相同的操作.

swagger .net-core swashbuckle openapi.net

9
推荐指数
1
解决办法
7328
查看次数

在 ASP.NET Core 3.0 Swagger 中将字符串标记为不可为空

我正在使用 ASP.NET Core 3 和 Swashbuckle,大部分是默认配置,我有一个 DTO 参数,上面有一个我希望不可为空的字符串。我怎样才能做到这一点?请注意,在 Swagger 中,Required 和 nullability 是不同的关注点。

它还使用 C#8 和不可为空的东西,因此编译器应该已经将属性注释为不可为空。可以理解的是,Swashbuckle 尚未更新以考虑到这一点(也许不能),但我希望能够以某种方式覆盖生成的元数据。

class MyDto {
    [Required]
    // I want this to show as non-nullable in the swagger documentation (and ideally also be non-nullable in the binding)
    public string TestProp { get; set; }
}

[HttpPost]
public void Post([FromBody] MyDto requestModel) {
}
Run Code Online (Sandbox Code Playgroud)

我试过让它成为必需的。我还尝试添加 Newtonsoft 注释,但这些注释似乎都没有。

生成的 Swagger 文档的相关位:

    "MyDto": {
      "required": [
        "testProp"
      ],
      "type": "object",
      "properties": {
        "testProp": {
          "type": "string",
          "nullable": true
        } …
Run Code Online (Sandbox Code Playgroud)

c# swashbuckle asp.net-core c#-8.0

9
推荐指数
2
解决办法
7177
查看次数

用于 net core 3.1 api 的 Swagger UI 非常慢

我将我们的网络核心 API 应用程序从 2.1 更新到 3.1,将 SwashBuckle.Asp.NetCore 更新到 5.0.0。这是我的启动集:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
     string authServerUrl = "http://testserver.com/identityserver4";
         services.AddControllersWithViews();

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

            // Define the OAuth2.0 scheme that's in use (i.e. Implicit …
Run Code Online (Sandbox Code Playgroud)

c# swagger swagger-ui .net-core swashbuckle

9
推荐指数
2
解决办法
3935
查看次数

使用 Swashbuckle 5.x 在通用 T 参数引用属性上指定 nullable = true

我最近使用 Swashbuckle 5 和 newtonsoft json nuget 将我的 API 升级到 .net core 3.1 服务器,它生成一个 openapi 3 模式。然后我使用 NSwag 生成一个 C# API。以前我有一个带有 swashbuckle 4 的 .net core 2.2 服务器,生成了一个 swagger 2.0 api 模式。

我有一个适用于所有响应的通用响应类,包含一些关于响应的元数据,如状态代码和消息,以及包含响应内容的通用类型 T 的 Payload 属性。

当响应是错误代码时,我将有效负载属性设置为 null。我正在努力寻找一种定义我的 api 的方法,以便 swashbuckle 和 NSwag 结合产生一个 C# api,它将允许有效载荷属性在反序列化时为空。(swagger 2.0 / swashbuckle 4 没有问题)。

尽我所能,Payload 属性总是获取注释[Newtonsoft.Json.JsonProperty("payload", Required = Newtonsoft.Json.Required.DisallowNull...][System.ComponentModel.DataAnnotations.Required]注释。

据我了解,开放 API 3 现在允许 "$ref" 属性在架构定义中具有 "nullable": true 属性。如果我在创建后手动将其添加到我的定义中,NSwag 会正确删除 CSharp api 中的必需属性,并将 JsonProperty 必需属性设置为“默认”(非必需)而不是“DisallowNull”。
但是,我标记有效负载属性的任何内容都不会导致 nullable: true 出现在我的架构 …

c# swagger swashbuckle openapi nswagstudio

9
推荐指数
1
解决办法
3667
查看次数