如何在 RAML 1.0 中重用 header 定义

How to reuse header definitions in RAML 1.0

我有一个 RAML 1.0 规范,我在其中定义了多个资源。每个资源都有相同的一组 headers。

我需要使用什么 RAML 构造来声明 headers 一次并在各种资源定义中重用它们?

例如

/read/records:
  post:
    description: api for reading records
    queryParameters: 
      table_name: string
    headers: 
            Authorization: 
                displayName: Authorization
                description: Basic authentication base 64 encoded string
                type: string
                required: true
            Tenant-Id: 
                displayName: Tenant-Id
                description: Tenant id for multi-tenant environments
                type: string
                required: true
            Content-type: 
                displayName: Content-type
                description: either xml or json
                type: string
                required: true
/adjust/records:
  post:
    description: api for editing records
    headers: 
            Authorization: 
                displayName: Authorization
                description: Basic authentication base 64 encoded string
                type: string
                required: true
            Tenant-Id: 
                displayName: Tenant-Id
                description: Tenant id for multi-tenant environments
                type: string
                required: true
            Content-type: 
                displayName: Content-type
                description: either xml or json
                type: string
                required: true

谢谢!

你可以使用 traits

#%RAML 1.0
traits: 
  hasHeaders: 
    headers: 
      Authorization: 
        displayName: Authorization
        description: Basic authentication base 64 encoded string
        type: string
        required: true
      Tenant-Id: 
        displayName: Tenant-Id
        description: Tenant id for multi-tenant environments
        type: string
        required: true
      Content-type: 
        displayName: Content-type
        description: either xml or json
        type: string
        required: true
/read/records:
  post:
    is: ["hasHeaders"]
    description: api for reading records
    queryParameters: 
      table_name: string
/adjust/records:
  post:
    is: ["hasHeaders"]
    description: api for editing records

Traits 必须表示一种行为,并通过路由和动词应用。对我来说 header 适用于您可以定义资源类型的任何路由/动词:

#%RAML 1.0 ResourceType
get?:
  headers: 
    Authorization: 
        displayName: Authorization
        description: Basic authentication base 64 encoded string
        type: string
        required: true
    Tenant-Id: 
        displayName: Tenant-Id
        description: Tenant id for multi-tenant environments
        type: string
        required: true
    Content-type: 
        displayName: Content-type
        description: either xml or json
        type: string
        required: true
post?:
  headers: 
    Authorization: 
        displayName: Authorization
        description: Basic authentication base 64 encoded string
        type: string
        required: true
    Tenant-Id: 
        displayName: Tenant-Id
        description: Tenant id for multi-tenant environments
        type: string
        required: true
    Content-type: 
        displayName: Content-type
        description: either xml or json
        type: string
        required: true