added yml2swagger

This commit is contained in:
Tony Tam 2013-12-20 16:42:59 -08:00
parent 773fb89acb
commit 569f77b512
6 changed files with 555 additions and 0 deletions

56
bin/yml2swagger.js Normal file
View File

@ -0,0 +1,56 @@
fs = require('fs')
yaml = require('js-yaml')
var args = process.argv.splice(2);
if(args.length == 0) {
process.exit(1);
}
var arg0 = args[0];
var outputdir = ".";
if(args.length > 1) {
outputdir = args[1];
}
var file = fs.lstatSync(arg0);
if(file.isFile()) {
convert(arg0, outputdir);
}
else if (file.isDirectory()) {
var files = fs.readdirSync(arg0);
files.map(function(item) {
var filename = arg0 + "/" + item;
var file = fs.lstatSync(filename);
if(file.isFile()) {
convert(filename, outputdir);
}
});
}
function convert(filename, outputdir) {
console.log("converting " + filename);
fs.readFile(filename, "utf8", function (err, data) {
if(err) {
console.log(err);
}
else {
try {
var js = yaml.load(data);
var prettyJs = JSON.stringify(js, undefined, 2);
var outputFilename = outputdir + "/" + filename.split("/").pop() + ".json";
console.log("writing to " + outputFilename);
fs.writeFile(outputFilename, prettyJs, function(err) {
if(err) {
console.log(err);
}
});
}
catch (err) {
console.log(err);
}
}
});
}

16
package.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "swagger-yaml",
"version": "2.0.1",
"description": "Converts yaml to swagger json",
"author": {
"name": "Tony Tam",
"email": "fehguy@gmail.com",
"url": "http://developer.wordnik.com"
},
"license": "Apache",
"readmeFilename": "README.md",
"dependencies": {
"json2yaml": "~1.0",
"js-yaml": "~3.0"
}
}

37
samples/yaml/api-docs.yml Normal file
View File

@ -0,0 +1,37 @@
apiVersion: 1.0.0
swaggerVersion: "1.2"
apis:
- path: /pet
description: Operations about pets
- path: /store
description: Operations about store
- path: /user
description: Operations about user
authorizations:
oauth2:
type: oauth2
scopes:
- PUBLIC
grantTypes:
implicit:
loginEndpoint:
url: "http://localhost:8002/oauth/dialog"
tokenName: access_code
authorization_code:
tokenRequestEndpoint:
url: "http://localhost:8002/oauth/requestToken"
clientIdName: client_id
clientSecretName: client_secret
tokenEndpoint:
url: "http://localhost:8002/oauth/token"
tokenName: access_code
apiKey:
type: apiKey
passAs: header
info:
title: Swagger Sample App
description: "This is a sample server Petstore server. You can find out more about Swagger \n at <a href=\"http://swagger.wordnik.com\">http://swagger.wordnik.com</a> or on irc.freenode.net, #swagger."
termsOfServiceUrl: "http://helloreverb.com/terms/"
contact: "apiteam@wordnik.com"
license: Apache 2.0
licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0.html"

181
samples/yaml/pet.yml Normal file
View File

@ -0,0 +1,181 @@
apiVersion: 1.0.0
swaggerVersion: "1.2"
basePath: "http://localhost:8002/api"
resourcePath: /pet
produces:
- application/json
- application/xml
- text/plain
- text/html
apis:
- path: "/pet/{petId}"
operations:
- method: GET
summary: Find pet by ID
notes: Returns a pet based on ID
type: Pet
nickname: getPetById
produces:
- application/json
- application/xml
authorizations:
- oauth2
parameters:
- name: petId
description: ID of pet that needs to be fetched
required: true
allowMultiple: false
type: string
paramType: path
responseMessages:
- code: 400
message: Invalid ID supplied
- code: 404
message: Pet not found
- method: DELETE
summary: Deletes a pet
notes: ""
type: void
nickname: deletePet
parameters:
- name: petId
description: Pet id to delete
required: true
allowMultiple: false
type: string
paramType: path
responseMessages:
- code: 400
message: Invalid pet value
- path: /pet
operations:
- method: POST
summary: Add a new pet to the store
notes: ""
type: void
nickname: addPet
parameters:
- name: body
description: Pet object that needs to be added to the store
required: true
allowMultiple: false
type: Pet
paramType: body
responseMessages:
- code: 405
message: Invalid input
- method: PUT
summary: Update an existing pet
notes: ""
type: void
nickname: updatePet
parameters:
- name: body
description: Pet object that needs to be updated in the store
required: true
allowMultiple: false
type: Pet
paramType: body
responseMessages:
- code: 400
message: Invalid ID supplied
- code: 404
message: Pet not found
- code: 405
message: Validation exception
- path: /pet/findByStatus
operations:
- method: GET
summary: Finds Pets by status
notes: Multiple status values can be provided with comma seperated strings
type: array
items:
$ref: Pet
nickname: findPetsByStatus
produces:
- application/json
- application/xml
parameters:
- name: status
description: Status values that need to be considered for filter
defaultValue: available
required: true
allowMultiple: true
type: string
paramType: query
enum:
- available
- pending
- sold
responseMessages:
- code: 400
message: Invalid status value
- path: /pet/findByTags
operations:
- method: GET
summary: Finds Pets by tags
notes: "Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing."
type: array
items:
$ref: Pet
nickname: findPetsByTags
produces:
- application/json
- application/xml
parameters:
- name: tags
description: Tags to filter by
required: true
allowMultiple: true
type: string
paramType: query
responseMessages:
- code: 400
message: Invalid tag value
deprecated: "true"
models:
Tag:
id: Tag
properties:
name:
type: string
id:
type: integer
format: int64
Pet:
id: Pet
description: "A pet is a person's best friend"
required:
- id
- name
properties:
id:
type: integer
format: int64
tags:
type: array
items:
$ref: Tag
name:
type: string
status:
type: string
description: pet status in the store
enum:
- available
- pending
- sold
category:
$ref: Category
photoUrls:
type: array
items:
type: string
Category:
id: Category
properties:
name:
type: string
id:
type: integer
format: int64

87
samples/yaml/store.yml Normal file
View File

@ -0,0 +1,87 @@
apiVersion: 1.0.0
swaggerVersion: "1.2"
basePath: "http://localhost:8002/api"
resourcePath: /store
produces:
- application/json
- application/xml
apis:
- path: "/store/order/{orderId}"
operations:
- method: GET
summary: Find purchase order by ID
notes: For valid response try integer IDs with value <= 5. Anything above 5 or nonintegers will generate API errors
type: Order
nickname: getOrderById
produces:
- application/json
- application/xml
parameters:
- name: orderId
description: ID of pet that needs to be fetched
required: true
allowMultiple: false
type: string
paramType: path
responseMessages:
- code: 400
message: Invalid ID supplied
- code: 404
message: Order not found
- method: DELETE
summary: Delete purchase order by ID
notes: For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
type: void
nickname: deleteOrder
parameters:
- name: orderId
description: ID of the order that needs to be deleted
required: true
allowMultiple: false
type: string
paramType: path
responseMessages:
- code: 400
message: Invalid ID supplied
- code: 404
message: Order not found
- path: /store/order
operations:
- method: POST
summary: Place an order for a pet
notes: ""
type: void
nickname: placeOrder
parameters:
- name: body
description: order placed for purchasing the pet
required: true
allowMultiple: false
type: Order
paramType: body
responseMessages:
- code: 400
message: Invalid order
models:
Order:
id: Order
properties:
id:
type: integer
format: int64
status:
type: string
description: Order Status
enum:
- placed
- approved
- delivered
petId:
type: integer
format: int64
quantity:
type: integer
format: int32
shipDate:
type: string
format: date-time

178
samples/yaml/user.yml Normal file
View File

@ -0,0 +1,178 @@
apiVersion: 1.0.0
swaggerVersion: "1.2"
basePath: "http://localhost:8002/api"
resourcePath: /user
produces:
- application/json
- application/xml
apis:
- path: /user
operations:
- method: POST
summary: Create user
notes: This can only be done by the logged in user.
type: void
nickname: createUser
parameters:
- name: body
description: Created user object
required: true
allowMultiple: false
type: User
paramType: body
- path: /user/createWithArray
operations:
- method: POST
summary: Creates list of users with given input array
notes: ""
type: void
nickname: createUsersWithArrayInput
parameters:
- name: body
description: List of user object
required: true
allowMultiple: false
type: array
items:
$ref: User
paramType: body
- path: /user/createWithList
operations:
- method: POST
summary: Creates list of users with given list input
notes: ""
type: void
nickname: createUsersWithListInput
parameters:
- name: body
description: List of user object
required: true
allowMultiple: false
type: array
items:
$ref: User
paramType: body
- path: "/user/{username}"
operations:
- method: PUT
summary: Updated user
notes: This can only be done by the logged in user.
type: void
nickname: updateUser
parameters:
- name: username
description: name that need to be deleted
required: true
allowMultiple: false
type: string
paramType: path
- name: body
description: Updated user object
required: true
allowMultiple: false
type: User
paramType: body
responseMessages:
- code: 400
message: Invalid username supplied
- code: 404
message: User not found
- method: DELETE
summary: Delete user
notes: This can only be done by the logged in user.
type: void
nickname: deleteUser
parameters:
- name: username
description: The name that needs to be deleted
required: true
allowMultiple: false
type: string
paramType: path
responseMessages:
- code: 400
message: Invalid username supplied
- code: 404
message: User not found
- method: GET
summary: Get user by user name
notes: ""
type: User
nickname: getUserByName
produces:
- application/json
- application/xml
parameters:
- name: username
description: The name that needs to be fetched. Use user1 for testing.
required: true
allowMultiple: false
type: string
paramType: path
responseMessages:
- code: 400
message: Invalid username supplied
- code: 404
message: User not found
- path: /user/login
operations:
- method: GET
summary: Logs user into the system
notes: ""
type: string
nickname: loginUser
produces:
- text/plain
parameters:
- name: username
description: The user name for login
required: true
allowMultiple: false
type: string
paramType: query
- name: password
description: The password for login in clear text
required: true
allowMultiple: false
type: string
paramType: query
responseMessages:
- code: 400
message: Invalid username and password combination
- path: /user/logout
operations:
- method: GET
summary: Logs out current logged in user session
notes: ""
type: void
nickname: logoutUser
produces:
- text/plain
parameters: []
models:
User:
id: User
properties:
id:
type: integer
format: int64
username:
type: string
password:
type: string
email:
type: string
firstName:
type: string
lastName:
type: string
phone:
type: string
userStatus:
type: integer
format: int32
description: User Status
enum:
- 1-registered
- 2-active
- 3-closed