Search code examples
apiblueprintapiary.ioapiarymson

Wrapping Data Structure in a data key API Blueprint / Apiary


So let's say I have a 200 response which body should be:

{
  "data": [
    {
      "id": 1,
      "title": "Activity 1"
    },
    {
      "id": 1,
      "title": "Activity 2"
    }
  ]
}

I have managed to get this behavior of the response body by using this in API Blueprint.

+ Response 200 (application/json)
    + Attributes
        + data (array[Activity])

(Note that I can't add the data key to the data structure itself, because it's only present on the single response. If I need to nest an Activity inside another structure, it shouldn't have the data key.)

This doesn't seem right

The reason why I don't think it's the correct way of doing it, is beacuse of the JSON schema for this response which is:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "data": {
      "type": "array"
    }
  }
}

Note how the actual activity is excluded.

How can I wrap my response in a data key properly, and have it reflected in both the body AND the schema?


Solution

  • You should use this line:

    + data(array[Activity], fixed-type)
    

    The fixed-type keyword fixes the type of the items in the array.