有没有办法从 Swagger/OpenAPI 规范生成控制器 Spring MVC 代码?
我知道 Swagger 可以从现有的 Spring 代码生成,但这是否可能反过来?
我正在使用 Swagger 编辑器编写 API 文档,但在包含 JSON 对象的多部分 POST 请求中遇到问题。这是我的 Swagger YAML 文件:
swagger: '2.0'
info:
version: 1.0.0
title: Documentation API
paths:
/agent:
post:
consumes:
- multipart/form-data
produces:
- text/html
parameters:
- in: query
name: method
description: name of method to access
required: true
type: string
- in: body
name: param
description: parameter to send
required: true
schema:
$ref: "#/definitions/Param"
responses:
201:
description: item created
400:
description: invalid input, object invalid
409:
description: an existing item already exists
definitions:
Param: # …Run Code Online (Sandbox Code Playgroud) 我想在我的api文档中表示小数点后2位和小数点后1位。我正在使用swagger 2.0,规格中是否有内置定义的类型或任何其他“圆形”参数,或者我唯一的选择是使用“ x-”扩展名?
我正在尝试创建 OpenAPI yml 文档文件(通过 swagger)。我的 API 调用之一返回资源列表。每个资源都有属性、一个自链接和一个到附加链接的链接,该链接将检索与资源相关的附加“资料”。
请看下面的例子:
[
{
"name": "object-01",
"links": [
{
"rel": "self",
"href": "http://localhost:8800/foo/object-01"
},
{
"rel": "Supported stuff",
"href": "http://localhost:8800/foo/object-01/stuff"
}
]
}, {
"name": "object-02",
"links": [
{
"rel": "self",
"href": "http://localhost:8800/foo/object-02"
},
{
"rel": "Supported stuff",
"href": "http://localhost:8800/foo/object-02/stuff"
}
]
}, {
"name": "object-03",
"links": [
{
"rel": "self",
"href": "http://localhost:8800/foo/object-03"
},
{
"rel": "Supported stuff",
"href": "http://localhost:8800/foo/object-03/stuff"
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
我不确定记录这一点的正确方法是什么,这就是我现在所拥有的。
paths:
/foo/objects:
get:
operationId: getObject
responses:
'200':
description: Respresentation …Run Code Online (Sandbox Code Playgroud) 我有一个创建包含以下内容的多部分文件的服务:
是否有可能使用YAML在提供的OpenAPI(扬鞭)定义这个自定义响应模型,?
我从 http 请求中得到以下形式的响应:它是一个未命名数组和对象的数组。对于这种情况,我无法找出正确的 Swagger(Open API)规范。
[
[
{
"prop1": "hello",
"prop2": "hello again"
},
{
"prop1": "bye",
"prop2": "bye again"
}
],
{
"key": 123
}
]
Run Code Online (Sandbox Code Playgroud) 我希望发布一个包含可变数量字符串的数组,例如
[“ string1”,“ string2”,...“ stringN”]
我现在的OpenAPI文档是这样定义的:
schema:
type: array
items:
description: networkIds
type: string
Run Code Online (Sandbox Code Playgroud)
这是按照OpenAPi v3规范进行编码的正确方法,还是有一种更精确的方法来指示数组中的一个或多个字符串?
据说"要定义cookie身份验证,请改用API密钥".在官方文档中
https://swagger.io/docs/specification/describing-parameters/#cookie-parameters
事实是我们尝试过
components:
securitySchemes:
cookieAuth:
type: apiKey
in: cookie
name: sessionId
...
security:
- cookieAuth: []
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,在Swagger UI中,我们可以单击挂锁来设置sessionId的值.但是当我们执行该方法时,cookie的值为NULL,我们看不到在Headers中发送的cookie(Chrome Developer工具)
我也尝试将其放在cookie参数中,如下所示:
parameters:
- in: cookie
name: sessionId
required: true
schema:
type: string
Run Code Online (Sandbox Code Playgroud)
但是,同样的结果(到达null,调试器工具中没有任何内容).
我们使用Swagger和openApi 3.0,其他参数,requestBody运行良好,但不是这个cookie传输.
对于任何有想法的东西都可以.
我定义了以下架构:
User:
type: object
required:
- id
- username
properties:
id:
type: integer
format: int32
readOnly: true
xml:
attribute: true
description: The user ID
username:
type: string
readOnly: true
description: The username
first_name:
type: string
description: Users First Name
last_name:
type: string
description: Users Last Name
avatar:
$ref: '#/components/schemas/Image'
example:
id: 10
username: jsmith
first_name: Jessica
last_name: Smith
avatar: image goes here
xml:
name: user
Run Code Online (Sandbox Code Playgroud)
效果很好。该GET /user/{id}调用会很好地显示示例数据。
我有第二个架构,可以创建上述架构的数组:
ArrayOfUsers:
type: array
items:
type: object
required:
- id
- …Run Code Online (Sandbox Code Playgroud) 我试图添加任意类型的嵌套数组。这些是我的注释:
* @OA\Property(
* @OA\Schema(
* type="array",
* @OA\Items(
* type="array",
* @OA\Items(type={})
* )
* ),
* description="bla bla bla"
* )
Run Code Online (Sandbox Code Playgroud) openapi ×10
swagger ×7
swagger-2.0 ×2
arrays ×1
cookies ×1
hateoas ×1
http ×1
java ×1
multipart ×1
php ×1
rest ×1
spring-mvc ×1
swagger-php ×1