Как определить параметр, принимающий массив произвольных объектов в OpenAPI (Swagger)?

Можно ли в моей спецификации 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

person Joey Eng    schedule 15.02.2018    source источник
comment
В вашем определении указано, что объекты входят в строку запроса - это предназначено или объекты должны входить в тело запроса POST?   -  person Helen    schedule 16.02.2018
comment
Вот в чем проблема. Я изменил ценность тела, и это позаботилось об этом. Это результат копирования и вставки и моего отсутствия знаний о ручном редактировании OpenAPI. Спасибо еще раз!   -  person Joey Eng    schedule 16.02.2018


Ответы (1)


Проблема, как указала Хелен, заключалась в том, что я указывал параметры как параметры запроса, а не тело запроса. Вот что я хотел:

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
person Joey Eng    schedule 16.02.2018