Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,482 changes: 414 additions & 1,068 deletions browser/swagger-client.js

Large diffs are not rendered by default.

352 changes: 169 additions & 183 deletions browser/swagger-client.min.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var SwaggerClient = module.exports = function (url, options) {
this.resourceCount = 0;
this.url = null;
this.useJQuery = false;
this.swaggerObject = {}
this.swaggerObject = {};

this.clientAuthorizations = new auth.SwaggerAuthorizations();

Expand Down Expand Up @@ -184,7 +184,7 @@ SwaggerClient.prototype.build = function (mock) {
}

self.swaggerVersion = responseObj.swaggerVersion;
self.swaggerObject = responseObj
self.swaggerObject = responseObj;

if (responseObj.swagger && parseInt(responseObj.swagger) === 2) {
self.swaggerVersion = responseObj.swagger;
Expand All @@ -194,11 +194,11 @@ SwaggerClient.prototype.build = function (mock) {
self.isValid = true;
} else {
var converter = new SwaggerSpecConverter();
self.oldSwaggerObject = self.swaggerObject
self.oldSwaggerObject = self.swaggerObject;

converter.setDocumentationLocation(self.url);
converter.convert(responseObj, self.clientAuthorizations, function(spec) {
self.swaggerObject = spec
self.swaggerObject = spec;
new Resolver().resolve(spec, self.url, self.buildFromSpec, self);
self.isValid = true;
});
Expand All @@ -208,7 +208,7 @@ SwaggerClient.prototype.build = function (mock) {
};

if (this.spec) {
self.swaggerObject = this.spec
self.swaggerObject = this.spec;
setTimeout(function () {
new Resolver().resolve(self.spec, self.buildFromSpec, self);
}, 10);
Expand Down
111 changes: 85 additions & 26 deletions lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Resolver.prototype.processAllOf = function(name, definition, resolutionTable, un
location = '/definitions/' + name + '/allOf';
this.resolveInline(null, spec, property, resolutionTable, unresolvedRefs, location);
}
}
};

Resolver.prototype.resolve = function (spec, arg1, arg2, arg3) {
var root = arg1, callback = arg2, scope = arg3, location, i;
Expand Down Expand Up @@ -115,6 +115,7 @@ Resolver.prototype.resolve = function (spec, arg1, arg2, arg3) {
// if the root is same as obj[i].root we can resolve locally
var all = resolutionTable;

var parts;
for(i = 0; i < all.length; i++) {
var a = all[i];
if(root === a.root) {
Expand Down Expand Up @@ -145,7 +146,7 @@ Resolver.prototype.resolve = function (spec, arg1, arg2, arg3) {
toResolve.push(a);
}
else {
var parts = a.key.split('#');
parts = a.key.split('#');
if(parts.length === 2) {
if(parts[0].indexOf('http://') === 0 || parts[0].indexOf('https://') === 0) {
a.root = parts[0];
Expand Down Expand Up @@ -175,6 +176,17 @@ Resolver.prototype.resolve = function (spec, arg1, arg2, arg3) {
}
else {
if (a.resolveAs === 'inline') {
if(a.key && a.key.indexOf('#') === -1 && a.key.charAt(0) !== '/') {
// handle relative schema
parts = a.root.split('/');
location = '';
for(i = 0; i < parts.length - 1; i++) {
location += parts[i] + '/';
}
location += a.key;
a.root = location;
a.location = '';
}
toResolve.push(a);
}
}
Expand Down Expand Up @@ -208,15 +220,18 @@ Resolver.prototype.resolve = function (spec, arg1, arg2, arg3) {
on: {
error: function () {
processedCalls += 1;
unresolvedRefs[item.key] = null;
unresolvedRefs[item.key] = {
root: item.root,
location: item.location
};

if (processedCalls === expectedCalls) {
self.finish(spec, _root, resolutionTable, resolvedRefs, unresolvedRefs, callback);
}
}, // jshint ignore:line
response: function (response) {
var swagger = response.obj;
self.resolveItem(swagger, _root, resolutionTable, resolvedRefs, unresolvedRefs, item);
self.resolveItem(swagger, item.root, resolutionTable, resolvedRefs, unresolvedRefs, item);
processedCalls += 1;

if (processedCalls === expectedCalls) {
Expand All @@ -242,23 +257,25 @@ Resolver.prototype.resolve = function (spec, arg1, arg2, arg3) {
Resolver.prototype.resolveItem = function(spec, root, resolutionTable, resolvedRefs, unresolvedRefs, item) {
var path = item.location;
var location = spec, parts = path.split('/');
for (var j = 0; j < parts.length; j++) {
var segment = parts[j];
if(segment.indexOf('~1') !== -1) {
segment = parts[j].replace(/~0/g, '~').replace(/~1/g, '/');
if(segment.charAt(0) !== '/') {
segment = '/' + segment;
if(path !== '') {
for (var j = 0; j < parts.length; j++) {
var segment = parts[j];
if (segment.indexOf('~1') !== -1) {
segment = parts[j].replace(/~0/g, '~').replace(/~1/g, '/');
if (segment.charAt(0) !== '/') {
segment = '/' + segment;
}
}
if (typeof location === 'undefined' || location === null) {
break;
}
if (segment === '' && j === (parts.length - 1) && parts.length > 1) {
location = null;
break;
}
if (segment.length > 0) {
location = location[segment];
}
}
if (typeof location === 'undefined' || location === null) {
break;
}
if(segment === '' && j === (parts.length - 1) && parts.length > 1) {
location = null;
break;
}
if (segment.length > 0) {
location = location[segment];
}
}
var resolved = item.key;
Expand Down Expand Up @@ -295,6 +312,9 @@ Resolver.prototype.finish = function (spec, root, resolutionTable, resolvedRefs,
if (resolvedTo) {
spec.definitions = spec.definitions || {};
if (item.resolveAs === 'ref') {
for (key in resolvedTo.obj) {
var abs = this.retainRoot(resolvedTo.obj[key], item.root);
}
spec.definitions[resolvedTo.name] = resolvedTo.obj;
item.obj.$ref = '#/definitions/' + resolvedTo.name;
} else if (item.resolveAs === 'inline') {
Expand Down Expand Up @@ -359,7 +379,9 @@ Resolver.prototype.getRefs = function(spec, obj) {
obj = obj || spec;
var output = {};
for(var key in obj) {
if (!obj.hasOwnProperty(key)) continue;
if (!obj.hasOwnProperty(key)) {
continue;
}
var item = obj[key];
if(key === '$ref' && typeof item === 'string') {
output[item] = null;
Expand Down Expand Up @@ -471,13 +493,48 @@ Resolver.prototype.resolveInline = function (root, spec, property, resolutionTab
};

Resolver.prototype.resolveTo = function (root, property, resolutionTable, location) {
var sp, i;
var ref = property.$ref;
if (ref) {
var lroot = root;
if (typeof ref !== 'undefined') {
if(ref.indexOf('#') >= 0) {
location = ref.split('#')[1];
var parts = ref.split('#');

// #/definitions/foo
// foo.json#/bar
if(parts[0] && ref.indexOf('/') === 0) {

}
else if(parts[0] && ref.indexOf('http') === 0) {

}
else if(parts[0] && parts[0].length > 0) {
// relative file
sp = root.split('/');
lroot = '';
for(i = 0; i < sp.length - 1; i++) {
lroot += sp[i] + '/';
}
lroot += parts[0];
}
else {

}

location = parts[1];
}
else {
// relative file
sp = root.split('/');
lroot = '';
for(i = 0; i < sp.length - 1; i++) {
lroot += sp[i] + '/';
}
lroot += ref;
location = '';
}
resolutionTable.push({
obj: property, resolveAs: 'ref', root: root, key: ref, location: location
obj: property, resolveAs: 'ref', root: lroot, key: ref, location: location
});
} else if (property.type === 'array') {
var items = property.items;
Expand All @@ -490,10 +547,12 @@ Resolver.prototype.resolveAllOf = function(spec, obj, depth) {
obj = obj || spec;
var name;
for(var key in obj) {
if (!obj.hasOwnProperty(key)) continue;
if (!obj.hasOwnProperty(key)) {
continue;
}
var item = obj[key];
if(item === null) {
throw new TypeError("Swagger 2.0 does not support null types (" + obj + "). See https://github.com/swagger-api/swagger-spec/issues/229.")
throw new TypeError('Swagger 2.0 does not support null types (' + obj + '). See https://github.com/swagger-api/swagger-spec/issues/229.');
}
if(typeof item === 'object') {
this.resolveAllOf(spec, item, depth + 1);
Expand Down
25 changes: 13 additions & 12 deletions lib/schema-markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports.schemaToJSON = schemaToJSON;

function optionHtml(label, value) {
return '<tr><td class="optionName">' + label + ':</td><td>' + value + '</td></tr>';
};
}

function typeFromJsonSchema(type, format) {
var str;
Expand Down Expand Up @@ -51,7 +51,7 @@ function typeFromJsonSchema(type, format) {
}

return str;
};
}

function getStringSignature(obj, baseComponent) {
var str = '';
Expand Down Expand Up @@ -95,7 +95,7 @@ function getStringSignature(obj, baseComponent) {
}

return str;
};
}

function schemaToJSON(schema, models, modelsToIgnore, modelPropertyMacro) {
// Resolve the schema (Handle nested schemas)
Expand All @@ -104,7 +104,7 @@ function schemaToJSON(schema, models, modelsToIgnore, modelPropertyMacro) {
if(typeof modelPropertyMacro !== 'function') {
modelPropertyMacro = function(prop){
return (prop || {}).default;
}
};
}

modelsToIgnore= modelsToIgnore || {};
Expand Down Expand Up @@ -182,7 +182,7 @@ function schemaToJSON(schema, models, modelsToIgnore, modelPropertyMacro) {
}

return output;
};
}

function schemaToHTML(name, schema, models, modelPropertyMacro) {

Expand Down Expand Up @@ -229,7 +229,7 @@ function schemaToHTML(name, schema, models, modelPropertyMacro) {
if(typeof modelPropertyMacro !== 'function') {
modelPropertyMacro = function(prop){
return (prop || {}).default;
}
};
}

var references = {};
Expand Down Expand Up @@ -279,7 +279,7 @@ function schemaToHTML(name, schema, models, modelPropertyMacro) {
}

return modelName;
};
}

function primitiveToHTML(schema) {
var html = '<span class="propType">';
Expand Down Expand Up @@ -321,7 +321,8 @@ function schemaToHTML(name, schema, models, modelPropertyMacro) {
html += '</span>';

return html;
};
}

function primitiveToOptionsHTML(schema, html) {
var options = '';
var type = schema.type || 'object';
Expand Down Expand Up @@ -415,7 +416,8 @@ function schemaToHTML(name, schema, models, modelPropertyMacro) {
}

return html;
};
}

function processModel(schema, name) {
var type = schema.type || 'object';
var isArray = schema.type === 'array';
Expand Down Expand Up @@ -520,6 +522,5 @@ function schemaToHTML(name, schema, models, modelPropertyMacro) {
}

return html + strongOpen + (isArray ? ']' : '}') + strongClose;
};

};
}
}
3 changes: 3 additions & 0 deletions lib/types/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ var Operation = module.exports = function (parent, scheme, operationId, httpMeth
}
}
}
else {
definitions = {};
}

for (i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
],
"description": "swagger-client is a javascript client for use with swaggering APIs.",
"version": "2.1.3",
"version": "2.1.4",
"homepage": "http://swagger.io",
"repository": {
"type": "git",
Expand Down
8 changes: 4 additions & 4 deletions test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('2.0 authorizations', function () {
});

it('does not apply api-key headers when the value has been manually added', function () {
var params = { api_key: 'foo' };
var params = { 'api_key': 'foo' };

var parameters = [
{
Expand All @@ -148,11 +148,11 @@ describe('2.0 authorizations', function () {
}
];
var authorizations = new auth.SwaggerAuthorizations();
authorizations.add("Basic", new auth.ApiKeyAuthorization('api_key', 'bar', 'header'));
authorizations.add('Basic', new auth.ApiKeyAuthorization('api_key', 'bar', 'header'));
var op = new Operation({}, 'http', 'test', 'get', '/path', { parameters: parameters },
{}, {}, authorizations);
var req = op.execute(params, {mock : true});
expect(req.headers.api_key).to.equal('foo');
expect(req.headers['api_key']).to.equal('foo');
});

it('does not apply password auth when the value has been manually added', function () {
Expand All @@ -166,7 +166,7 @@ describe('2.0 authorizations', function () {
}
];
var authorizations = new auth.SwaggerAuthorizations();
authorizations.add("Basic", new auth.PasswordAuthorization('bar', 'baz'));
authorizations.add('Basic', new auth.PasswordAuthorization('bar', 'baz'));
var op = new Operation({}, 'http', 'test', 'get', '/path', { parameters: parameters },
{}, {}, authorizations);
var req = op.execute(params, {mock : true});
Expand Down
Loading