5

I have an API that returns rows of data based on a request. The rows are JSON elements containing name:value pairs. However, the attribute names (and datatypes of the values) need to be fluid and undefined in the spec.

For example:

{
    "row_id": 1234,
    "data": {
        "foo": "bar",
        "date": "2019-07-31",
        "some_number": 5
    }
 }

In this example, the attributes 'row_id' and 'data' are the only fixed things. All the name:values pairs inside the data element can be anything.

I believe I can use open api additionalProperties to describe this, but no examples I can find tell me how or confirm that this is correct.

Does anyone have an idea how to do this or can point me in the right direction?

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
  • Possible duplicate of [Swagger editor dictionary parameter definition](https://stackoverflow.com/q/41867499/113116), [Swagger complex response model with dynamic key value hash maps](https://stackoverflow.com/a/41105628/113116) – Helen Apr 23 '20 at 06:23

1 Answers1

7

It looks like what I was looking for was:

    recordData:
      type: object
      additionalProperties: {}

type: object defines it as a general object, and additionalProperties: {} says the object contains properties that have not been specifically defined.

The docs I finally located also say that additionalProperties: true would also work.

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
  • above is all i had to do. i didn't have to specify json in particular. i believe the `{}` implies it in this situation – Kevin Bedell May 11 '20 at 16:09