Skip to content

Commit 9d9f58a

Browse files
committed
Merge pull request #901 from swagger-api/issue-899
added operation type checking, write header to library, updated client library
2 parents f2f939e + 707a705 commit 9d9f58a

File tree

6 files changed

+386
-328
lines changed

6 files changed

+386
-328
lines changed

dist/lib/swagger-client.js

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* swagger-client - swagger.js is a javascript client for use with swaggering APIs.
3-
* @version v2.1.0-M1
3+
* @version v2.1.1-M1
44
* @link http://swagger.io
55
* @license apache 2.0
66
*/
@@ -31,7 +31,13 @@ ArrayModel.prototype.createJSONSample = function(modelsToIgnore) {
3131
}
3232
else if (this.ref) {
3333
var name = simpleRef(this.ref);
34-
result = models[name].createJSONSample();
34+
if(typeof modelsToIgnore[name] === 'undefined') {
35+
modelsToIgnore[name] = this;
36+
result = models[name].createJSONSample(modelsToIgnore);
37+
}
38+
else {
39+
return name;
40+
}
3541
}
3642
return [ result ];
3743
};
@@ -90,6 +96,7 @@ SwaggerAuthorizations.prototype.apply = function (obj, authorizations) {
9096
else {
9197
// 2.0 support
9298
if (Array.isArray(authorizations)) {
99+
93100
for (var i = 0; i < authorizations.length; i++) {
94101
var auth = authorizations[i];
95102
for (name in auth) {
@@ -607,12 +614,15 @@ var Operation = function(parent, scheme, operationId, httpMethod, path, args, de
607614
param.allowableValues.descriptiveValues.push({value : value, isDefault: isDefault});
608615
}
609616
}
610-
if(param.type === 'array' && typeof param.allowableValues === 'undefined') {
611-
// can't show as a list if no values to select from
612-
delete param.isList;
613-
delete param.allowMultiple;
617+
if(param.type === 'array') {
618+
innerType = [innerType];
619+
if(typeof param.allowableValues === 'undefined') {
620+
// can't show as a list if no values to select from
621+
delete param.isList;
622+
delete param.allowMultiple;
623+
}
614624
}
615-
param.signature = this.getModelSignature(innerType, models);
625+
param.signature = this.getModelSignature(innerType, models).toString();
616626
param.sampleJSON = this.getModelSampleJSON(innerType, models);
617627
param.responseClassSignature = param.signature;
618628
}
@@ -764,16 +774,21 @@ Operation.prototype.getModelSignature = function(type, definitions) {
764774
listType = true;
765775
type = type[0];
766776
}
777+
else if(typeof type === 'undefined')
778+
type = 'undefined';
767779

768780
if(type === 'string')
769781
isPrimitive = true;
770782
else
771783
isPrimitive = (listType && definitions[listType]) || (definitions[type]) ? false : true;
772784
if (isPrimitive) {
773-
return type;
785+
if(listType)
786+
return 'Array[' + type + ']';
787+
else
788+
return type.toString();
774789
} else {
775790
if (listType)
776-
return definitions[type].getMockSignature();
791+
return 'Array[' + definitions[type].getMockSignature() + ']';
777792
else
778793
return definitions[type].getMockSignature();
779794
}
@@ -965,8 +980,13 @@ Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
965980
fail(message);
966981
return;
967982
}
968-
var headers = this.getHeaderParams(args);
969-
headers = this.setContentTypes(args, opts);
983+
var allHeaders = this.getHeaderParams(args);
984+
var contentTypeHeaders = this.setContentTypes(args, opts);
985+
986+
var headers = {};
987+
for (var attrname in allHeaders) { headers[attrname] = allHeaders[attrname]; }
988+
for (var attrname in contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; }
989+
970990
var body = this.getBody(headers, args);
971991
var url = this.urlify(args);
972992

@@ -1013,10 +1033,10 @@ Operation.prototype.setContentTypes = function(args, opts) {
10131033
else
10141034
definedFormParams.push(param);
10151035
}
1016-
else if(param.in === 'header' && this.headers) {
1036+
else if(param.in === 'header' && opts) {
10171037
var key = param.name;
1018-
var headerValue = this.headers[param.name];
1019-
if(typeof this.headers[param.name] !== 'undefined')
1038+
var headerValue = opts[param.name];
1039+
if(typeof opts[param.name] !== 'undefined')
10201040
headers[key] = headerValue;
10211041
}
10221042
else if(param.in === 'body' && typeof args[param.name] !== 'undefined') {
@@ -1184,21 +1204,20 @@ var Model = function(name, definition) {
11841204
};
11851205

11861206
Model.prototype.createJSONSample = function(modelsToIgnore) {
1187-
var result = {};
1207+
var i, result = {};
11881208
modelsToIgnore = (modelsToIgnore||{});
11891209
modelsToIgnore[this.name] = this;
1190-
var i;
11911210
for (i = 0; i < this.properties.length; i++) {
11921211
prop = this.properties[i];
1193-
result[prop.name] = prop.getSampleValue(modelsToIgnore);
1212+
var sample = prop.getSampleValue(modelsToIgnore);
1213+
result[prop.name] = sample;
11941214
}
11951215
delete modelsToIgnore[this.name];
11961216
return result;
11971217
};
11981218

11991219
Model.prototype.getSampleValue = function(modelsToIgnore) {
1200-
var i;
1201-
var obj = {};
1220+
var i, obj = {};
12021221
for(i = 0; i < this.properties.length; i++ ) {
12031222
var property = this.properties[i];
12041223
obj[property.name] = property.sampleValue(false, modelsToIgnore);
@@ -1207,8 +1226,7 @@ Model.prototype.getSampleValue = function(modelsToIgnore) {
12071226
};
12081227

12091228
Model.prototype.getMockSignature = function(modelsToIgnore) {
1210-
var propertiesStr = [];
1211-
var i, prop;
1229+
var i, prop, propertiesStr = [];
12121230
for (i = 0; i < this.properties.length; i++) {
12131231
prop = this.properties[i];
12141232
propertiesStr.push(prop.toString());
@@ -1282,7 +1300,7 @@ Property.prototype.isArray = function () {
12821300
Property.prototype.sampleValue = function(isArray, ignoredModels) {
12831301
isArray = (isArray || this.isArray());
12841302
ignoredModels = (ignoredModels || {});
1285-
var type = getStringSignature(this.obj);
1303+
var type = getStringSignature(this.obj, true);
12861304
var output;
12871305

12881306
if(this.$ref) {
@@ -1292,8 +1310,9 @@ Property.prototype.sampleValue = function(isArray, ignoredModels) {
12921310
ignoredModels[type] = this;
12931311
output = refModel.getSampleValue(ignoredModels);
12941312
}
1295-
else
1296-
type = refModel;
1313+
else {
1314+
output = refModelName;
1315+
}
12971316
}
12981317
else if(this.example)
12991318
output = this.example;
@@ -1324,16 +1343,20 @@ Property.prototype.sampleValue = function(isArray, ignoredModels) {
13241343
return output;
13251344
};
13261345

1327-
getStringSignature = function(obj) {
1346+
getStringSignature = function(obj, baseComponent) {
13281347
var str = '';
13291348
if(typeof obj.$ref !== 'undefined')
13301349
str += simpleRef(obj.$ref);
13311350
else if(typeof obj.type === 'undefined')
13321351
str += 'object';
13331352
else if(obj.type === 'array') {
1334-
str += 'Array[';
1335-
str += getStringSignature((obj.items || obj.$ref || {}));
1336-
str += ']';
1353+
if(baseComponent)
1354+
str += getStringSignature((obj.items || obj.$ref || {}));
1355+
else {
1356+
str += 'Array[';
1357+
str += getStringSignature((obj.items || obj.$ref || {}));
1358+
str += ']';
1359+
}
13371360
}
13381361
else if(obj.type === 'integer' && obj.format === 'int32')
13391362
str += 'integer';

0 commit comments

Comments
 (0)