Skip to content

Commit

Permalink
generate schema from example data
Browse files Browse the repository at this point in the history
  • Loading branch information
x-jason-hu committed Apr 12, 2017
1 parent 2166a9d commit b6c5eac
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ node_modules
# Swagger server
swagger-ui-master/*
swagger-ui-master.tar.gz

# IDE
.vscode
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var url = require('url'),
http = require('http'),
drafter = require('drafter.js'),
UriTemplate = require('uritemplate');
UriTemplate = require('uritemplate'),
GenerateSchema = require('generate-schema');

var apib2swagger = module.exports.convertParsed = function(apib) {
//console.log(JSON.stringify(apib, null, 4));
Expand Down Expand Up @@ -129,6 +130,15 @@ var swaggerOperation = function (pathParams, uriTemplate, action, tag) {
if (request.reference) {
schema.push({'$ref': '#/definitions/' + request.reference.id + 'Model'});
}
// fall back to body
if (request.body && (schema == null || schema.length == 0)) {
scheme = GenerateSchema.json("", JSON.parse(request.body));
if (scheme) {
delete scheme.title;
delete scheme.$schema;
schema.push(scheme);
}
}
}
}
}
Expand Down Expand Up @@ -319,6 +329,17 @@ function swaggerResponses(examples) {
var schema = searchDataStructure(response.content); // Attributes in response
if (schema) swaggerResponse.schema = schema;
}
if (!swaggerResponse.schema) {
// fall back to body
if (response.body) {
schema = GenerateSchema.json("", JSON.parse(response.body));
if (schema) {
delete schema.title;
delete schema.$schema;
swaggerResponse.schema = schema;
}
}
}
for (var n = 0; n < response.headers.length; n++) {
var header = response.headers[n];
//swaggerResponse.headers[header.name] = {'type':'string'}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"apib-include-directive": "^0.1.0",
"drafter.js": "^2.6.2",
"generate-schema": "^2.5.0",
"nopt": "^3.0.6",
"uritemplate": "^0.3.4"
},
Expand Down
16 changes: 16 additions & 0 deletions test/input/Schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,19 @@
+ Response 200

[Some resource 2][]

# Some resource 3 [/some/resource/3]

## POST

+ Request (application/json)

{"prop1": "a string", "prop2": 1, "prop3": {"prop3a": false, "prop3b": [{"prop3b1": 1}, {"prop3b1": 2}]}}

+ Response 200 (application/json)

+ Body
{
"prop1": "a string"
}

77 changes: 74 additions & 3 deletions test/output/Schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"type": "string"
}
},
"required": ["prop1"]
"required": [
"prop1"
]
}
}
]
Expand Down Expand Up @@ -78,11 +80,80 @@
}
]
}
},
"/some/resource/3": {
"post": {
"responses": {
"200": {
"description": "OK",
"headers": {},
"examples": {
"application/json": {
"prop1": "a string"
}
},
"schema": {
"type": "object",
"properties": {
"prop1": {
"type": "string"
}
}
}
}
},
"summary": "",
"description": "",
"tags": [],
"produces": [
"application/json"
],
"parameters": [
{
"name": "body",
"in": "body",
"schema": {
"type": "object",
"properties": {
"prop1": {
"type": "string"
},
"prop2": {
"type": "number"
},
"prop3": {
"type": "object",
"properties": {
"prop3a": {
"type": "boolean"
},
"prop3b": {
"type": "array",
"items": {
"type": "object",
"properties": {
"prop3b1": {
"type": "number"
}
},
"required": [
"prop3b1"
]
}
}
}
}
}
}
}
]
}
}
},
"definitions": {
"Some resource": {},
"Some resource 2": {},
"Some resource 2Model": {}
"Some resource 2Model": {},
"Some resource 3": {}
}
}
}
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("apib2swagger", function () {
files.concat(includedFiles).forEach(function (file) {
it(file, function (done) {
this.timeout(10000); // 10s
var apib = fs.readFileSync(localInput + file, "utf-8");
var apib = fs.readFileSync(localInput + file, "utf-8").replace(/\r/g, '');
apib2swagger.convert(apib, function (error, result) {
if (error) {
return done(error);
Expand Down

0 comments on commit b6c5eac

Please sign in to comment.