簡體   English   中英

如何在openAPI JSON中指定名稱不是static的密鑰

[英]How to specify the key in openAPI JSON whose name is not static

我想創建一個具有 openAPI 規范的 JSON。 api/endpoint的示例負載格式如下,

"abc": {"name": "PA", "id": "21"}

所以這里是 openapi JSON 格式,

{
  "openapi": "3.0.0",
  "info": {...},
  "paths": {
    "api/endpoint": {
       "put": {
         "requestBody": {
           "required": true,
           "content": {
             "application/json": {
               "schema": {
                 "$ref": "#/components/schemas/putSchema"
               }
             }
           }
         }
       }
     }
  },
  "components": {
    "schemas": {
      "putSchema": {
        "abc": {        ----------> * 
          "type": "object",
          "properties": {
            "name": {...},
            "id": {...}
          }
        }
      }
    }
  }
}

但我的有效負載可能會有所不同,例如,

"abc": {"name": "PA", "id": "21"}

"xyz": {"name": "ST", "id": "35"}

"def": {"name": "UV", "id": "94"}

不確定如何為動態密鑰名稱定義 JSON 格式(此處為 abc/xyz/def)-(即)在 JSON 中的 * 處填寫什么)。

所以我的查詢是如何在JSON中指定名稱不是static的密鑰。

在您的示例中, putSchema是一個字符串到對象 map,其中“abc”/“xyz”/etc。 是 map 中的鍵。使用additionalProperties關鍵字定義映射:

"putSchema": {
  "type": "object",
  "additionalProperties": {
    "type": "object",
    "properties": {
      "name": { ...},
      "id": { ... }
    }
  }
}

如果您的負載只有一個根密鑰(例如,只有“abc”但不能同時使用“abc”和“xyz”),您可以將"maxProperties": 1添加到您的putSchema以限制根密鑰的數量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM