Skip to main content

I'm defining an API with common response structure that contains an object with 3 attributes. The first attribute is an object that contains information about pagination, order and others, the second is data response ( dynamic for each data type ) and the third attibute is a boolean.

 

I want to define it dynamic because if the first attribute changes, I only change one time ( not in every example ).

 

Is this possible in RAML?

1 件の回答
  1. 2023年10月29日 8:32

    Hello @Josep Campos Gadea​ ,

    Yes, RAML (RESTful API Modeling Language) supports reuse and modularization, which makes it an excellent choice for defining APIs with common structures. You can use RAML types and traits to achieve this.

    Here's an example of how you can model your common response structure in RAML:

     

    1. Define a Library for Common Types

    #%RAML 1.0 Library

    usage: This library contains common data types.

     

    types:

     

    PaginationInfo:

    type: object

    properties:

    currentPage: number

    totalPages: number

    itemsPerPage: number

    order: string

    # ... other pagination attributes

     

    CommonResponse:

    type: object

    properties:

    pagination: PaginationInfo

    data: any

    success: boolean

    2.Use the Library in Your RAML API Definition

    #%RAML 1.0

    title: My API

    baseUri: http://api.example.com/

     

    uses:

    common: ./commonTypes.raml

     

    /resource:

    get:

    responses:

    200:

    body:

    application/json:

    type: common.CommonResponse

    example:

    pagination:

    currentPage: 1

    totalPages: 10

    itemsPerPage: 20

    order: "asc"

    data:

    key1: "value1"

    key2: "value2"

    success: true

0/9000