简体   繁体   English

使用yaml对Swagger API说明进行“模板化”

[英]'Templating' on Swagger API Description with yaml

Is it possible to use templating w/ swagger. 是否可以使用带有摇摆的模板。 How is it done. 怎么做
I do not want to repeat the three properties time , len and off each time. 我不想每次都重复三个属性timelenoff

Have a look at the end of this post where I made up a 'template' for explanation. 看看这篇文章的末尾,我在那里做了一个“模板”进行解释。

More Detail: 更多详情:

I have a JSON response structure which always returns a JSON always with the same properties but only the content of data is subject to change. 我有一个JSON响应结构,该结构总是返回始终具有相同属性的JSON,但仅数据内容可能会更改。

data could be an array, could be a string, a number, null or an object. 数据可以是数组,可以是字符串,数字,空值或对象。
That depends on the Api's function handling. 这取决于Api的功能处理。

{
  time: "2019-02-01T12:12:324",
  off: 13,
  len: 14,
  data: [
    "Last item in the row of 14 items :-)"
  ]
}

See at the end of this post for my example of the Swagger definition. 有关我的Swagger定义示例,请参见本文末尾。 It is a yaml which can be pasted into the swagger editor at https://editor.swagger.io/ 它是一个Yaml ,可以粘贴到swagger编辑器中, 网址https://editor.swagger.io/

In the swagger documentation ( yaml ) I do not want to repeat the statically reoccuring items, which will not change in their functionality for any other request. 在庞大的文档( yaml )中,我不想重复静态重复出现的项,对于任何其他请求,它们都不会改变其功能。

Let me know, if the question is not precisely enough to understand. 让我知道,如果这个问题不够准确,难以理解。

swagger: "2.0"
info:
  description: ""
  version: 1.0.0
  title: "Templating?"
  contact:
    email: "someone@somewhere.com"

host: localhost
basePath: /api

paths:

  /items:
    get:
      summary: "list of items"
      produces:
        - application/json
      responses:
        200:
          description: "successful operation"
          schema:
            $ref: "#/definitions/Items"

  /item/{id}:
    get:
      summary: "specific item"
      produces:
        - application/json
      parameters: 
        - name: id
          in: path
          description: "ID of the demanded item"
          required: true
      responses:
        200:
          description: "successful operation"
          schema:
            $ref: "#/definitions/Item"

definitions:

  Items:
    type: object
    description: ""
    properties:
      time: 
        type: string
        format: date-time
        description: "date-time of the request"
      off:
        type: integer
        description: "index 0 based offset of list data"
        default: 0
      len:
        type: integer
        description: "overall amount of items returned"
        default: -1
      data:
        type: array
        items:
          $ref: "#/definitions/ListingItem"

  Item:
    type: object
    description: ""
    properties:
      time: 
        type: string
        format: date-time
        description: "date-time of the request"
      off:
        type: integer
        description: "index 0 based offset of list data"
        default: 0
      len:
        type: integer
        description: "overall amount of items returned"
        default: -1
      data:
        $ref: "#/definitions/InfoItem"

  ListingItem:
    type: integer
    description: "ID of the referenced item"

  InfoItem:
    type: object
    properties:
      id:
        type: string
      text:
        type: string

Based on @Anthon's answer it came to my mind that this is somewhat the construct I would need. 根据@Anthon的回答,我想到这是我需要的构造。 Actually it is inheriting from a 'template': 实际上,它是从“模板”继承的:

...
templates:

  AbstractBasicResponse:
    properties:
      time: 
        type: string
        format: date-time
        description: "date-time of the request"
      off:
        type: integer
        description: "index 0 based offset of list data"
        default: 0
      len:
        type: integer
        description: "overall amount of items returned"
        default: -1

definitions:

  Items:
    type: object
    extends: AbstractBasicResponse
    properties:
      data:
        type: array
        items:
          $ref: "#/definitions/ListingItem"

  Item:
    type: object
    extends: AbstractBasicResponse
    properties:
      data:
        $ref: "#/definitions/InfoItem"

  ListingItem:
    type: integer
    description: "ID of the referenced item"

  InfoItem:
    type: object
    properties:
      id:
        type: string
      text:
        type: string
...

You might not have to revert to full templating, there are two things within YAML that help with "undoubling" recurring data: anchors/aliases and merge keys. 您可能不必恢复到完整的模板,YAML中有两件事可帮助“重复处理”重复数据:锚定/别名和合并键。

An example of an anchor (introduced by & ) referenced by an alias ( * ) would be: 由别名( * )引用的锚 (由&引入)的示例为:

definitions:

  Items:
    type: object
    description: ""
    properties:
      time: 
        type: string
        format: date-time
        description: "date-time of the request"
      off: &index
        type: integer
        description: "index 0 based offset of list data"
        default: 0
      len: &amount
        type: integer
        description: "overall amount of items returned"
        default: -1
      data:
        type: array
        items:
          $ref: "#/definitions/ListingItem"

  Item:
    type: object
    description: ""
    properties:
      time: 
        type: string
        format: date-time
        description: "date-time of the request"
      off: *index
      len: *amount
  data:

A YAML parser needs to be able to handle this, but since the alias points to the same object after loading, the code using the data might no longer work the same because of side effect in some cases depending on how the loaded data is processed. YAML解析器需要能够处理此问题,但是由于别名在加载后指向同一个对象,因此使用数据的代码可能不再起作用,因为在某些情况下,副作用取决于加载数据的处理方式。
You can have multiple aliases referring to the same anchor. 您可以有多个别名引用同一个锚。

The merge key ( << ) is a special key in a mapping with which you can pre-load that mapping where it occurs with a bunch of key-value pairs. 合并键<< )是映射中的特殊键,通过该键,您可以通过一堆键/值对预加载该映射。 This is most effective when used with a anchor/alias. 与锚点/别名一起使用时,这是最有效的。 With that you you some finer control and you could do: 有了它,您可以更好地控制并且可以执行以下操作:

  len: &altlen
    type: integer
    description: "overall amount of items returned"
    default: -1

and then 接着

  len:
    <<: &altlen
    default: 42

Which would then be the same doing: 然后,这将是相同的:

  len:
    type: integer
    description: "overall amount of items returned"
    default: 42

Merge keys are normally resolved at load time by the YAML parser, so there are no potential side-effects when using those even though they involve anchors and aliases. 合并键通常在加载时由YAML解析器解析,因此,使用合并键时,即使它们涉及锚和别名,也没有潜在的副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM