文件上传时,Swagger UI执行按钮不起作用 [英] Swagger UI execute button not working when file is uploading

查看:524
本文介绍了文件上传时,Swagger UI执行按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的API中的一个端点是用于上传文件并提交有关该文件的详细信息.该API在ASP.Net Core中实现.NSwag软件包已安装在我的项目中.实施OAS 3规范

One of the endpoint in my API is for uploading a file and submitting details about the file . The API is implemented in ASP.Net Core. NSwag package is installed in my project. OAS 3 specification is implemented

在Swagger UI上,我可以提交数据而无需上传文件.如果使用文件浏览器选择了文件,则单击执行"按钮后将不起作用.

On Swagger UI I can submit data without uploading file. if the file is chosen using file browser, the execute button is not working when clicked.

我检查了Chrome Dev工具中的控制台和网络"标签.没有网络移动.根本没有任何要求.在控制台中均未看到错误或任何日志.

I checked Console and Network tab in Chrome Dev tools. There is no network movement. There is no request at all. Neither errors nor any logs were seen in console.

我根据使用了承载令牌授权.在这两种情况下,无论是处于授权状态还是非授权状态,我都尝试过.在这两种情况下,选择文件后,执行"按钮均不起作用.当没有给出承载令牌时,我用文件以外的其他参数执行.我得到了401个状态响应.如果选择了文件,则单击执行按钮无效.

I used bearer token authorisation according to this. I tried in both cases of when in authorised and not authorised state. In both case Execute button not working when file is chosen. When bearer token was not given, and I execute with other parameters except file. I got 401 status response. If the file chosen , the click of execute button has no effect.

摇摇欲坠的文件是:

{
  "x-generator": "NSwag v13.1.5.0 (NJsonSchema v10.0.27.0 (Newtonsoft.Json v12.0.0.0))",
  "openapi": "3.0.0",
  "info": {
    "title": "My Title",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://localhost:5300"
    }
  ],
  "paths": {
    "/api/Order": {
      "post": {
        "tags": [
          "Order"
        ],
        "operationId": "Order_Post",
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/octet-stream": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          }
        }
      }
    },
    "/api/Order/{action}/document": {
      "post": {
        "tags": [
          "Order"
        ],
        "operationId": "Order_CreateDocument",
        "parameters": [
          {
            "name": "FileName",
            "in": "formData",
            "schema": {
              "type": "string",
              "nullable": true
            },
            "x-position": 1
          },
          {
            "name": "FileType",
            "in": "formData",
            "schema": {
              "type": "string",
              "nullable": true
            },
            "x-position": 2
          },
          {
            "name": "DocumentType",
            "in": "formData",
            "schema": {
              "type": "string",
              "nullable": true
            },
            "x-position": 3
          },
          {
            "name": "Description",
            "in": "formData",
            "schema": {
              "type": "string",
              "nullable": true
            },
            "x-position": 4
          },
          {
            "name": "Owners",
            "in": "formData",
            "collectionFormat": "multi",
            "schema": {
              "type": "array",
              "nullable": true,
              "items": {
                "type": "string"
              }
            },
            "x-position": 5
          },
          {
            "type": "file",
            "name": "SomeFile",
            "in": "formData",
            "schema": {
              "type": "string",
              "format": "binary",
              "nullable": true
            },
            "nullable": true
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/octet-stream": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          }
        }
      }
    },
    "/api/Debug": {
      "get": {
        "tags": [
          "Debug"
        ],
        "operationId": "Debug_Get",
        "parameters": [
          {
            "name": "message",
            "in": "query",
            "schema": {
              "type": "string",
              "nullable": true
            },
            "x-position": 1
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "authorization": {
        "type": "apiKey",
        "name": "authorization",
        "in": "header"
      }
    }
  },
  "security": [
    {
      "authorization": []
    }
  ]
}

我想将文件和其他详细信息作为多部分表单数据提交.

I want to submit the file and other details as multipart form data.

端点的ASP.Net Core代码:

ASP.Net Core code for the endpoint:

        [HttpPost("document")]
        public ActionResult CreateDocument([FromForm]Document request)
        { ...  }

文档类:

public class Document
    {
        public string FileName { get; set; }
        public string FileType { get; set; }
        public string DocumentType { get; set; }
        public string Description { get; set; }
        public List<string> Owners { get; set; } = new List<string>();
        public Microsoft.AspNetCore.Http.IFormFile SomeFile { get; set; }

    }

在Startup.cs中:

In Startup.cs :

public void ConfigureServices(IServiceCollection services)
{
  .... 
   ...
            // Register the Swagger services
            services.AddOpenApiDocument(document =>
            {
                document.AddSecurity("authorization", Enumerable.Empty<string>(), new NSwag.OpenApiSecurityScheme
                {
                    Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
                    Name = "authorization",
                    In = NSwag.OpenApiSecurityApiKeyLocation.Header
                });

                document.OperationProcessors.Add(
                    new NSwag.Generation.Processors.Security.AspNetCoreOperationSecurityScopeProcessor("bearer"));
            }); // registers a OpenAPI v3.0 document 

}


public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            . . . . . .
            . . . . . .
            // Register the Swagger generator and the Swagger UI middlewares
            app.UseOpenApi();
            app.UseSwaggerUi3();
        }

为什么Swagger-UI中的执行按钮不起作用?如何从Swagger-UI上传文件?

Why is execute button in Swagger-UI not working ? How can i upload file from Swagger-UI ?

推荐答案

@Helen有

@Helen has commented and pointed the issue. It is NSwag.AspNetCore package/library.

似乎NSwag库在Open API sepecification 3.0中的ASP.Core API的Swagger Generation中有一个错误.问题.仅当使用FromForm属性进行模型绑定时.以我为例,该属性之一是Microsoft.AspNetCore.Http.IFormFile类型,它也可能导致此错误.

It seems NSwag library has a bug in Swagger Generation for ASP.Core API in OpenAPI sepecification 3.0 . the issues for this error. It is only when using FromForm attribute for Model binding. In my case one of the property is Microsoft.AspNetCore.Http.IFormFile type that also may cause this error.

更改为OAS 2.0是临时解决方案.

Changing to OAS 2.0 is temporary solution.

这篇关于文件上传时,Swagger UI执行按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆