2017-04-07 53 views
0

我有一個RAML 1.0規範,我定義了多個資源。每個資源都具有相同的標題集。如何重用RAML 1.0中的標題定義

RAML的構造是否需要用來聲明一次頭並在各種資源定義中重用它們?

例如,

/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 

謝謝!

回答

1

您可以使用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 
+0

令人驚歎。這只是一個小小的改變。我不必爲hasHeaders提供[]。它的工作原理是:hasHeaders。他們需要別的東西嗎?此外,「是」構造表明什麼?性狀是否也適用於一般性地聲明查詢參數? –

+1

「是」表示應用特徵的位置。當它只有一個時,你可以使用沒有「[]」。您可以將多個特徵應用於資源+動詞,在這種情況下,您可以使用數組「[]」。 – Pedro

0

性狀必須represet行爲和it's申請通過的路線和動詞。對於我來說,標題適用於任何可定義資源類型的路徑/動詞:

#%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