diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java index f4dcd074404..d9aea7da624 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java @@ -13,6 +13,13 @@ public class CodegenModel { public String unescapedDescription; public String defaultValue; public List vars = new ArrayList(); + + // list of all required parameters + public Set mandatory = new HashSet(); + + // TODO: temporary solution to get a delimited list of default constructor parameters, should be replaced if there is a better way of injecting the mandatory parameters into the mustache templates + public String mandatoryParams; + public Set imports = new HashSet(); public Boolean hasVars, emptyVars, hasMoreModels, hasEnums; public ExternalDocs externalDocs; diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 29ccd153d00..e1292cbac95 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -1881,6 +1881,10 @@ public class DefaultCodegen { m.vars.add(cp); } } + + m.mandatory = mandatory; + m.mandatoryParams = StringUtils.join(mandatory, ", "); + } else { m.emptyVars = true; m.hasVars = false; diff --git a/modules/swagger-codegen/src/main/resources/Javascript/api.mustache b/modules/swagger-codegen/src/main/resources/Javascript/api.mustache index b4ae1c4096e..6d4f4541649 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/api.mustache @@ -25,7 +25,13 @@ function {{classname}}() { } {{/required}}{{/allParams}} // create path and map variables - var {{localVariablePrefix}}path = '{{basePath}}' + replaceAll(replaceAll("{{{path}}}", "\\{format\\}","json"){{#pathParams}} + var basePath = '{{basePath}}'; + // if basePath ends with a /, remove it as path starts with a leading / + if (basePath.substring(basePath.length-1, basePath.length)=='/') { + basePath = basePath.substring(0, basePath.length-1); + } + + var {{localVariablePrefix}}path = basePath + replaceAll(replaceAll("{{{path}}}", "\\{format\\}","json"){{#pathParams}} , "\\{" + "{{baseName}}" + "\\}", {{localVariablePrefix}}apiClient.escapeString({{{paramName}}}.toString()){{/pathParams}}); var queryParams = {}; @@ -44,10 +50,10 @@ function {{classname}}() { path += createQueryString(queryParams); - if (console) { - console.log('path: ' + path); - console.log('queryParams: ' + queryParams); - } + //if (console) { + //console.log('path: ' + path); + //console.log('queryParams: ' + queryParams); + //} {{#isResponseBinary}} byte[] {{localVariablePrefix}}response = null; diff --git a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache index ca5e2c3230e..792fde00b12 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache @@ -4,16 +4,17 @@ {{#description}}/** * {{description}} **/{{/description}} -function {{classname}}() { {{#parent}}/* extends {{{parent}}}*/{{/parent}} +function {{classname}}({{mandatoryParams}}) { {{#parent}}/* extends {{{parent}}}*/{{/parent}} var self = this; {{#vars}} /**{{#description}} * {{{description}}}{{/description}} - * datatype: {{{datatypeWithEnum}}}{{#minimum}} + * datatype: {{{datatypeWithEnum}}}{{#required}} + * required{{/required}}{{#minimum}} * minimum: {{minimum}}{{/minimum}}{{#maximum}} * maximum: {{maximum}}{{/maximum}} **/ - self.{{name}} = {{{defaultValue}}}; + self.{{name}} = {{#required}}{{name}}{{/required}}{{^required}}{{{defaultValue}}}{{/required}}; {{/vars}} self.constructFromObject = function(data) { @@ -42,6 +43,9 @@ function {{classname}}() { {{#parent}}/* extends {{{parent}}}*/{{/parent}} } {{/vars}} + self.toJson = function () { + return JSON.stringify(self); + } } {{/model}} {{/models}}