首頁  >  文章  >  後端開發  >  openAPI規範中如何描述go的datatypes.JSON資料型別?

openAPI規範中如何描述go的datatypes.JSON資料型別?

WBOY
WBOY轉載
2024-02-11 08:54:08431瀏覽

openAPI規範中如何描述go的datatypes.JSON資料型別?

php小編百草為您介紹openAPI規範中如何描述go的datatypes.JSON資料型態。在openAPI規範中,使用Schema Object來描述資料類型。對於go中的datatypes.JSON資料型別,可以使用type為"string",format為"json"來描述。這樣可以明確指定該欄位的資料類型為JSON格式的字串。此外,還可以使用example欄位來提供範例值,幫助開發者更好地理解該資料類型的結構和用法。透過合理使用openAPI規範,可以準確地描述go中的datatypes.JSON資料類型,為開發者提供清晰的介面文件和資料類型定義。

問題內容

我有一個 golang-gin 專案。其中有這樣的結構:

type Value struct { 
    gorm.Model 
    QuesAns       datatypes.JSON json:"ques_ans" 
}

QuesAns 欄位應具有這三種類型中任一種的 JSON。

"ques_ans": {
    "receiver.ques": [
         "Q1",
         "Q2"
     ], 
    "receiver.ans": [
         "Ans1",
         "Ans2",
         "Ans3" 
     ]
 }
"ques_ans": {
     "id": "1",
     "receiver.sid": "2743dfjfh87",
     "receiver.ques": [
         "Q1",
         "Q2",
         "Q3" 
     ] 
 }
"ques_ans": {
     "receiver.ques_key": [
         "1",
         "2" 
     ],
     "receiver.ans_key": [
         "13",
         "20" 
     ] 
 }

您如何描述整合開放 API 規格?

我嘗試了多種類型,但無法同步所有類型,因為 JSON 可以是不同類型,而且只有這三種類型有效。

解決方法

好的!我是這樣解決的:

components:
  schemas:
    Value:
      type: object
      properties:
        ques_ans:
          oneOf:
            - $ref: '#/components/schemas/Type1'
            - $ref: '#/components/schemas/Type2'
            - $ref: '#/components/schemas/Type3'
    Type1:
      type: object
      properties:
        receiver.ques:
          type: array
          items:
            type: string
        receiver.ans:
          type: array
          items:
            type: string
    Type2:
      type: object
      properties:
        id:
          type: string
        receiver.sid:
           type: string
        receiver.ques:
          type: array
          items:
            type: string
    Type3:
      type: object
      properties:
        receiver.ques_key:
          type: array
          items:
            type: string
        receiver.ans_key:
          type: array
          items:
            type: string

以上是openAPI規範中如何描述go的datatypes.JSON資料型別?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除