没有固定属性列表的对象的Swagger Yaml模式定义

Jai*_*ime 4 schema json yaml dicom swagger

我在Swagger的帮助下实现了API优先应用程序.要返回的最重要的对象之一是DICOM对象,它返回具有灵活名称的属性集合,例如:

{ 
  "00080005": {"vr":"CS","Value":["ISO_IR 100"]},
  "00080020": {"vr":"DA","Value":["20160602"]},
  "00080030": {"vr":"TM","Value":["171855.7490"]},
  "00080050": {"vr":"SH","Value":["1234"]},
  "00080090": {"vr":"PN","Value":[{"Alphabetic":"Parikh MD^Anush^M"}]}
}
Run Code Online (Sandbox Code Playgroud)

所以我不知道所有属性的提前(00080005,00080030,等等)的名称虽然文件结构非常均匀.

我的具体问题是:这种JSON文档的模式定义是什么.

我试过以下没有成功:

definitions:
  DicomMetadataJson:
    type: object
    patternProperties:
      ^\d{8}:
        type: object      
Run Code Online (Sandbox Code Playgroud)

但Swagger编辑器返回如下错误:

代码:"OBJECT_ADDITIONAL_PROPERTIES"

消息:"不允许其他属性:patternProperties"

description:"JSON Schema对象的确定性版本."

Arn*_*ret 6

OpenAPI(fka.Swagger)仅使用JSON Schema v4的一个子集,遗憾的是,它没有提出patternProperties.

但是,如果提供的示例是地图,您可以使用additionalProperties以下方法对其进行描述:

swagger: "2.0"
info:
  version: 1.0.0
  title: Hashmap

paths: {}

definitions:
  DicomMetadataJson:
    additionalProperties:
      properties:
        vr:
          type: string
        Value:
          type: array
          items:
            type: string
Run Code Online (Sandbox Code Playgroud)

密钥未定义,应该是一个字符串(因此您不能强制执行它的格式).

请注意,SwaggerUI Swagger UI暂时不渲染它们.此处跟踪问题https://github.com/swagger-api/swagger-ui/issues/1248

同时,您可以使用此技巧定义相同类型的地图对象的非必需属性(在下面的示例中为默认值),并在描述中给出一些提示:

swagger: "2.0"
info:
  version: 1.0.0
  title: Hashmap

paths: {}

definitions:
  MetaData:
    properties:
      vr:
        type: string
      Value:
        type: array
        items:
          type: string

  DicomMetadataJson:
    description: 'A <string,MetaData> map, default key is only for documentation purpose'
    properties:
      default:
        $ref: '#/definitions/MetaData'
    additionalProperties:
      $ref: '#/definitions/MetaData'
Run Code Online (Sandbox Code Playgroud)

关于引用架构公开了两种类型的字段.固定字段,具有声明的名称,以及图案化字段,用于声明字段名称的正则表达式模式.只要每个都有唯一的名称,图案化的字段可以有多次出现.,它涉及OpenAPI规范本身的格式,而不是使用OpenAPI规范描述的API使用的对象.