Typescript-Jquery refactor/improvement proposition (#5751)

* First commit of the Java Play Framework server generator. It is highly based on Spring so there might me a couple of things that don't make sense (like options or parameters) for the Play Framework.

* Fix suggestions in the PR discussion + add .bat and .sh file as requested.

* Updated Readme.md file

* Remove unused mustache file + fix baseName vs paramName in all the mustache files.

* Fix the compilation error when we have a body which is a list or map. Doesn't fix the problem with the annotation itself.

* Fix the problem with the Http.MultipartFormData.FilePart

* First iteration of updating the typescript-jquery template/generator

* first RC of the new version of Typescript-JQuery using better models.

* Minor fix to the generation

* first RC of the new version of Typescript-JQuery using better models.

* Add an options for people using this client in a legacy app and that already have jquery loaded with a script tag somewhere

* Generation of the samples based on the latest changes from the typescript-jquery generator

* Fix to the check if the value is null and undefined

* Small fix when using collection
This commit is contained in:
Jean-François Côté
2017-07-03 11:08:48 -04:00
committed by wing328
parent 6b40bc6d30
commit 616f57a592
38 changed files with 3189 additions and 214 deletions

View File

@@ -1,5 +1,9 @@
package io.swagger.codegen.languages;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenParameter;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -9,7 +13,6 @@ import java.util.Date;
import io.swagger.codegen.CliOption;
import io.swagger.codegen.SupportingFile;
import io.swagger.models.properties.BooleanProperty;
public class TypeScriptJqueryClientCodegen extends AbstractTypeScriptClientCodegen {
private static final Logger LOGGER = LoggerFactory.getLogger(TypeScriptNodeClientCodegen.class);
@@ -19,6 +22,7 @@ public class TypeScriptJqueryClientCodegen extends AbstractTypeScriptClientCodeg
public static final String NPM_VERSION = "npmVersion";
public static final String NPM_REPOSITORY = "npmRepository";
public static final String SNAPSHOT = "snapshot";
public static final String JQUERY_ALREADY_IMPORTED = "jqueryAlreadyImported";
protected String npmName = null;
protected String npmVersion = "1.0.0";
@@ -26,6 +30,13 @@ public class TypeScriptJqueryClientCodegen extends AbstractTypeScriptClientCodeg
public TypeScriptJqueryClientCodegen() {
super();
modelTemplateFiles.put("model.mustache", ".ts");
apiTemplateFiles.put("api.mustache", ".ts");
typeMapping.put("Date", "Date");
apiPackage = "api";
modelPackage = "model";
outputFolder = "generated-code/typescript-jquery";
embeddedTemplateDir = templateDir = "typescript-jquery";
@@ -33,23 +44,75 @@ public class TypeScriptJqueryClientCodegen extends AbstractTypeScriptClientCodeg
this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package"));
this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json"));
this.cliOptions.add(new CliOption(SNAPSHOT, "When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm", BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(JQUERY_ALREADY_IMPORTED, "When using this in legacy app using mix of typescript and javascript, this will only declare jquery and not import it", BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString()));
}
@Override
public void processOpts() {
super.processOpts();
supportingFiles.add(new SupportingFile("api.mustache", null, "api.ts"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("models.mustache", modelPackage().replace('.', File.separatorChar), "models.ts"));
supportingFiles.add(new SupportingFile("apis.mustache", apiPackage().replace('.', File.separatorChar), "api.ts"));
supportingFiles.add(new SupportingFile("configuration.mustache", getIndexDirectory(), "configuration.ts"));
supportingFiles.add(new SupportingFile("index.mustache", getIndexDirectory(), "index.ts"));
supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts"));
LOGGER.warn("check additionals: " + additionalProperties.get(NPM_NAME));
if(additionalProperties.containsKey(NPM_NAME)) {
if (additionalProperties.containsKey(NPM_NAME)) {
addNpmPackageGeneration();
}
}
private String getIndexDirectory() {
String indexPackage = modelPackage.substring(0, Math.max(0, modelPackage.lastIndexOf('.')));
return indexPackage.replace('.', File.separatorChar);
}
@Override
public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p);
if (isLanguagePrimitive(swaggerType) || isLanguageGenericType(swaggerType)) {
return swaggerType;
}
return addModelPrefix(swaggerType);
}
private String addModelPrefix(String swaggerType) {
String type = null;
if (typeMapping.containsKey(swaggerType)) {
type = typeMapping.get(swaggerType);
} else {
type = swaggerType;
}
if (!isLanguagePrimitive(type) && !isLanguageGenericType(type)) {
type = "models." + swaggerType;
}
return type;
}
private boolean isLanguagePrimitive(String type) {
return languageSpecificPrimitives.contains(type);
}
private boolean isLanguageGenericType(String type) {
for (String genericType : languageGenericTypes) {
if (type.startsWith(genericType + "<")) {
return true;
}
}
return false;
}
@Override
public void postProcessParameter(CodegenParameter parameter) {
super.postProcessParameter(parameter);
parameter.dataType = addModelPrefix(parameter.dataType);
}
private void addNpmPackageGeneration() {
if(additionalProperties.containsKey(NPM_NAME)) {
if (additionalProperties.containsKey(NPM_NAME)) {
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
}
@@ -77,6 +140,12 @@ public class TypeScriptJqueryClientCodegen extends AbstractTypeScriptClientCodeg
return indexPackage.replace('.', File.separatorChar);
}
@Override
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, ModelImpl swaggerModel) {
codegenModel.additionalPropertiesType = getSwaggerType(swaggerModel.getAdditionalProperties());
addImport(codegenModel, codegenModel.additionalPropertiesType);
}
@Override
public String getName() {
return "typescript-jquery";