zipmark/rspec_api_documentation

How to define complex request body

vaibhavatul47 opened this issue · 1 comments

How can I document following request body schemas:
1.

      {
        "data": [
          {
            "key1": "foo",
            "key2": "bar;"
          },
          {
            "key1": "baz",
            "key3": 123456,
            "key5": true
          }
        ]
      }
{
  "data": {
    "key1": [
      {
        "key2": "foo",
        "key3": [
          {
            "key4": "bar",
            "key5": {
              "key6": 123
            }
          }
        ]
      },
      {
        "key2": "foofoo",
        "key3": [
          {
            "key4": "barbar",
            "key5": {
              "key6": 999
            }
          }
        ]
      },
    ]
  }
}

For the second one I used below code, but I am unable to generate doc for the first one:

with_options scope: :data, with_example: true do
  parameter :key1, type: :array, items: { type: :object }
end

let(:key1) do
  [ { "key2": "foo",
      "key3": [{ "key4": "bar",
                 "key5": { "key6": 123 } }] },
    { "key2": "foofoo",
      "key3": [{ "key4": "barbar",
                 "key5": { "key6": 999 } }] }]
end

The examples shown in Readme are very basic and doesn't explain nested objects.
I am using OpenApi format.

I generated doc for first case using following code:

parameter :data, "request payload", in: :body, with_example: true

let(:raw_post) { params.to_json }

context "200 response code" do
  let(:data) do
    [
      {
        "key1": "foo",
        "key2": "bar;"
      },
      {
        "key1": "baz",
        "key3": 123456,
        "key5": true
      }
    ]
  end
  example "it does smth" do
    ..
  end
end