在我的OpenAPI规范中,是否可以将参数定义为对象而无需定义其属性(匿名对象)?更具体地说,我希望我的API能够接受这些匿名对象的数组。
这是我拥有的,但我得到了一个"无效的参数定义" Swagger编辑器中的错误。
swagger: '2.0'
info:
title: Test API
description: Test
version: "1.0.0"
host: api.example.com
schemes:
- https
basePath: /v1
produces:
- application/json
paths:
/api/example:
post:
description: Endpoint description
parameters:
- name: param1
in: query
description: param1
required: true
type: array
items:
type: object
- name: param2
in: query
description: param2
required: true
type: string
responses:
200:
description: error response
schema:
type: object
default:
description: Unexpected error
schema:
$ref: '#/definitions/Error'
definitions:
Error:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
fields:
type: string
这是错误:
Object
code: "ONE_OF_MISSING"
params: Array [0]
message: "Not a valid parameter definition"
path: Array [5]
schemaId: "http://swagger.io/v2/schema.json#"
inner: Array [2]
0: Object
code: "ONE_OF_MISSING"
params: Array [0]
message: "Data does not match any schemas from 'oneOf'"
path: Array [5]
inner: Array [2]
1: Object
code: "OBJECT_MISSING_REQUIRED_PROPERTY"
params: Array [1]
0: "$ref"
message: "Missing required property: $ref"
path: Array [5]
0: "paths"
1: "/api/example"
2: "post"
3: "parameters"
4: "0"
level: 900
type: "Swagger Error"
description: "Not a valid parameter definition"
lineNumber: 23
答案 0 :(得分:0)
Helen指出的问题是我将参数指定为查询参数而不是请求体。这就是我的目标:
swagger: '2.0'
info:
title: Test API
description: Test
version: "1.0.0"
host: api.example.com
schemes:
- https
basePath: /v1
consumes:
- application/json
produces:
- application/json
paths:
/api/example:
post:
description: Endpoint description
parameters:
- name: MyRequest
in: body
description: My parameters
required: true
schema:
$ref: "#/definitions/MyRequest"
responses:
200:
description: error response
schema:
type: array
items:
type: object
default:
description: Unexpected error
schema:
$ref: '#/definitions/Error'
definitions:
MyRequest:
type: object
properties:
param1:
type: array
items:
type: object
param2:
type: string
Error:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
fields:
type: string