3

My API has a default envelope for all collection-type resources, like the following:

{
  "data": [{},{},{}],
  "self": "http://api-url/members",
  "total": 120,
  "limit": 10,
  "offset": 0
}

Is it possible to define this using RAML? Inside a resourceType? How?

stefanobaldo
  • 1,957
  • 6
  • 27
  • 40

2 Answers2

1

It depends on what you are using for specifying your API's entities:

  • JSON Schema Draft 3 - Create a base schema with these common fields and use the extends mechanism in each concrete schema,
  • JSON Schema Draft 4 - Create a shared schema with these common fields and use the allOf mechanism to mix it in each concrete schema,
  • RAML 1.0 Types - Create a common type and refer to it in each concrete type via the type attribute.
David Dossot
  • 33,403
  • 4
  • 38
  • 72
1

With RAML 1 you can use types and inheritance to define your envelope and data types. And resource types for applying to all collection-type resources, for example:

types:
    person:
        type: object
        properties:
            name:
            age:
    car:
        type: object
        properties:
           model:
           brand:
    envelop:
        type: object
        properties:
            data: person[] | car[]
            self: string
            total: integer
            limit: integer
            offset: integer
resourceTypes:
    - collection:
        get:
            responses:
                200:
                    body:
                        type: envelope
/users:
    type: collection
    get:

More info here, here and here

Pedro
  • 1,875
  • 12
  • 15