首页 > 解决方案 > OpenAPI3:如何在查询中传递 json 对象数组

问题描述

我应该在休息操作中通过学生名单。学生列表定义如下:

- name: student-list
              in: query
              description: Id of the Student of the target NF
              content:
                application/json:
                  schema:
                    type: array
                    items:
                      $ref: 'TS29571_CommonData.yaml#/components/schemas/StudentId'
                    minItems: 1

其中每个项目定义如下:

StudentId:
      type: object
      properties:
        class:
          type: string
        rollno:
          type: integer

根据上述表示,我应该传递 StudentId json 数组(带有 style = form 和 explode = true )。
根据 OpenAPI Generator 生成的代码,需要 Student 列表的操作会遍历列表中的每个 StudentId。对于每个 StudentId,它调用 toString 方法生成 json 表示 StudentId 对象
StudentId 对象有以下 toString 方法:

@Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class StudentId {\n");
    sb.append("    class: ").append(toIndentedString(mcc)).append("\n");
    sb.append("    rollno: ").append(toIndentedString(mnc)).append("\n");
    sb.append("}");
    return sb.toString();
   }

并且 toIndentedString 方法定义如下:

private String toIndentedString(Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }

我的问题是:为什么 OpenAPI 没有生成正确的代码。toString 方法没有生成有效的 json (因为 jSON 以“class StudentId”开头并且它与“\n”杂乱无章}。
这个问题与现有问题不重复在这里,我问的是发送带有样式 = 的 json 对象数组form and explode = false. 现有问题没有为我的问题提供解决方案。

标签: javaopenapiopenapi-generator

解决方案


推荐阅读