Skip to content

Commit b5ba521

Browse files
committed
Merge pull request #2229 from xhh/js-optional-params
[JavaScript] Support optional parameters with object syntax
2 parents 6bbd691 + 72b9e2d commit b5ba521

File tree

14 files changed

+259
-266
lines changed

14 files changed

+259
-266
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavascriptClientCodegen.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.swagger.codegen.CodegenConstants;
88
import io.swagger.codegen.CodegenModel;
99
import io.swagger.codegen.CodegenOperation;
10+
import io.swagger.codegen.CodegenParameter;
1011
import io.swagger.codegen.CodegenProperty;
1112
import io.swagger.codegen.CodegenType;
1213
import io.swagger.codegen.SupportingFile;
@@ -448,6 +449,35 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
448449
return codegenModel;
449450
}
450451

452+
@Override
453+
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
454+
// Generate and store argument list string of each operation into
455+
// vendor-extension: x-codegen-argList.
456+
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
457+
if (operations != null) {
458+
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
459+
for (CodegenOperation operation : ops) {
460+
List<String> argList = new ArrayList();
461+
boolean hasOptionalParams = false;
462+
for (CodegenParameter p : operation.allParams) {
463+
if (p.required != null && p.required) {
464+
argList.add(p.paramName);
465+
} else {
466+
hasOptionalParams = true;
467+
}
468+
}
469+
if (hasOptionalParams) {
470+
argList.add("opts");
471+
}
472+
if (!usePromises) {
473+
argList.add("callback");
474+
}
475+
operation.vendorExtensions.put("x-codegen-argList", StringUtils.join(argList, ", "));
476+
}
477+
}
478+
return objs;
479+
}
480+
451481
@Override
452482
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
453483
List<Object> models = (List<Object>) objs.get("models");
@@ -491,7 +521,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
491521
}
492522
allowableValues.put("enumVars", enumVars);
493523
}
494-
// set vendor-extension: x-hasMoreRequired
524+
// set vendor-extension: x-codegen-hasMoreRequired
495525
CodegenProperty lastRequired = null;
496526
for (CodegenProperty var : cm.vars) {
497527
if (var.required != null && var.required) {
@@ -500,9 +530,9 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
500530
}
501531
for (CodegenProperty var : cm.vars) {
502532
if (var == lastRequired) {
503-
var.vendorExtensions.put("x-hasMoreRequired", false);
533+
var.vendorExtensions.put("x-codegen-hasMoreRequired", false);
504534
} else if (var.required != null && var.required) {
505-
var.vendorExtensions.put("x-hasMoreRequired", true);
535+
var.vendorExtensions.put("x-codegen-hasMoreRequired", true);
506536
}
507537
}
508538
}
Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
1-
(function(root, factory) {
1+
{{=< >=}}(function(root, factory) {
22
if (typeof define === 'function' && define.amd) {
33
// AMD. Register as an anonymous module.
4-
define(['../ApiClient'{{#imports}}, '../model/{{import}}'{{/imports}}], factory);
4+
define(['../ApiClient'<#imports>, '../model/<import>'</imports>], factory);
55
} else if (typeof module === 'object' && module.exports) {
66
// CommonJS-like environments that support module.exports, like Node.
7-
module.exports = factory(require('../ApiClient'){{#imports}}, require('../model/{{import}}'){{/imports}});
7+
module.exports = factory(require('../ApiClient')<#imports>, require('../model/<import>')</imports>);
88
} else {
99
// Browser globals (root is window)
10-
if (!root.{{moduleName}}) {
11-
root.{{moduleName}} = {};
10+
if (!root.<moduleName>) {
11+
root.<moduleName> = {};
1212
}
13-
root.{{moduleName}}.{{classname}} = factory(root.{{moduleName}}.ApiClient{{#imports}}, root.{{moduleName}}.{{import}}{{/imports}});
13+
root.<moduleName>.<classname> = factory(root.<moduleName>.ApiClient<#imports>, root.<moduleName>.<import></imports>);
1414
}
15-
}(this, function(ApiClient{{#imports}}, {{import}}{{/imports}}) {
15+
}(this, function(ApiClient<#imports>, <import></imports>) {
1616
'use strict';
1717
18-
var {{classname}} = function {{classname}}(apiClient) {
18+
var <classname> = function <classname>(apiClient) {
1919
this.apiClient = apiClient || ApiClient.default;
2020
2121
var self = this;
22-
{{#operations}}
23-
{{#operation}}
22+
<#operations>
23+
<#operation>
2424
/**
25-
* {{summary}}
26-
* {{notes}}
27-
{{#allParams}} * @param {{=<% %>=}}{<% dataType %>} <%={{ }}=%> {{paramName}} {{description}}
28-
{{/allParams}} {{^usePromises}}* @param {function} callback the callback function, accepting three arguments: error, data, response{{/usePromises}}{{#returnType}}
29-
* data is of type: {{{returnType}}}{{/returnType}}
25+
* <summary>
26+
* <notes><#allParams>
27+
* @param {<dataType>} <#required><paramName></required><^required>opts['<paramName>']</required> <description></allParams><^usePromises>
28+
* @param {function} callback the callback function, accepting three arguments: error, data, response</usePromises><#returnType>
29+
* data is of type: <&returnType></returnType>
3030
*/
31-
self.{{nickname}} = function({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{^usePromises}}{{#hasParams}}, {{/hasParams}}callback{{/usePromises}}) {
32-
var postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
33-
{{#allParams}}{{#required}}
34-
// verify the required parameter '{{paramName}}' is set
35-
if ({{paramName}} == null) {
36-
throw "Missing the required parameter '{{paramName}}' when calling {{nickname}}";
31+
self.<nickname> = function(<vendorExtensions.x-codegen-argList>) {<#hasOptionalParams>
32+
opts = opts || {};</hasOptionalParams>
33+
var postBody = <#bodyParam><#required><paramName></required><^required>opts['<paramName>']</required></bodyParam><^bodyParam>null</bodyParam>;
34+
<#allParams><#required>
35+
// verify the required parameter '<paramName>' is set
36+
if (<paramName> == null) {
37+
throw "Missing the required parameter '<paramName>' when calling <nickname>";
3738
}
38-
{{/required}}{{/allParams}}
39+
</required></allParams>
3940

40-
{{=< >=}}
4141
var pathParams = {<#pathParams>
42-
'<baseName>': <paramName><#hasMore>,</hasMore></pathParams>
42+
'<baseName>': <#required><paramName></required><^required>opts['<paramName>']</required><#hasMore>,</hasMore></pathParams>
4343
};
4444
var queryParams = {<#queryParams>
45-
'<baseName>': <#collectionFormat>this.apiClient.buildCollectionParam(<paramName>, '<collectionFormat>')</collectionFormat><^collectionFormat><paramName></collectionFormat><#hasMore>,</hasMore></queryParams>
45+
'<baseName>': <#collectionFormat>this.apiClient.buildCollectionParam(<#required><paramName></required><^required>opts['<paramName>']</required>, '<collectionFormat>')</collectionFormat><^collectionFormat><#required><paramName></required><^required>opts['<paramName>']</required></collectionFormat><#hasMore>,</hasMore></queryParams>
4646
};
4747
var headerParams = {<#headerParams>
48-
'<baseName>': <paramName><#hasMore>,</hasMore></headerParams>
48+
'<baseName>': <#required><paramName></required><^required>opts['<paramName>']</required><#hasMore>,</hasMore></headerParams>
4949
};
5050
var formParams = {<#formParams>
51-
'<baseName>': <#collectionFormat>this.apiClient.buildCollectionParam(<paramName>, '<collectionFormat>')</collectionFormat><^collectionFormat><paramName></collectionFormat><#hasMore>,</hasMore></formParams>
51+
'<baseName>': <#collectionFormat>this.apiClient.buildCollectionParam(<#required><paramName></required><^required>opts['<paramName>']</required>, '<collectionFormat>')</collectionFormat><^collectionFormat><#required><paramName></required><^required>opts['<paramName>']</required></collectionFormat><#hasMore>,</hasMore></formParams>
5252
};
5353

5454
var authNames = [<#authMethods>'<name>'<#hasMore>, </hasMore></authMethods>];
@@ -61,11 +61,11 @@
6161
pathParams, queryParams, headerParams, formParams, postBody,
6262
authNames, contentTypes, accepts, returnType<^usePromises>, callback</usePromises>
6363
);
64-
<={{ }}=>
64+
6565
}
66-
{{/operation}}
67-
{{/operations}}
66+
</operation>
67+
</operations>
6868
};
6969

70-
return {{classname}};
71-
}));
70+
return <classname>;
71+
}));<={{ }}=>

modules/swagger-codegen/src/main/resources/Javascript/model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{{#description}}/**
1919
* {{description}}
2020
**/{{/description}}
21-
var {{classname}} = function {{classname}}({{#vars}}{{#required}}{{name}}{{#vendorExtensions.x-hasMoreRequired}}, {{/vendorExtensions.x-hasMoreRequired}}{{/required}}{{/vars}}) { {{#parent}}/* extends {{{parent}}}*/{{/parent}}
21+
var {{classname}} = function {{classname}}({{#vars}}{{#required}}{{name}}{{#vendorExtensions.x-codegen-hasMoreRequired}}, {{/vendorExtensions.x-codegen-hasMoreRequired}}{{/required}}{{/vars}}) { {{#parent}}/* extends {{{parent}}}*/{{/parent}}
2222
{{#vars}}{{#required}}
2323
/**{{#description}}
2424
* {{{description}}}{{/description}}

0 commit comments

Comments
 (0)