Skip to content

Commit

Permalink
Feature/es6 angular (swagger-api#5495)
Browse files Browse the repository at this point in the history
* module code added for es6-angular client code generation

* typescript wiped out the core gitignore file

* added USE_ES6 cli option to javascript-closure-angular, will use the javascript-es6-angular templates instead

* fixed issue with module file

* added annotations to the templates

* moved default output folder declaration

* moved es6 template folder under closure-angular
  • Loading branch information
miguelvaladez authored and wing328 committed Jun 15, 2017
1 parent ba4ecea commit 85850b2
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 10 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,3 @@ samples/client/petstore/typescript-node/npm/npm-debug.log

# aspnetcore
samples/server/petstore/aspnetcore/.vs/

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.*;
import io.swagger.models.properties.*;
import io.swagger.models.Swagger;

import java.util.TreeSet;
import java.util.*;
Expand All @@ -11,8 +12,14 @@
import org.apache.commons.lang3.StringUtils;

public class JavascriptClosureAngularClientCodegen extends DefaultCodegen implements CodegenConfig {

public static final String USE_ES6 = "useEs6";

protected boolean useEs6;

public JavascriptClosureAngularClientCodegen() {
super();
outputFolder = "generated-code/javascript-closure-angular";

supportsInheritance = false;
setReservedWordsLowerCase(Arrays.asList("abstract",
Expand Down Expand Up @@ -64,15 +71,11 @@ public JavascriptClosureAngularClientCodegen() {

typeMapping.put("binary", "string");

outputFolder = "generated-code/javascript-closure-angular";
modelTemplateFiles.put("model.mustache", ".js");
apiTemplateFiles.put("api.mustache", ".js");
embeddedTemplateDir = templateDir = "Javascript-Closure-Angular";
apiPackage = "API.Client";
modelPackage = "API.Client";

cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "hides the timestamp when files were generated")
.defaultValue(Boolean.TRUE.toString()));
cliOptions.add(new CliOption(USE_ES6,
"use ES6 templates")
.defaultValue(Boolean.FALSE.toString()));
}

@Override
Expand All @@ -83,6 +86,28 @@ public void processOpts() {
if (!additionalProperties.containsKey(CodegenConstants.HIDE_GENERATION_TIMESTAMP)) {
additionalProperties.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, Boolean.TRUE.toString());
}

if (additionalProperties.containsKey(USE_ES6)) {
setUseEs6(convertPropertyToBooleanAndWriteBack(USE_ES6));
}
}

@Override
public void preprocessSwagger(Swagger swagger) {
super.preprocessSwagger(swagger);

if (useEs6) {
embeddedTemplateDir = templateDir = "Javascript-Closure-Angular/es6";
apiPackage = "resources";
apiTemplateFiles.put("api.mustache", ".js");
supportingFiles.add(new SupportingFile("module.mustache", "", "module.js"));
} else {
modelTemplateFiles.put("model.mustache", ".js");
apiTemplateFiles.put("api.mustache", ".js");
embeddedTemplateDir = templateDir = "Javascript-Closure-Angular";
apiPackage = "API.Client";
modelPackage = "API.Client";
}
}

@Override
Expand All @@ -102,7 +127,7 @@ public CodegenType getTag() {
}

@Override
public String escapeReservedWord(String name) {
public String escapeReservedWord(String name) {
if(this.reservedWordsMappings().containsKey(name)) {
return this.reservedWordsMappings().get(name);
}
Expand All @@ -121,7 +146,7 @@ public String modelFileFolder() {
@Override
public String toVarName(String name) {
// sanitize name
name = sanitizeName(name);
name = sanitizeName(name);

// replace - with _ e.g. created-at => created_at
name = name.replaceAll("-", "_");
Expand Down Expand Up @@ -273,4 +298,7 @@ public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*");
}

public void setUseEs6(boolean useEs6) {
this.useEs6 = useEs6;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @fileoverview AUTOMATICALLY GENERATED service for {{package}}.{{classname}}.
* Do not edit this file by hand or your changes will be lost next time it is
* generated.{{#appDescription}}
*
* {{ appDescription }}{{/appDescription}}{{#version}}
* Version: {{version}}{{/version}}{{#appContact}}
* Contact: {{appContact}}{{/appContact}}
{{^hideGenerationTimestamp}}
* Generated at: {{generatedDate}}
{{/hideGenerationTimestamp}}
* Generated by: {{generatorClass}}
*/{{#licenseInfo}}
/**
* @license {{licenseInfo}}{{#licenseUrl}}
* {{licenseUrl}}{{/licenseUrl}}
*/
{{/licenseInfo}}

{{#operations}}

export default class {{classname}} {
/**
{{#description}}
* {{&description}}
{{/description}}
* @constructor
* @param {!angular.$resource} $resource
* @ngInject
*/
constructor($resource) {
this.basePath = '{{basePath}}';
return $resource(null, {}, {
{{#operation}}
'{{operationId}}': this.{{operationId}}(),
{{/operation}}
});
}

{{#operation}}

/**
* {{summary}}
* {{notes}} {{#pathParams}}
* @param {object} { {{paramName}} : !{{{dataType}}}{{^required}}={{/required}} } path parameters required by resource {{/pathParams}}{{#bodyParams}}
* @param {object} { {{paramName}} : !{{{dataType}}}{{^required}}={{/required}} } postData required by resource {{/bodyParams}}
* @param {function} Success Callback
* @param {function} Error Callback
* @return {object}
*/
{{operationId}}() {
return {
method: '{{httpMethod}}',
url: this.basePath + '{{path}}'
{{#pathParams}}
.replace('{' + '{{baseName}}' + '}', ':{{paramName}}')
{{/pathParams}}
}
}
{{/operation}}
}
{{/operations}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{#apiInfo}}
{{#apis}}
{{#operations}}
import {{classname}} from './resources/{{classname}}';
{{/operations}}
{{/apis}}

let moduleName = '{{appName}}'.toLowerCase().replace(/\s/g, '.');

export default angular
.module(moduleName, [])
{{#apis}}
{{#operations}}
.service('{{classname}}', {{classname}})
{{/operations}}
{{/apis}};

{{/apiInfo}}

0 comments on commit 85850b2

Please sign in to comment.