OpenApi 3.0 oneOf或allOf

时间:2017-08-10 13:55:07

标签: json inheritance swagger-2.0 openapi

我一直在搜索,并且没有找到很多关于何时在OpenApi 3.0中使用allOf或oneOf的示例或明确说明。
使用allOf和discriminator或oneOf有什么区别?

我的用例如下:
如果未定义deviceType,则无法创建设备。您应该只能创建"移动设备" /" pin device" /" beacon device"其中包含取决于其类型的属性以及协议中的deviceId和名称。

现在我已经实现了allOf版本,它正在运行。但我想知道这是不是我想要的。

  Device:
    type: object
    required:
      - id
      - name
      - deviceType
    discriminator: deviceType
    properties:
      deviceId:
        type: string
        description: The id (UUID) of the device
        readOnly: true
      name:
        type: string
        description: The name of the device
      deviceType:
        type: string
        description: The type of the device
        enum:
          - MobileDevice
          - PinDevice
          - BeaconDevice
  mobileDevice:
    allOf:
      - $ref: '#/definitions/Device'
      - required:
          - platform
          - deviceToken
          - location
        properties:
          platform:
            type: string
            description: |
              The platform of the device, this can be any string
              representing the platform type, for instance 'iOS'
          deviceToken:
            type: string
            description: |
              The deviceToken is the device push notification token
              given to this device by the OS, either iOS or Android for
              identifying the device with push notification
              services.
          location:
            "$ref": "#/definitions/Location"
  pinDevice:
    allOf:
      - $ref: '#/definitions/Device'
      - required:
          - location
        properties:
          location:
            "$ref": "#/definitions/Location"
  beaconDevice:
    allOf:
        - $ref: '#/definitions/Device'
        - required:
            - uuid
            - major
            - minor
          properties:
            uuid:
              type: string
              description: |
                The UUID of the beacon, the purpose is to distinguish iBeacons
                in your network, from all other beacons in
                networks outside your control.
            major:
              type: integer
              description: |
                Major values are intended to identify and
                distinguish a group
              format: int32
              minimum: 0
              exclusiveMinimum: false
              maximum: 65535
              exclusiveMaximum: false
            minor:
              type: integer
              description: |
                Minor values are intended to identify and
                distinguish an individual
              format: int32
              minimum: 0
              exclusiveMinimum: false
              maximum: 65535
              exclusiveMaximum: false

我希望实现我不能在不指定类型的情况下初始化设备,如果它具有特定类型,则需要具有所有必需的属性。

0 个答案:

没有答案