-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJsonSchemaGenerator.js
More file actions
198 lines (157 loc) · 5.45 KB
/
JsonSchemaGenerator.js
File metadata and controls
198 lines (157 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// This is a Controller mixin to add methods for generating Swagger data.
// __Dependencies__
var mongoose = require('mongoose');
var AbstractSchemaGenerator = require('./AbstractSchemaGenerator');
// __Private Members__
// __Module Definition__
var JsonSchemaGenerator = module.exports = function () {
};
JsonSchemaGenerator.prototype = new AbstractSchemaGenerator();
// A method used to generate a Swagger model definition for a controller
JsonSchemaGenerator.prototype.generate = function (schema) {
var definition = {};
definition.properties = this.generateProperties(schema);
return definition;
}
// A method used to generate a Swagger model definition for a controller
JsonSchemaGenerator.prototype.generateSchema = function (schema) {
if (schema.paths) {
var definition = {};
definition.properties = this.generateProperties(schema);
return definition;
} else {
return this.generateProperty(schema);
}
}
JsonSchemaGenerator.prototype.generateProperty = function (path, schema) {
var property = {};
//var select = controller.get('select');
var method = "generate" + this.swaggerTypeFor(path.options.type).type;
if (typeof this[method] === "function") {
var property = this[method](path, schema);
} else {
// throw new Error("cannot handle type " + path.options.type);
return undefined;
}
return property;
};
JsonSchemaGenerator.prototype.generateProperties = function (schema) {
var properties = {};
Object.keys(schema.paths).forEach(function (name) {
if (!this.getEmbeddedName(name) && this.isIncluded(name, schema.paths[name].options)) {
var property = this.generateProperty(schema.paths[name], schema);
properties[name] = property;
}
}, this);
var embeddeds = this.findEmbeddeds(schema);
for (var key in embeddeds) {
var property = this.generateEmbedded(embeddeds[key]);
if (property) {
properties[key] = property;
}
}
// also called for mebedded
if (schema.options && schema.options.versionKey) {
properties[schema.options.versionKey] = {type: "number", required: false}
}
return properties;
};
JsonSchemaGenerator.prototype.generateString = function (path, schema) {
var property = {};
this.setGeneralProperties(property, path);
property.type = "string";
if (path.options.enum) {
property.enum = path.options.enum;
}
return property;
}
JsonSchemaGenerator.prototype.generateDate = function (path, schema) {
var property = {};
this.setGeneralProperties(property, path);
property.type = "date";
return property;
}
JsonSchemaGenerator.prototype.generateBoolean = function (path, schema) {
var property = {};
this.setGeneralProperties(property, path);
property.type = "boolean";
return property;
}
JsonSchemaGenerator.prototype.generateNumber = function (path, schema) {
var property = {};
this.setGeneralProperties(property, path);
property.type = "number";
if (path.options.min) {
property.min = path.options.min;
}
if (path.options.max) {
property.min = path.options.max;
}
return property;
}
JsonSchemaGenerator.prototype.generateDate = function (path, schema) {
var property = {};
this.setGeneralProperties(property, path);
property.type = "date";
return property;
}
JsonSchemaGenerator.prototype.generateArray = function (path, schema) {
var property = {};
this.setGeneralProperties(property, path);
property.type = "array";
var method = "generateType" + this.swaggerTypeFor(path.options.type[0]).type;
if (typeof this[method] === "function") {
property.items = this[method](path.options.type[0]);
}
return property;
}
JsonSchemaGenerator.prototype.generateObject = function (path, schema) {
var property = {type: "object"};
this.setGeneralProperties(property, path);
// an object of propertiey
property.type = this.generateSchema(path.options.type[0]);
return property;
}
JsonSchemaGenerator.prototype.generateEmbedded = function (embedded) {
var property = {type: "object"};
var required = false;
var schema = {paths: {}, options: {}};
embedded.forEach(function (path) {
if (path.options.required === true) {
required = true;
}
schema.paths[path.path] = path;
});
property.required = required;
property.type = "object";
// todo move this to another method
property.properties = this.generateProperties(schema);
return property;
}
JsonSchemaGenerator.prototype.generateObjectId = function (path, schema) {
var property = {};
if (path.options.ref) {
this.setGeneralProperties(property, path);
// an object of propertiey
property.type = "string";
} else {
this.setGeneralProperties(property, path);
// an object of propertiey
property.type = "string";
}
return property;
}
JsonSchemaGenerator.prototype.generateTypeObjectId = function (type) {
return {type: "string"};
}
JsonSchemaGenerator.prototype.generateTypeString = function (type) {
return {type: "string"};
}
JsonSchemaGenerator.prototype.generateTypeEmbedded = function (type) {
var schema = this.generate(type);
schema.type = "object";
return schema;
}
JsonSchemaGenerator.prototype.setGeneralProperties = function (property, path) {
property.required = path.options.required === true;
}