当前位置: 首页 > 知识库问答 >
问题:

如何创建一个包含不同类型数组的swagger模式

傅振濂
2023-03-14

我试图为一个包含不同类型对象数组的对象定义一个swagger模式定义。

下面是模板对象(以及所有相关对象类型)的 json 架构。我知道 swagger 不支持 oneOf 谓词,所以我只是想弄清楚如何在 swagger 中描述这个数据结构。我已经尝试了这种语法的许多变体,但没有一个有效,这是我根据规范和这里找到的一些例子所能达到的最接近的:http://json-schema.org/example2.html

swagger: '2.0'
info:
  version: 1.0.0
  title: IDMU
paths:

definitions:
  template:
    type: object
    properties:
      collection:
        type: string
      name:
        type: string
      columnValue:
        type: string
      description:
        type: string
      outputFile:
        type: string
      content:
        type: string
      directives:
        type: array
        items:
          type: object
          oneOf: 
            - 
              $ref: '#/definitions/directiveRequire'
            - 
              $ref: '#/definitions/directiveReplace'
            - 
              $ref: '#/definitions/directiveReplaceRowSql'
            - 
              $ref: '#/definitions/directiveReplaceRowCsv'
            - 
              $ref: '#/definitions/directiveReplaceColSql'
            - 
              $ref: '#/definitions/directiveReplaceColCsv'
            - 
              $ref: '#/definitions/directiveInsertTag'
            - 
              $ref: '#/definitions/directiveInsertCsv'
            - 
              $ref: '#/definitions/directiveInsertSql'
  providerCsv:
    type: object
    properties:
      type:
        type: integer
        maximum: 3
        minimum: 3
      tag:
        type: string
      url:
        type: string
      staticData:
        type: string
  providerTag:
    type: object
    properties:
      type:
        type: integer
        maximum: 2
        minimum: 2
      tag:
        type: string
      condition:
        type: integer
      list:
        type: boolean
      value:
        type: string
  providerSql:
    type: object
    properties:
      type:
        type: integer
        maximum: 1
        minimum: 1
      source:
        type: string
      columns:
        type: string
      from:
        type: string
      where:
        type: string
  directive:
    type: object
    discriminator: type
    properties:
      type:
        type: integer
      softFail:
        type: boolean
    required:
      - type
  directiveRequire:
    type: object
    allOf:
      - $ref: '#/definitions/directive'
      - properties:
          tags:
            type: array
            items:
              type: string
  directiveReplace:
    type: object
    allOf:
      - $ref: '#/definitions/directive'
      - properties:
          description:
            type: string
          from:
            type: string
          to:
            type: string
  directiveReplaceRowSql:
    type: object
    allOf:
      - $ref: '#/definitions/directive'
      - properties:
          description:
            type: string
          provider:
            $ref: '#/definitions/providerSql'
  directiveReplaceRowCsv:
    type: object
    allOf:
      - $ref: '#/definitions/directive'
      - properties:
          description:
            type: string
          provider:
            $ref: '#/definitions/providerCsv'
  directiveReplaceColCsv:
    type: object
    allOf:
      - $ref: '#/definitions/directive'
      - properties:
          description:
            type: string
          fromColumn:
            type: string
          toColumn:
            type: string
          provider:
            $ref: '#/definitions/providerCsv'
  directiveReplaceColSql:
    type: object
    allOf:
      - $ref: '#/definitions/directive'
      - properties:
          description:
            type: string
          fromColumn:
            type: string
          toColumn:
            type: string
          provider:
            $ref: '#/definitions/providerSql'
  directiveInsertTag:
    type: object
    allOf:
      - $ref: '#/definitions/directive'
      - properties:
          description:
            type: string
          notLast:
            type: array
            items:
              type: string
          onlyLast:
            type: array
            items:
              type: string
          provider:
            $ref: '#/definitions/providerTag'
      directiveInsertSql:
        type: object
        allOf:
          - $ref: '#/definitions/directive'
          - properties:
              description:
                type: string
              notLast:
                type: array
                items:
                  type: string
              onlyLast:
                type: array
                items:
                  type: string
              provider:
                $ref: '#/definitions/providerSql'
      directiveInsertCsv:
        type: object
        allOf:
          - $ref: '#/definitions/directive'
          - properties:
              description:
                type: string
              notLast:
                type: array
                items:
                  type: string
              onlyLast:
                type: array
                items:
                  type: string
              provider:
                $ref: '#/definitions/providerCsv'

共有3个答案

匡晟
2023-03-14

返回视频数组的示例API响应

  responses:
    '200':
      description: An array of videos
      schema:
        type: array
        items:
          $ref: '#/definitions/Video' 

参考

https://swagger.io/docs/specification/adding-examples/

章增
2023-03-14

您可以设置项目:对基类型的引用。在从 swagger 导出期间,继承模型将因语言而异,但在实践中,如果您希望能够接受继承同一基模型的多个子类,则方法定义将使用基模型指定可接受的参数类型。

招摇片段-

definitions:
  template:
    type: object
    properties:
      collection:
        type: string
      ...
      directives:
        type: array
        items:
          $ref: '#/definitions/directive'
  directive:
    type: object
    discriminator: type
    properties:
      type:
        type: integer
      softFail:
        type: boolean
    required:
      - type
  directiveRequire:
    allOf:
    - $ref: '#/definitions/directive'
    - type: object
      properties:
        tags:
          type: array
          items:
            type: string
  directiveReplace:
    allOf:
    - $ref: '#/definitions/directive'
    - type: object
      properties:
        description:
          type: string
        from:
          type: string
        to:
          type: string

伪代码-

class template {
  // all the other properties
  directive[] directives;
  function addDirective(directive newDirective) {
    this.directives.push(newDirective);
  }
}

class directive {
  int type;
  boolean softFail;
}

class directiveRequire inherits directive {
 //inherits type, softFail
 string[] tags;
}

class directiveReplace {
  //inherits type, softFail
  string description;
  string from;
  string to;
}

template templateOne = new template();

directiveReplace directiveOne = new directiveReplace();
directiveOne.type = "replace";
directiveOne.softFail = false;
directiveOne.description = "first directive replace";
directiveOne.from = "first";
directiveOne.to = "one";

directiveRequire directiveTwo = new directiveRequire();
directiveTwo.type = "require";
directiveTwo.softFail = true;
directiveTwo.tags = ["second","directive"];

templateOne.addDirective(directiveOne);
templateOne.addDirective(directiveTwo);
卫鸿朗
2023-03-14

OpenAPI 3.0 支持 oneOfanyOf

      schema:
        type: array
        items:
          oneOf:
            - $ref: '#/components/schemas/directiveRequire'
            - $ref: '#/components/schemas/directiveReplace'
            - ...

在OpenAPI 2.0中,您可以将具有不同属性的对象定义为< code>type: object(自由形式的对象)。对于您的情况,您可能需要这样做:

      schema:
        type: array
        items:
          type: object
 类似资料:
  • 我想在swagger中建模一个包含一组不同类型对象的响应对象,如下所示: 我尝试了下面的解决方案,但它将所有属性包装在一个对象中 { [ { "用户": [], "客户": [] } ] }.

  • 问题内容: 我正在寻找以下所有替代方案,以创建一个包含1到N的JavaScript数组,其中N仅在运行时才知道。 在我看来,应该有一种没有循环的方法。 问题答案: 如果我能得到想要的结果,则需要一个数字数组,以后可以循环使用。 如果这是您所需要的,您可以代替吗? 然后在您要使用它时…(未优化,例如) 例如,如果您不需要在数组中 存储 任何内容,则只需要一个长度合适的容器即可进行迭代……这可能会更容

  • 问题内容: 我想创建一个包含唯一字符串的数组。我怎样才能做到这一点? 问题答案: 如果要收集唯一元素,则为Set数据类型。Go没有集合数据类型,但是您可以使用来充当集合。 对于“好的”集合,请使用具有值类型(带有值)的映射并利用零值。对于内存占用最小的集合,请使用具有值类型的映射,因为类型的值不占用内存;并使用逗号分隔的习惯用法来判断值是否在集合/映射中。 这是set的“不错”版本的样子。代替切片

  • 问题内容: 如何创建泛型类型的数组?通用方法如何工作?它返回通用数组的副本。因此可以创建通用数组。但是如何?怎么能写一个类似的方法呢? 问题答案: 如果需要在运行时创建它,则至少需要在此时知道类型,因此可以使用以下方法: where 是泛型类型,是的类,并且是初始大小。 这里的文件

  • 问题内容: 在下面的示例中,我希望所有元素都是元组,为什么当元组仅包含单个字符串时,它会转换为字符串? 问题答案: 因为前两个元素不是元组;他们只是字符串。括号不会自动使它们成为元组。你必须在字符串后添加一个逗号,以指示python它应该是一个元组。 要修复示例代码,请在此处添加逗号: 从Python文档: 一个特殊的问题是包含0或1项的元组的构造:语法具有一些额外的怪癖来容纳这些项。空元组由一对

  • 问题内容: 我有一个我必须验证问题值的对象,这些对象的某些属性是自定义对象的数组。这样一来,这将涉及到数组的各个元素。为每个元素执行吸气剂,例如: 这是我需要达到的。我已使用带有属性列表的枚举以以下方式提取了数据。 致电: 我将使用数组中不同类型和不同值的多个数组。我想创建一个如下的方法。 这给了我多个错误,这些错误主要与repeatingGrp []有关。数组类型应与枚举名称相同。是否可以创建这