Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
Add examples of "location" param
Browse files Browse the repository at this point in the history
  • Loading branch information
postatum committed Aug 14, 2019
1 parent 04a6484 commit 744af9a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
17 changes: 17 additions & 0 deletions examples/api-specs/includes/cat-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$id": "https://example.com/cat.schema.json",
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Cat",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The cat's name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
}
}
}
12 changes: 12 additions & 0 deletions examples/api-specs/includes/cat-schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"$id": https://example.com/cat.schema.json
"$schema": http://json-schema.org/draft-04/schema#
title: Cat
type: object
properties:
name:
type: string
description: The cat's name.
age:
description: Age in years which must be equal to or greater than zero.
type: integer
minimum: 0
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// Runs all the example classes
public class WebApiParserDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException, UnsupportedEncodingException {
Raml10Parsing.parseStringWithLocation();
Raml10Parsing.parseString();
Raml10Parsing.parseFile();

Expand Down
26 changes: 25 additions & 1 deletion examples/java/src/main/java/co/acme/parse/Raml10Parsing.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void parseFile() throws InterruptedException, ExecutionException {
System.out.println("Parsed Raml10 file. Expected unit encoding webapi: " + ((WebApiDocument) result).encodes());
}

// Example of parsing RAML 1.0 file
// Example of parsing RAML 1.0 string
public static void parseString() throws InterruptedException, ExecutionException {
String inp ="#%RAML 1.0\n" +
"\n" +
Expand All @@ -42,4 +42,28 @@ public static void parseString() throws InterruptedException, ExecutionException
String output = Raml10.generateString(doc).get();
System.out.println("Generated Raml10 string:\n" + output);
}

// Example of parsing RAML 1.0 string with location param
public static void parseStringWithLocation() throws InterruptedException, ExecutionException {
String inp = "#%RAML 1.0\n" +
"title: API with Types\n" +
"/users/{id}:\n" +
" get:\n" +
" responses:\n" +
" 200:\n" +
" body:\n" +
" application/json:\n" +
" type: !include cat-schema.json";
String location = "file://../api-specs/includes/api.raml";
System.out.println("Input Raml10 string:\n" + inp);

// Parse the string
WebApiDocument doc = (WebApiDocument) Raml10.parse(inp, location).get();

System.out.println("Parsed content location:\n" + doc.location());

// Generate RAML 1.0 string from updated model and log it
String output = Raml10.generateString(doc).get();
System.out.println("Generated Raml10 string:\n" + output);
}
}
35 changes: 35 additions & 0 deletions examples/js/raml10-string-location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Example of parsing RAML 1.0 with basePath parameter.
*/
const wap = require('webapi-parser').WebApiParser
const path = require('path')

const ramlStr = `
#%RAML 1.0
title: API with Types
/users/{id}:
get:
responses:
200:
body:
application/json:
type: !include cat-schema.json
`

async function main () {
// Parse RAML 1.0 string with basePath parameter
console.log('Input:\n', ramlStr)
const fpath = path.resolve(
__dirname,
'../api-specs/includes/api.raml')
const location = `file://${fpath}`
const model = await wap.raml10.parse(ramlStr, location)
console.log('Model location:\n', model.location)
const resolved = await wap.raml10.resolve(model)

// Generate RAML 1.0 string
const generated = await wap.raml10.generateString(resolved)
console.log('Generated:\n', generated)
}

main()

0 comments on commit 744af9a

Please sign in to comment.