我想做的是指定有时对 API 调用的响应可能是 PDF 文档,有时是 JSON。我想以 OpenAPI 3.0 格式执行此操作。对于 PDF,响应将如下所示:
responses:
'200':
description: An invoice in PDF format.
content:
application/pdf:
schema:
type: string
format: binary
Run Code Online (Sandbox Code Playgroud)
在 JSON 响应的情况下,这将描述响应:
responses:
'200':
description: A JSON object containing user name and avatar
content:
application/json:
schema:
$ref: "#/components/schemas/Invoice"
Run Code Online (Sandbox Code Playgroud)
OAS3 文档 ( https://swagger.io/docs/specification/describing-responses/ ) 提供了以下示例,说明如何指定几种不同的 JSON 模式之一可以是对特定 API 调用的响应。这几乎就是我想要的,除了描述不同的可能的 JSON 模式之外,我想指定不同的可能的内容类型,如上所述。有没有办法以 OAS3 格式执行此操作?
responses:
'200':
description: A JSON object containing pet information
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
- $ref: '#/components/schemas/Hamster'
Run Code Online (Sandbox Code Playgroud) 在OpenAPI 3.0 规范中,根OpenAPI 对象具有作为服务器对象servers数组的属性。并且Path Item Object还允许一个可选的属性。servers
规范中给出的描述并没有清楚地说明如何servers提供帮助。
servers财产的意义是什么?我们是否有任何示例来解释servers作为根 OpenAPI 对象的直接属性和路径项的属性的用例?
在 Golang 中,是否有一种更简洁的方法来实现围绕 oneOf、anyOf、allOf 的 OpenAPI 3.0 构造。我觉得拥有一个带有鉴别器字段的扁平结构是明智的。(下例)
package main
import (
"encoding/json"
"fmt"
)
type OneOfCertification struct {
AssetType string `json:"assetType"`
CertDetails *json.RawMessage `json:"certDetails"`
}
type FlatCertification struct {
AssetType string `json:"assetType"`
DiamondCert *Diamond `json:"diamondCert,omitempty"`
GemCert *Gem `json:"gemCert,omitempty"`
}
type Diamond struct {
DiamondAttr string `json:"diamondAttr"`
}
type Gem struct {
GemAttr string `json:"gemAttr"`
}
func oneOfUnmarshalMarshal(data []byte) ([]byte, error) {
var cert OneOfCertification
json.Unmarshal(data, &cert)
switch cert.AssetType {
case "DIA":
var dia Diamond
json.Unmarshal(*cert.CertDetails, &dia)
fmt.Println("(oneOf) Diamond Cert …Run Code Online (Sandbox Code Playgroud) 我想通过 POST 方法将数组发送到我的 API。
这是我的代码:
/**
* @OA\Post(path="/test/bulk/{edition}/{company}", tags={"Test"}, summary="Test",
* @OA\Parameter(
* name="company", required=true, in="path", @OA\Schema(type="integer")
* ),
* @OA\Parameter(
* name="edition", required=true, in="path", @OA\Schema(type="integer")
* ),
* @OA\RequestBody(
* @OA\MediaType(mediaType="multipart/form-data",
* @OA\Schema(
* @OA\Property(property="key[]", type="array", collectionFormat="multi", @OA\Items(type="string")),
* @OA\Property(property="value[]", type="array", collectionFormat="multi", @OA\Items(type="string")),
* @OA\Property(property="file[]", type="array", collectionFormat="multi", @OA\Items(type="string", format="binary")),
* @OA\Property(property="confirmed[]", type="array", collectionFormat="multi", @OA\Items(type="integer")),
* required={"key", "confirmed"}
* )
* )
* ),
* @OA\Response(response=201, description="OK"),
* @OA\Response(response=400, description="Not Found or Validation Error"),
* )
*/
Run Code Online (Sandbox Code Playgroud)
但我不知道为什么 swagger 以这种方式发送: …
我想schema在 OpenAPI 3.0 API 定义中将以下 JSON 表示为 a :
{
get-question: {
question-id:string
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经写了:
{
get-question: {
question-id:string
}
}
Run Code Online (Sandbox Code Playgroud)
但我在 Swagger 编辑器中收到这些错误:
Schema error at components.schemas['GetQuestion']
should have required property '$ref'
missingProperty: $ref
Jump to line 79
Schema error at components.schemas['GetQuestion']
should match exactly one schema in oneOf
Jump to line 79
Schema error at components.schemas['GetQuestion'].properties['get-questions']
should have required property '$ref'
missingProperty: $ref
Jump to line 81
Schema error at components.schemas['GetQuestion'].properties['get-questions']
should match exactly one …Run Code Online (Sandbox Code Playgroud) 有一些输入的形式,如“名字”、“姓氏”、“中间名”等。它们通过后请求作为数组发送,它看起来像:
乘客[0][名字]
乘客[0][姓氏]
乘客[0][中间名]
乘客[1][名字]
乘客[1][姓氏]
乘客[1][中间名]。
如何用 swagger for php (v3.0.2) 来描述它?我试过这样做:
* @OA\Parameter(name="passengers", required=true, description="passenger information", in="query",
* @OA\Schema(
* type="array",
* @OA\Items(
* type="object",
* @OA\Property(
* property="firstName",
* description="The product name",
* @OA\Schema(type="string")
* ),
* @OA\Property(
* property="lastName",
* @OA\Schema(type="string")
* ),
* @OA\Property(
* property="middleName",
* @OA\Schema(type="string")
* )
* )
* )
* ),
Run Code Online (Sandbox Code Playgroud)
但它只显示带有 JSON 的 textarea。
如何使用不同类型(字符串、整数等)的项目来处理数组类型的参数?我不需要将数据作为 JSON 发送,它应该作为表单元素的数组发送。
我正在使用 OpenApi 3.0 规范来记录我的 API。它基本上是一个 REST API,除了登录请求之外,每个请求都需要一个有效的 Bearer 令牌。OpenAPI 规范如下所示:
---
openapi: 3.0.0
info:
title: MyTitle
contact:
email: mymail@mail.com
version: 0.0.1
servers:
- url: http://localhost/api/v1
components:
schemas:
...
securitySchemes:
bearerAuth:
type: http
bearerFormat: JWT
scheme: bearer
security:
- bearerAuth: []
Run Code Online (Sandbox Code Playgroud)
在自动生成的 swagger-ui 中工作得非常好,当我下载 OpenAPI 规范并将其导入 Postman 时,邮递员也是如此。
但是,当使用以下命令使用 openapi-generator 时:
docker run --user `id -u`:`id -g` --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate -i /local/api.json -g python -o /local/api
Run Code Online (Sandbox Code Playgroud)
但是,它尝试使用用户名和密码代替不记名令牌:
def get_basic_auth_token(self):
"""Gets HTTP basic authentication header (string).
:return: The token for …Run Code Online (Sandbox Code Playgroud) 当您呈现文档时,RAML 会使用相应的模式自动验证包含的示例。
有没有办法对 OpenAPI 3 规范做同样的事情?
codekie/swagger-examples-validator目前仅支持 OpenAPI 2。
任何人都知道一个简单的解决方案来根据他们的模式检查所有示例?
鉴于已经存在一个特殊的“type”属性,它是一个保留关键字,有什么方法可以命名自定义属性“type”。
components:
schemas:
element:
type: object
properties:
name:
type: string #type here is the keyword
type: #type here is the actual name of the property!
type: string
enum:
- radiogroup
- checkbox
Run Code Online (Sandbox Code Playgroud)
无法修改生成 JSON 消息的后端系统来重命名属性。谢谢。
我正在编写一个新的 API 并使用 Swagger/OpenAPI 对其进行记录。记录错误响应似乎是一个很好的标准,开发人员可能会遇到。但是我找不到关于内部服务器错误的任何指导方针或最佳实践。理论上,每条路径都可能抛出未处理的异常。我不希望它发生,但它可能会发生。所有路径都应该有一个状态代码为 500“内部服务器错误”的响应,还是我应该只记录开发人员可以做任何事情的响应,即 2xx、3xx 和 4xx?
openapi ×10
swagger ×4
php ×2
api ×1
go ×1
jsonschema ×1
lumen ×1
python ×1
surveyjs ×1
swagger-2.0 ×1
validation ×1
yaml ×1