Skip to content

Commit 569f77b

Browse files
committed
added yml2swagger
1 parent 773fb89 commit 569f77b

File tree

6 files changed

+555
-0
lines changed

6 files changed

+555
-0
lines changed

bin/yml2swagger.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
fs = require('fs')
2+
yaml = require('js-yaml')
3+
4+
var args = process.argv.splice(2);
5+
6+
if(args.length == 0) {
7+
process.exit(1);
8+
}
9+
10+
var arg0 = args[0];
11+
var outputdir = ".";
12+
13+
if(args.length > 1) {
14+
outputdir = args[1];
15+
}
16+
17+
var file = fs.lstatSync(arg0);
18+
19+
if(file.isFile()) {
20+
convert(arg0, outputdir);
21+
}
22+
else if (file.isDirectory()) {
23+
var files = fs.readdirSync(arg0);
24+
files.map(function(item) {
25+
var filename = arg0 + "/" + item;
26+
var file = fs.lstatSync(filename);
27+
if(file.isFile()) {
28+
convert(filename, outputdir);
29+
}
30+
});
31+
}
32+
33+
function convert(filename, outputdir) {
34+
console.log("converting " + filename);
35+
fs.readFile(filename, "utf8", function (err, data) {
36+
if(err) {
37+
console.log(err);
38+
}
39+
else {
40+
try {
41+
var js = yaml.load(data);
42+
var prettyJs = JSON.stringify(js, undefined, 2);
43+
var outputFilename = outputdir + "/" + filename.split("/").pop() + ".json";
44+
console.log("writing to " + outputFilename);
45+
fs.writeFile(outputFilename, prettyJs, function(err) {
46+
if(err) {
47+
console.log(err);
48+
}
49+
});
50+
}
51+
catch (err) {
52+
console.log(err);
53+
}
54+
}
55+
});
56+
}

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "swagger-yaml",
3+
"version": "2.0.1",
4+
"description": "Converts yaml to swagger json",
5+
"author": {
6+
"name": "Tony Tam",
7+
"email": "[email protected]",
8+
"url": "http://developer.wordnik.com"
9+
},
10+
"license": "Apache",
11+
"readmeFilename": "README.md",
12+
"dependencies": {
13+
"json2yaml": "~1.0",
14+
"js-yaml": "~3.0"
15+
}
16+
}

samples/yaml/api-docs.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
apiVersion: 1.0.0
2+
swaggerVersion: "1.2"
3+
apis:
4+
- path: /pet
5+
description: Operations about pets
6+
- path: /store
7+
description: Operations about store
8+
- path: /user
9+
description: Operations about user
10+
authorizations:
11+
oauth2:
12+
type: oauth2
13+
scopes:
14+
- PUBLIC
15+
grantTypes:
16+
implicit:
17+
loginEndpoint:
18+
url: "http://localhost:8002/oauth/dialog"
19+
tokenName: access_code
20+
authorization_code:
21+
tokenRequestEndpoint:
22+
url: "http://localhost:8002/oauth/requestToken"
23+
clientIdName: client_id
24+
clientSecretName: client_secret
25+
tokenEndpoint:
26+
url: "http://localhost:8002/oauth/token"
27+
tokenName: access_code
28+
apiKey:
29+
type: apiKey
30+
passAs: header
31+
info:
32+
title: Swagger Sample App
33+
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."
34+
termsOfServiceUrl: "http://helloreverb.com/terms/"
35+
contact: "[email protected]"
36+
license: Apache 2.0
37+
licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0.html"

samples/yaml/pet.yml

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
apiVersion: 1.0.0
2+
swaggerVersion: "1.2"
3+
basePath: "http://localhost:8002/api"
4+
resourcePath: /pet
5+
produces:
6+
- application/json
7+
- application/xml
8+
- text/plain
9+
- text/html
10+
apis:
11+
- path: "/pet/{petId}"
12+
operations:
13+
- method: GET
14+
summary: Find pet by ID
15+
notes: Returns a pet based on ID
16+
type: Pet
17+
nickname: getPetById
18+
produces:
19+
- application/json
20+
- application/xml
21+
authorizations:
22+
- oauth2
23+
parameters:
24+
- name: petId
25+
description: ID of pet that needs to be fetched
26+
required: true
27+
allowMultiple: false
28+
type: string
29+
paramType: path
30+
responseMessages:
31+
- code: 400
32+
message: Invalid ID supplied
33+
- code: 404
34+
message: Pet not found
35+
- method: DELETE
36+
summary: Deletes a pet
37+
notes: ""
38+
type: void
39+
nickname: deletePet
40+
parameters:
41+
- name: petId
42+
description: Pet id to delete
43+
required: true
44+
allowMultiple: false
45+
type: string
46+
paramType: path
47+
responseMessages:
48+
- code: 400
49+
message: Invalid pet value
50+
- path: /pet
51+
operations:
52+
- method: POST
53+
summary: Add a new pet to the store
54+
notes: ""
55+
type: void
56+
nickname: addPet
57+
parameters:
58+
- name: body
59+
description: Pet object that needs to be added to the store
60+
required: true
61+
allowMultiple: false
62+
type: Pet
63+
paramType: body
64+
responseMessages:
65+
- code: 405
66+
message: Invalid input
67+
- method: PUT
68+
summary: Update an existing pet
69+
notes: ""
70+
type: void
71+
nickname: updatePet
72+
parameters:
73+
- name: body
74+
description: Pet object that needs to be updated in the store
75+
required: true
76+
allowMultiple: false
77+
type: Pet
78+
paramType: body
79+
responseMessages:
80+
- code: 400
81+
message: Invalid ID supplied
82+
- code: 404
83+
message: Pet not found
84+
- code: 405
85+
message: Validation exception
86+
- path: /pet/findByStatus
87+
operations:
88+
- method: GET
89+
summary: Finds Pets by status
90+
notes: Multiple status values can be provided with comma seperated strings
91+
type: array
92+
items:
93+
$ref: Pet
94+
nickname: findPetsByStatus
95+
produces:
96+
- application/json
97+
- application/xml
98+
parameters:
99+
- name: status
100+
description: Status values that need to be considered for filter
101+
defaultValue: available
102+
required: true
103+
allowMultiple: true
104+
type: string
105+
paramType: query
106+
enum:
107+
- available
108+
- pending
109+
- sold
110+
responseMessages:
111+
- code: 400
112+
message: Invalid status value
113+
- path: /pet/findByTags
114+
operations:
115+
- method: GET
116+
summary: Finds Pets by tags
117+
notes: "Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing."
118+
type: array
119+
items:
120+
$ref: Pet
121+
nickname: findPetsByTags
122+
produces:
123+
- application/json
124+
- application/xml
125+
parameters:
126+
- name: tags
127+
description: Tags to filter by
128+
required: true
129+
allowMultiple: true
130+
type: string
131+
paramType: query
132+
responseMessages:
133+
- code: 400
134+
message: Invalid tag value
135+
deprecated: "true"
136+
models:
137+
Tag:
138+
id: Tag
139+
properties:
140+
name:
141+
type: string
142+
id:
143+
type: integer
144+
format: int64
145+
Pet:
146+
id: Pet
147+
description: "A pet is a person's best friend"
148+
required:
149+
- id
150+
- name
151+
properties:
152+
id:
153+
type: integer
154+
format: int64
155+
tags:
156+
type: array
157+
items:
158+
$ref: Tag
159+
name:
160+
type: string
161+
status:
162+
type: string
163+
description: pet status in the store
164+
enum:
165+
- available
166+
- pending
167+
- sold
168+
category:
169+
$ref: Category
170+
photoUrls:
171+
type: array
172+
items:
173+
type: string
174+
Category:
175+
id: Category
176+
properties:
177+
name:
178+
type: string
179+
id:
180+
type: integer
181+
format: int64

samples/yaml/store.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
apiVersion: 1.0.0
2+
swaggerVersion: "1.2"
3+
basePath: "http://localhost:8002/api"
4+
resourcePath: /store
5+
produces:
6+
- application/json
7+
- application/xml
8+
apis:
9+
- path: "/store/order/{orderId}"
10+
operations:
11+
- method: GET
12+
summary: Find purchase order by ID
13+
notes: For valid response try integer IDs with value <= 5. Anything above 5 or nonintegers will generate API errors
14+
type: Order
15+
nickname: getOrderById
16+
produces:
17+
- application/json
18+
- application/xml
19+
parameters:
20+
- name: orderId
21+
description: ID of pet that needs to be fetched
22+
required: true
23+
allowMultiple: false
24+
type: string
25+
paramType: path
26+
responseMessages:
27+
- code: 400
28+
message: Invalid ID supplied
29+
- code: 404
30+
message: Order not found
31+
- method: DELETE
32+
summary: Delete purchase order by ID
33+
notes: For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
34+
type: void
35+
nickname: deleteOrder
36+
parameters:
37+
- name: orderId
38+
description: ID of the order that needs to be deleted
39+
required: true
40+
allowMultiple: false
41+
type: string
42+
paramType: path
43+
responseMessages:
44+
- code: 400
45+
message: Invalid ID supplied
46+
- code: 404
47+
message: Order not found
48+
- path: /store/order
49+
operations:
50+
- method: POST
51+
summary: Place an order for a pet
52+
notes: ""
53+
type: void
54+
nickname: placeOrder
55+
parameters:
56+
- name: body
57+
description: order placed for purchasing the pet
58+
required: true
59+
allowMultiple: false
60+
type: Order
61+
paramType: body
62+
responseMessages:
63+
- code: 400
64+
message: Invalid order
65+
models:
66+
Order:
67+
id: Order
68+
properties:
69+
id:
70+
type: integer
71+
format: int64
72+
status:
73+
type: string
74+
description: Order Status
75+
enum:
76+
- placed
77+
- approved
78+
- delivered
79+
petId:
80+
type: integer
81+
format: int64
82+
quantity:
83+
type: integer
84+
format: int32
85+
shipDate:
86+
type: string
87+
format: date-time

0 commit comments

Comments
 (0)