【问题标题】:How to set EXAMPLE value for array of a defined component in OpenAPI YAML?如何为 OpenAPI YAML 中已定义组件的数组设置示例值?
【发布时间】:2021-10-14 16:36:23
【问题描述】:

我在 OpenAPI 中定义了以下组件:

 Template:
      type: object
      properties:
        Callback:
          description: 'If set, this url will be called with a POST when a job completes. If the text "{guid}" is in the url, that text will be replaced with the Guid for the callback.'
          type: string
        OutputFormat:
          type: string
          description: 'Generate the document in the provided format.'
          enum: [pdf, docx, xlsx, pptx, txt, html, prn, csv, rtf, jpg, png, svg, eps, bmp, gif]
        Data:
          description: 'The source of the template- embedded or external. Embed template as a Base64-encoded string.'
          type: string
          format: base64
        ConnectionString:
          description: "Set this to provide the template as a connection string of the template's location."
          type: string
        Format:
          type: string
          description: 'Format of the template. Auto-determined if not provided.'
          enum: [docx, html, xlsx, pptx]
        Properties:
          description: "Windward properties for this document. These override any properties set in the configuration file on the server side."
          type: array
          items:
            $ref: '#/components/schemas/Property' 
          xml: 
            wrapped: true 
        Parameters:
          description: "A set of input parameters for this document. The parameters are global and shared among all data sources."
          type: array
          items:
            $ref: '#/components/schemas/Parameter'
          xml: 
            wrapped: true 
        Datasources:
          description: "The datasources to apply to the template. The datasources are applied simultaneously."
          type: array
          items:
            $ref: '#/components/schemas/Datasource'
          xml: 
            wrapped: true 
        Tag:
          type: string 
          description: "Anything you want. This is passed in to the repository & job handlers and is set in the final generated document object. The RESTful engine ignores this setting, it is for the caller's use."
        TrackImports:
          type: boolean
          description: "Return all imports with the generated document."
        TrackErrors:
          type: integer
          minimum: 0
          maximum: 3
          description: "Enable or disable the error handling and verify functionality."
        MainPrinter:
          type: string
          description: "If you are using printer output use to specify main printer. Printer must be recognized by Network"
        FirstPagePrinter:
          type: string
          description: "Set first page printer if main printer is already set"
        PrinterJobName:
          type: string
          description: "Assign print job name"
        PrintCopies:
          type: integer
          description: "Set number of copies to print"
        PrintDuplex:
          type: string
          description: "Selects the printer duplex mode.  Only if supported by the printer."

如果您查看Datasources 条目,它是Datasource 组件的数组:

Datasources:
   description: "The datasources to apply to the template. The datasources are applied 
   simultaneously."
          
   type: array
   items:
     $ref: '#/components/schemas/Datasource'

我正在尝试为 POST 请求定义一个示例请求正文(您发送的正文是我在上面显示的模板组件)。当我尝试定义示例值时,它看起来是这样的:

这就是它呈现的内容:

问题在于它将其显示为字典字典(带有“{}”括号)。我需要它是一个字典数组(外面有“[]”)。有谁知道怎么做?

我试过这样做:

但 Swagger 编辑器不喜欢这样。有什么想法吗?

为了更清楚,这就是我想要做的:

# I NEED THIS
Datasources: [
  Datasource: {
    Name: "...",
    Type: "..."
  }
]

# INSTEAD OF THIS
Datasources: {
  Datasource: {
    Name: "...",
    Type: "..."
  }
}

【问题讨论】:

标签: yaml swagger openapi


【解决方案1】:

这是在 YAML 中编写对象数组(序列)的方法。请注意每个数组项前的破折号。

example:
  ...
  Datasources:
    - Name:
      Type: json
      ConnectionString: some value
    - Name: Name2
      Type: yaml
      ConnectionString: some other value
  ...

你也可以使用JSON数组语法[ ... ],但是在这种情况下,数组必须写为有效的JSON,即数组项必须用逗号分隔,嵌套对象必须写为{ ... },所有键用引号括起来的名称和字符串值,等等。

example:
  ...
  Datasources: [
    {
      "Name": null,
      "Type": "json",
      "ConnectionString": "some value"
    },
    {
      "Name": "Name2",
      "Type": "yaml",
      "ConnectionString": "some other value"
    }
  ]
  Tag: ...

【讨论】:

  • 谢谢!这帮助很大!
猜你喜欢
  • 1970-01-01
  • 2019-09-22
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多