Search code examples
raml

Reuse enum definition in RAML


In Swagger/OpenAPI 3.0, it is possible to reuse an enum definition?

Example - share Color enum definition:

openapi: 3.0.0
info:
  description: "This is a sample server Petstore server.  You can find out more about     Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).      For this sample, you can use the api key `special-key` to test the authorization     filters."
  version: "1.0.0"
  title: "Swagger Petstore"
  termsOfService: "http://swagger.io/terms/"
  contact:
    email: "[email protected]"
  license:
    name: "Apache 2.0"
    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
paths:
  /products:
    get:
      parameters:
      - in: query
        name: color
        required: true
        schema:
          $ref: '#/components/schemas/Color'
      responses:
        '200':
          description: OK
  /products2:
    get:
      parameters:
      - in: query
        name: color
        required: true
        schema:
          $ref: '#/components/schemas/Color'
      responses:
        '200':
          description: OK
          
components:
  schemas:
    Color:
      type: string
      enum:
        - black
        - white
        - red
        - green
        - blue

I want to do the same in RAML, but can't find a solution.


Solution

  • In RAML 1 you can do this:

    #%RAML 1.0
    title: Example API
    version: v1
    types:
      platform:
        enum:
          - win
          - mac
    /installer:
      get:
        queryParameters:
          platform:
            type: platform
    /foo:
      get:
        queryParameters:
          platform:
            type: platform
    

    You can separate the type/s definition into it's own file and include it. Using any of the techniques described here: https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md/#modularization

    Also, you can define a trait with query parameters to be reused in many resources. More about that here: https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md/#resource-types-and-traits