英文:
Swagger UI - Rendering nested object in multipart/form-data request "try it out" shows JSON instead of form UI fields
问题
I am using Swagger/OpenAPI version: OpenAPI 3.0.3
示例Swagger/OpenAPI定义:
openapi: 3.0.3
info:
title: 'My API'
version: 3.0.0
servers: []
paths:
/api_app:
post:
operationId: apiAppCreate
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/ApiAppCreateRequest'
responses:
'200':
description: 'success'
content:
application/json:
schema: {}
components:
schemas:
ApiAppCreateRequest:
properties:
oauth:
type: object
properties:
callback_url:
type: string
example: 'https://example.com/oauth'
scopes:
type: array
items:
type: string
example:
- basic_account_info
- request_signature
type: object
嵌套对象以Json输入形式显示,而不是如附图所示的form-data。
只有当请求的类型为form-data或multipart/form-data并且包含嵌套对象时,才会发生此情况。
似乎在Swagger UI的GitHub问题中没有解决方案,所以想在StackOverflow上询问,看看是否有人能解决这个问题。
我使用Java 8,并且依赖如下:
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>
另一个例子,UI部分呈现为form-data,部分呈现为json输入。
英文:
I am using Swagger/OpenAPI version: OpenAPI 3.0.3
Example Swagger/OpenAPI definition:
openapi: 3.0.3
info:
title: 'My API'
version: 3.0.0
servers: []
paths:
/api_app:
post:
operationId: apiAppCreate
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/ApiAppCreateRequest'
responses:
'200':
description: 'success'
content:
application/json:
schema: {}
components:
schemas:
ApiAppCreateRequest:
properties:
oauth:
type: object
properties:
callback_url:
type: string
example: 'https://example.com/oauth'
scopes:
type: array
items:
type: string
example:
- basic_account_info
- request_signature
type: object
The nested object displays as Json input instead of form-data as shown in attached image.
It should be rendered as form-fields. This happens only when we have form-data or multipart/form-data type of request and it has a nested object within.
Seems no solution on github issues of swagger ui so thought of asking on stackoverflow if in case anyone able to resolve this issue.
I am using Java 8 with dependency:
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>
Another example of how the UI renders partly in form-data and partly as json input
答案1
得分: 1
问题
我不确定oauth
到底是用作什么,但似乎这是一个请求oauth令牌的请求。您面临的问题是由于您的模式声明,您将所有内容声明为oauth
的属性。type: object
将所有内容强制转换为JSON模式,并通过将所有内容声明为oauth
的属性,将其强制转换为JSON对象的oauth
模式。
properties:
oauth:
type: object
properties:
您可以通过两种方式修复此问题。
解决方案1
删除顶级属性值,并减小callback_url
和scopes
的缩进。
components:
schemas:
ApiAppCreateRequest:
properties:
oath:
type: object
callback_url:
type: string
example: 'https://example.com/oauth'
scopes:
type: array
items:
type: string
example:
- basic_account_info
- request_signature
type: object
解决方案2
完全删除oauth
。
components:
schemas:
ApiAppCreateRequest:
properties:
callback_url:
type: string
example: 'https://example.com/oauth'
scopes:
type: array
items:
type: string
example:
- basic_account_info
- request_signature
type: object
备选方案
根据您对oauth
属性的具体用途,您还可以重新构造属性,例如将其作为文件或安全模式:
作为文件
将其视为文件,只需更新模式以具有type: string
和format: binary
。这将允许用户上传文件以用作安全授权。
oauth:
type: string
format: binary
作为安全模式
您还可以将oauth作为请求的一部分单独包括在安全模式中。
openapi: 3.0.3
info:
title: 'My API'
version: 3.0.0
servers: []
paths:
/api_app:
post:
operationId: apiAppCreate
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/ApiAppCreateRequest'
security:
- oAuthSample:
- create_app
responses:
'200':
description: 'success'
content:
application/json:
schema: {}
components:
schemas:
ApiAppCreateRequest:
properties:
callback_url:
type: string
example: 'https://example.com/oauth'
scopes:
type: array
items:
type: string
example:
- basic_account_info
- request_signature
type: object
securitySchemes:
oAuthSample:
type: oauth2
description: This API uses OAuth 2 with the implicit grant flow. [More info](https://whatever.com)
flows:
implicit:
authorizationUrl: https://whatever.com
scopes:
create_app: create a new app
最后
multipart/form-data
内容类型有很多可能性。您可以查看swagger文档或openapi文档以获取更多信息。例如,您可以使用encoding
属性来控制请求正文的各个部分的序列化方式。这包括设置标头、explode
和style
。
英文:
Problem
I'm not sure what exactly oauth
is being used as, but it seems like this is a request for an oauth token. The issue you are facing is due to your schema declaration, where you are declaring everything as a property of oauth
. The type: object
forces everything into a json schema, and by declaring everything as a property of oauth
, it's forcing it into the oauth
schema as a json object.
properties:
oauth:
type: object
properties:
You can fix this two ways.
Solution 1
remove the top level properties value and reduce the indentation of the callback_url
and scopes
.
components:
schemas:
ApiAppCreateRequest:
properties:
oath:
type: object
callback_url:
type: string
example: 'https://example.com/oauth'
scopes:
type: array
items:
type: string
example:
- basic_account_info
- request_signature
type: object
This will result in the following display in your try it out
Solution 2
Remove the oauth
entirely.
components:
schemas:
ApiAppCreateRequest:
properties:
callback_url:
type: string
example: 'https://example.com/oauth'
scopes:
type: array
items:
type: string
example:
- basic_account_info
- request_signature
type: object
This will remove the json object for oauth completely, as the below screenshot demonstrates.
Alternatives
Depending on what you are trying to do with the oauth
property, you could also restructure what the property is, such as a file or a security schema:
As a file
As a file, simply update the schema to have a type: string
and format: binary
. This will allow the user to upload a file to be used as the security authorization.
oauth:
type: string
format: binary
As security schema
You can also include the oauth as the security schema separately as a part of the request.
openapi: 3.0.3
info:
title: 'My API'
version: 3.0.0
servers: []
paths:
/api_app:
post:
operationId: apiAppCreate
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/ApiAppCreateRequest'
security:
- oAuthSample:
- create_app
responses:
'200':
description: 'success'
content:
application/json:
schema: {}
components:
schemas:
ApiAppCreateRequest:
properties:
callback_url:
type: string
example: 'https://example.com/oauth'
scopes:
type: array
items:
type: string
example:
- basic_account_info
- request_signature
type: object
securitySchemes:
oAuthSample:
type: oauth2
description: This API uses OAuth 2 with the implicit grant flow. [More info](https://whatever.com)
flows:
implicit:
authorizationUrl: https://whatever.com
scopes:
create_app: create a new app
Finally
The multipart/form-data
content type has a lot of possibilities. You can check the swagger documentation or the openapi documentation for more information. For example, you can use the encoding
property to gain control over the way in which various parts of the request bodies are serialized. This includes setting the headers, explode
, and style
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论