raml-org / raml-js-parser-2

(deprecated)
Other
138 stars 53 forks source link

expand() is not applying traits from libraries #163

Closed juancoen closed 8 years ago

juancoen commented 8 years ago

RAML

#%RAML 1.0
title: Mobile Order API
version: 1.0
#baseUri: http://localhost:8081/api
baseUri: http://ec2-52-201-242-128.compute-1.amazonaws.com/mocks/97a819d0-751f-4b70-a5a0-99889ba06eea/api

uses:
  assets:  assets-lib.raml

/products:
  get:
    is: [assets.client-id-required]
    description: Gets a list of all the inventory products.

/orders:
  displayName: Orders
  description: Orders collection resource used to create new orders.
  get:
    is: [ assets.paging ]
    description: lists all orders of a specific user
    queryParameters:
      userId:
        type: string
        description: use to query all orders of a user
        required: true
        example: "1964401a-a8b3-40c1-b86e-d8b9f75b5842"
    responses:
      200:
        body:
          application/json:
            type: assets.Orders
            examples:
              single-order:
                orders:
                  -
                    order_id: "ORDER-437563756"
                    creation_date: "2016-03-30"
                    items:
                      -
                        product_id: "PRODUCT-1"
                        quantity: 5
                      -
                        product_id: "PRODUCT-2"
                        quantity: 2
              multiple-orders:
                  orders:
                    -
                      order_id: "ORDER-437563756"
                      creation_date: "2016-03-30"
                      items:
                        -
                          product_id: "PRODUCT-1"
                          quantity: 5
                        -
                          product_id: "PRODUCT-2"
                          quantity: 2
                    -
                      order_id: "ORDER-437542111"
                      creation_date: "2016-03-30"
                      items:
                        -
                          product_id: "PRODUCT-7"
                          quantity: 6

Library:

#%RAML 1.0 Library

types:
  ProductItem:
    type: object
    properties:
      product_id: string
      quantity: integer
  Order:
    type: object
    properties:
      order_id: string
      creation_date: string
      items: ProductItem[]
  Orders:
    type: object
    properties:
      orders: Order[]

traits:
  paging:
    queryParameters:
      size:
        description: the amount of elements of each result page
        type: integer
        required: false
        example: 10
      page:
        description: the page number
        type: integer
        required: false
        example: 0

  client-id-required:
    queryParameters:
      client_id:
        type: string
      client_secret:
        type: string

JSON:

{
  "uses": [
    {
      "key": "assets",
      "value": "assets-lib.raml"
    }
  ],
  "traits": [
    {
      "paging": {
        "queryParameters": {
          "size": {
            "name": "size",
            "type": [
              "integer"
            ],
            "example": 10,
            "required": false,
            "description": "the amount of elements of each result page",
            "structuredExample": {
              "value": 10,
              "strict": true,
              "name": null,
              "structuredValue": 10
            }
          },
          "page": {
            "name": "page",
            "type": [
              "integer"
            ],
            "example": null,
            "required": false,
            "description": "the page number"
          }
        },
        "name": "paging"
      }
    },
    {
      "client-id-required": {
        "queryParameters": {
          "client_id": {
            "name": "client_id",
            "type": [
              "string"
            ]
          },
          "client_secret": {
            "name": "client_secret",
            "type": [
              "string"
            ]
          }
        },
        "name": "client-id-required"
      }
    }
  ],
  "title": "Mobile Order API",
  "version": "1.0",
  "baseUri": "http://ec2-52-201-242-128.compute-1.amazonaws.com/mocks/97a819d0-751f-4b70-a5a0-99889ba06eea/api",
  "protocols": [
    "HTTP"
  ],
  "resources": [
    {
      "methods": [
        {
          "protocols": [
            "HTTP"
          ],
          "is": [
            "assets.client-id-required"
          ],
          "description": "Gets a list of all the inventory products.",
          "method": "get"
        }
      ],
      "relativeUri": "/products",
      "displayName": "/products",
      "relativeUriPathSegments": [
        "products"
      ]
    },
    {
      "methods": [
        {
          "queryParameters": {
            "userId": {
              "name": "userId",
              "displayName": "userId",
              "type": [
                "string"
              ],
              "example": "1964401a-a8b3-40c1-b86e-d8b9f75b5842",
              "repeat": false,
              "required": true,
              "description": "use to query all orders of a user",
              "structuredExample": {
                "value": "1964401a-a8b3-40c1-b86e-d8b9f75b5842",
                "strict": true,
                "name": null,
                "structuredValue": "1964401a-a8b3-40c1-b86e-d8b9f75b5842"
              }
            }
          },
          "responses": {
            "200": {
              "code": "200",
              "body": {
                "application/json": {
                  "name": "application/json",
                  "displayName": "application/json",
                  "type": [
                    "assets.Orders"
                  ],
                  "examples": [
                    {
                      "value": {
                        "orders": [
                          {
                            "order_id": "ORDER-437563756",
                            "creation_date": "2016-03-30",
                            "items": [
                              {
                                "product_id": "PRODUCT-1",
                                "quantity": 5
                              },
                              {
                                "product_id": "PRODUCT-2",
                                "quantity": 2
                              }
                            ]
                          }
                        ]
                      },
                      "strict": true,
                      "name": "single-order",
                      "structuredValue": {
                        "orders": [
                          {
                            "order_id": "ORDER-437563756",
                            "creation_date": "2016-03-30",
                            "items": [
                              {
                                "product_id": "PRODUCT-1",
                                "quantity": 5
                              },
                              {
                                "product_id": "PRODUCT-2",
                                "quantity": 2
                              }
                            ]
                          }
                        ]
                      }
                    },
                    {
                      "value": {
                        "orders": [
                          {
                            "order_id": "ORDER-437563756",
                            "creation_date": "2016-03-30",
                            "items": [
                              {
                                "product_id": "PRODUCT-1",
                                "quantity": 5
                              },
                              {
                                "product_id": "PRODUCT-2",
                                "quantity": 2
                              }
                            ]
                          },
                          {
                            "order_id": "ORDER-437542111",
                            "creation_date": "2016-03-30",
                            "items": [
                              {
                                "product_id": "PRODUCT-7",
                                "quantity": 6
                              }
                            ]
                          }
                        ]
                      },
                      "strict": true,
                      "name": "multiple-orders",
                      "structuredValue": {
                        "orders": [
                          {
                            "order_id": "ORDER-437563756",
                            "creation_date": "2016-03-30",
                            "items": [
                              {
                                "product_id": "PRODUCT-1",
                                "quantity": 5
                              },
                              {
                                "product_id": "PRODUCT-2",
                                "quantity": 2
                              }
                            ]
                          },
                          {
                            "order_id": "ORDER-437542111",
                            "creation_date": "2016-03-30",
                            "items": [
                              {
                                "product_id": "PRODUCT-7",
                                "quantity": 6
                              }
                            ]
                          }
                        ]
                      }
                    }
                  ],
                  "repeat": false,
                  "required": true
                }
              }
            }
          },
          "protocols": [
            "HTTP"
          ],
          "is": [
            "assets.paging"
          ],
          "description": "lists all orders of a specific user",
          "method": "get"
        }
      ],
      "description": "Orders collection resource used to create new orders.",
      "relativeUri": "/orders",
      "displayName": "Orders",
      "relativeUriPathSegments": [
        "orders"
      ]
    }
  ]
}

Note that traits is included in the RAML, but queryParameters are not expanded into the methods that use the trait.

juancoen commented 8 years ago

This is the code that uses the parser:

parser.loadApi(resolve(process.cwd(), filename), {attributeDefaults: true, rejectOnErrors: true})
  .then(function (raml) {
    var apiJSON;
    raml = raml.expand ? raml.expand() : raml;
    apiJSON = raml.toJSON({serializeMetadata: false});
    console.log(JSON.stringify(apiJSON, null, 2));
  })
  .catch(function (err) {
    console.log('Error found: ' + err);
    console.error(JSON.stringify(err, null, 2));

    process.exit(1);
  });