卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35323本站已运行395

openAPI规范中如何描述go的datatypes.JSON数据类型?

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
卓越飞翔博客
上一篇: Zabbix Agent 2 在尝试根据官方说明处理测试插件时抛出错误
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏