raml-org/webapi-parser

What method do we use to extract example response payload/body?

Closed this issue · 7 comments

What method do we use to extract example response payload/body?

Code is in Java

Hi @hb308. Please provide an example of your RAML and let me know what piece of it you need to access. Thanks!

responses:
200:
body:
application/json:
type: campaignTypes.Campaign
required: false
example: !include examples/retrieveCampaign-Response.json

The response is defined in a json file but I'm guessing the parser extracts that anyway. What methods do I use to access that example?

Thank you!

Hi again @hb308.

Considering RAML with inclusion like so:

#%RAML 1.0
title: API with Types
types:
  User:
    type: object
    properties:
      firstName: string
      lastName:  string
      age:
        type: integer
        minimum: 0
        maximum: 99

/users/{id}:
  get:
    responses:
      200:
        body:
          application/json:
            type: User
            example: !include ../includes/cat-schema.json

Here's how you get a string value of the example:

package co.acme.parse;

import webapi.Raml10;
import webapi.WebApiDocument;
import amf.client.model.domain.WebApi;
import amf.client.model.domain.Example;
import amf.client.model.domain.NodeShape;

import java.util.concurrent.ExecutionException;

public class Raml10Parsing {

  // Example of parsing RAML 1.0 file
  public static void parseFile() throws InterruptedException, ExecutionException {
    // Parse the file
    WebApiDocument model = (WebApiDocument) Raml10.parse(
      "file://../api-specs/raml/api-with-types.raml").get();

    WebApi api = (WebApi) model.encodes();

    NodeShape schema = (NodeShape) api
      .endPoints().get(0)
      .operations().get(0)
      .responses().get(0)
      .payloads().get(0)
      .schema();
    Example example = (Example) schema.examples().get(0);

    System.out.println(example.value().value());
  }
}

You can also get other values from Example object using its interface: link.

Please close this gh issue if your issue has been resolved. Thanks!

Thank you!