Javier Alvarez
Javier Alvarez

Reputation: 1439

Add custom default sub paths to endpoints in RAML

In my REST application model, many of my entities include an interface "Auditable". This forces me to add these sub-paths to those entities endpoints:

/myEntityResource:

  #... boring code ...

  /createdBy:
    get:
      responses:
        200:
          body:
            application/hal+json:
              example: !include samples/userAccount.json
  /lastModifiedBy:
    get:
      responses:
        200:
          body:
            application/hal+json:
              example: !include samples/userAccount.json

The ideal solution should be to add a trait "auditable" to the endpoints, but by RAML definition traits are applicable only at verb level.

Another ideal option for me should be to define a resource type "auditableItem" that includes both paths, but RAML definition doesn't allow to add paths to resource types.

My best approach at the moment is to add both paths to every endpoint and include the body in a separate file:

/createdBy: !include auditableBody.raml
/lastModifiedBy: !include auditableBody.raml

Is there any better way?

Upvotes: 1

Views: 708

Answers (1)

wwulfric
wwulfric

Reputation: 531

The short answer is "Yes".

According to http://apiworkbench.com/docs/#creating-resource-type, you can refactor it to:

#%RAML 1.0 
title: Auditable Example
version: 1

resourceTypes:
  Auditable-createdBy:
    get:
      responses:
        200:
          body:
            application/hal+json:
              # example: !include samples/userAccount.json
  Auditable-lastModifiedBy:
    get:
      responses:
        200:
          body:
            application/hal+json:
              # example: !include samples/userAccount.json

/myEntityResource:
  /createdBy: 
    type: Auditable-createdBy
  /lastModifiedBy: 
    type: Auditable-lastModifiedBy

And you can move the resourceTypes to an independent file, of course.

Upvotes: 1

Related Questions