Uri id and $ref, join description

This commit is contained in:
Ying Tang 2015-04-06 17:00:32 -07:00
parent 1dbc9cf113
commit b9001a3130
3 changed files with 31 additions and 12 deletions

View File

@ -5,7 +5,7 @@
*/
var _cloneDeep = require('lodash.clonedeep');
var translateDataTypeKeys = require('./translate-data-type-keys');
var TYPES_PRIMITIVE = ['array', 'boolean', 'integer', 'number', 'null', 'object', 'string'];
var TYPES_PRIMITIVE = ['array', 'boolean', 'integer', 'number', 'null', 'object', 'string', 'any'];
/**
* Export the modelHelper singleton.
@ -29,10 +29,18 @@ var modelHelper = module.exports = {
referencedModels.push(model);
}
}
}
};
var convertTypeTo$Ref = function convertTypeTo$Ref(prop, modelUri){
if (prop.type && TYPES_PRIMITIVE.indexOf(prop.type) === -1 ){
prop.$ref = modelUri + '/definitions/' + prop.type;
delete prop.type;
}
};
var def = modelClass.definition;
var name = def.name;
var modelUri = 'https://strongloop.com/loopback/' + name + '#';
var out = definitions || {};
if (out[name]) {
// The model is already included
@ -74,24 +82,26 @@ var modelHelper = module.exports = {
processType(modelClass.app, prop.type, referencedModels);
if (prop.items) {
processType(modelClass.app, prop.items.type, referencedModels);
convertTypeTo$Ref(prop.items, modelUri);
}
// Required props sit in a per-model array.
if (prop.required) {
if (prop.required || (prop.id && !prop.generated)) {
required.push(key);
}
// Change mismatched keys.
prop = translateDataTypeKeys(prop);
if (TYPES_PRIMITIVE.indexOf(prop.type) === -1 ){
prop.$ref = prop.type;
delete prop.type;
}
convertTypeTo$Ref(prop, modelUri);
delete prop.required;
delete prop.id;
if (prop.description) {
prop.description = Array.isArray(prop.description) ? prop.description.join('') : prop.description;
}
// Assign this back to the properties object.
properties[key] = prop;
@ -112,11 +122,19 @@ var modelHelper = module.exports = {
});
out[name] = {
id: name,
id: modelUri,
$schema: 'http://json-schema.org/draft-04/schema#',
properties: properties,
required: required
};
if (def.settings && typeof def.settings.additionalProperties != 'undefined') {
if (def.description){
out[name].description = Array.isArray(def.description) ? def.description.join('') : def.description;
}
if (def.settings && typeof def.settings.strict != 'undefined') {
out[name].additionalProperties = !def.settings.strict;
} else if (def.settings && typeof def.settings.additionalProperties != 'undefined') {
out[name].additionalProperties = def.settings.additionalProperties;
}

View File

@ -15,6 +15,7 @@ describe('model-helper', function() {
buf: Buffer // {type: 'string', format: 'byte'}
});
var props = def.properties;
console.log(props.str);
expect(props.str).to.eql({ type: 'string' });
expect(props.num).to.eql({ type: 'number', format: 'double' });
expect(props.date).eql({ type: 'string', format: 'date' });
@ -111,14 +112,14 @@ describe('model-helper', function() {
expect(prop).to.eql({ type: 'array', items: { type: 'any' } });
});
it('converts Model type', function() {
it('converts Model type to $ref', function() {
var Address = loopback.createModel('Address', {street: String});
var def = buildSwaggerModels({
str: String,
address: Address
});
var prop = def.properties.address;
expect(prop).to.eql({ $ref: 'Address' });
expect(prop).to.eql({ $ref: 'https://strongloop.com/loopback/testModel#/definitions/Address' });
});
});

View File

@ -91,7 +91,7 @@ describe('swagger definition', function() {
getReq.end(function(err, res) {
if (err) return done(err);
var data = res.body.models.product;
expect(data.id).to.equal('product');
expect(data.id).to.equal('https://strongloop.com/loopback/product#');
expect(data.required.sort()).to.eql(['aNum', 'foo'].sort());
expect(data.properties.foo.type).to.equal('string');
expect(data.properties.bar.type).to.equal('string');