1

I am supposed to pass student-list in a rest operation. The student-list is defined as follow:

- 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

Where each item is defined as follow:

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

As per above representation, I am supposed to pass array of StudentId json ( with style = form and explode = true ).
As per code generated by OpenAPI Generator, The operation requiring Student list, iterates over each StudentId in the list. For each StudentId, it calls toString method to generate json representation StudentId Object
The StudentId Object have following toString method:

@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();
   }

And the toIndentedString method is defined as follows:

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

My question is: Why OpenAPI is not generating correct code. The toString method is not generating valid json ( as the jSON begins with "class StudentId" and it clutter with "\n" }.
This question is not duplicate of existing question Here, I am asking about sending array of json objects with style = form and explode = false. The existing question does not provide solution to my problem.

Tarun
  • 3,162
  • 3
  • 29
  • 45
  • Possible duplicate of [OpenAPI query string parameter with list of objects](https://stackoverflow.com/questions/52892768/openapi-query-string-parameter-with-list-of-objects) – Helen May 20 '19 at 13:30
  • @Helen I reject your claim of duplicate question. Here I am asking about Array of Appliation/json type objects, while the mentioned link talks about deep object. – Tarun May 20 '19 at 13:32
  • How is the query string supposed to look like in the HTTP request URL? (That would clarify if it's a dupe or not.) – Helen May 20 '19 at 13:33
  • @Helen I dont know how it should look like. All I have is OpenAPI spec which i have shared in the question. Please dont mark it duplicate, if you havent worked with OpenAPI – Tarun May 20 '19 at 13:37
  • I do know OpenAPI. There are two options for query param serialization - 1) `?student-list[0].class=class1&student-list[0].rollno=1&student-list[1].class=class2&student-list[0].rollno=2`; 2) `?student-list=[{"class":"class1","rollno":1},{"class":"class2","rollno":2}]` (or, after URL encoding - `?student-list=%5B%7B%22class%22%3A%22class1%22%2C%22rollno%22%3A1%7D%2C%7B%22class%22%3A%22class2%22%2C%22rollno%22%3A2%7D%5D`). From your question it looks like you want (2) but maybe you meant (1)? Just trying to clarify the use case. – Helen May 20 '19 at 13:41
  • @Helen Since by default, style = form and explode = true. I think the url would look like: ?StudentId={"class":"class1","rollno":1}&StudentId={"class":"class2","rollno":2} – Tarun May 20 '19 at 13:46

0 Answers0