我有一个高朗杜松子wine 项目.其中有一个这样的 struct :

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规范?

我try 了几种类型,但无法同步所有类型,因为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
    

Go相关问答推荐

如何在另一个文件夹中使用Delve运行二进制文件?

Golang html/模板&需要错误数量的参数1在模板中使用';调用';获得0&q;

+在具有html/模板Golang的Base64中

困扰围棋官方巡回赛的S建议所有方法都使用同一类型的接收器

Golang中的泛型 struct /接口列表

如何使用Gio设置标题栏图标

文件路径.Abs()未在结果中提供子目录

为什么要立即调用内联函数,而不仅仅是调用其包含的函数?

在 Go 中将元数据从一个 JPEG 复制到另一个

在 GoLang 中对自定义 struct 体数组进行排序

Go time.Parse 无效的 ISO 日期

以编程方式取消 pyspark dataproc 批处理作业(job)

用于提取 <*n 的正则表达式(其中 n 是一个数字)

从单词中删除特殊字符

go version -m 输出中的箭头符号=>是什么意思?

使用 GO 在侧 tar 文件中提取 tar 文件的最快方法

仅在工作日运行 cron

Golang并发写入多个文件

如何发送带有登录数据的 GET 请求并将 cookie 数据保存到 txt 文件?

如何动态解析 Go Fiber 中的请求正文?