Skip to content

Commit 83023ca

Browse files
committed
Merge pull request #385 from whitlockjc/issue-306
Fix issue with nested schemas
2 parents f3c22d8 + 1f3684f commit 83023ca

File tree

5 files changed

+121
-36
lines changed

5 files changed

+121
-36
lines changed

browser/swagger-client.js

+88-16
Large diffs are not rendered by default.

browser/swagger-client.min.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/helpers.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict';
22

3+
var _ = {
4+
isPlainObject: require('lodash-compat/lang/isPlainObject')
5+
};
6+
37
module.exports.__bind = function (fn, me) {
48
return function(){
59
return fn.apply(me, arguments);
@@ -24,6 +28,14 @@ module.exports.optionHtml = function (label, value) {
2428
return '<tr><td class="optionName">' + label + ':</td><td>' + value + '</td></tr>';
2529
};
2630

31+
var resolveSchema = module.exports.resolveSchema = function (schema) {
32+
if (_.isPlainObject(schema.schema)) {
33+
schema = resolveSchema(schema.schema);
34+
}
35+
36+
return schema;
37+
};
38+
2739
module.exports.typeFromJsonSchema = function (type, format) {
2840
var str;
2941

lib/types/model.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,16 @@ var schemaToHTML = function (name, schema, models) {
264264
var html = '<span class="propName ' + required + '">' + name + '</span> (';
265265
var model;
266266

267+
// Resolve the schema (Handle nested schemas)
268+
property = helpers.resolveSchema(property);
269+
267270
// We need to handle property references to primitives (Issue 339)
268271
if (!_.isUndefined(property.$ref)) {
269272
model = models[helpers.simpleRef(property.$ref)];
270273

271274
if (_.isUndefined(model) || _.indexOf([undefined, 'array', 'object'], model.definition.type) === -1) {
272275
// Use referenced schema
273-
property = model.definition;
276+
property = helpers.resolveSchema(model.definition);
274277
}
275278
}
276279

@@ -303,6 +306,8 @@ var schemaToHTML = function (name, schema, models) {
303306
return html + strongOpen + (isArray ? ']' : '}') + strongClose;
304307
};
305308

309+
// Resolve the schema (Handle nested schemas)
310+
schema = helpers.resolveSchema(schema);
306311

307312
// Generate current HTML
308313
var html = processModel(schema, name);
@@ -326,8 +331,11 @@ var schemaToHTML = function (name, schema, models) {
326331
};
327332

328333
var schemaToJSON = function (schema, models, modelsToIgnore) {
334+
// Resolve the schema (Handle nested schemas)
335+
schema = helpers.resolveSchema(schema);
336+
329337
var type = schema.type || 'object';
330-
var format = schema.format
338+
var format = schema.format;
331339
var model;
332340
var output;
333341

test/compat/modelSignatures.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
'use strict';
44

5-
var test = require('unit.js');
65
var expect = require('expect');
76
var mock = require('./mock');
87
var sample, instance;
@@ -21,19 +20,13 @@ describe('1.2 model signatures', function () {
2120
});
2221

2322
it('returns the json representation of a pet with repeating models', function () {
24-
var pet = sample.models.Animals;
23+
var animals = sample.models.Animals;
24+
var pet = sample.models.Pet;
2525

26-
test.object(pet);
27-
28-
// verify that each instance of `Pet` is represented
29-
var petModel = '{"id":0,"category":{"id":0,"name":""},"name":"","photoUrls":[""],"tags":[{"id":0,"name":""}],"status":"","phone":{"code":"","number":""},"owner":{"name":"","phone":{"code":"","number":""}}}';
30-
var model = '';
31-
32-
model += '{' + '"cat":' + petModel + ',';
33-
model += '"dog":' + petModel + ',';
34-
model += '"mouse":' + petModel + '}';
35-
36-
// TODO: enable with issue https://github.com/swagger-api/swagger-js/issues/306
37-
// expect(JSON.stringify(pet.createJSONSample())).toBe(model);
26+
expect(animals.createJSONSample()).toEqual({
27+
cat: pet.createJSONSample(),
28+
dog: pet.createJSONSample(),
29+
mouse: pet.createJSONSample()
30+
});
3831
});
3932
});

0 commit comments

Comments
 (0)