Add optional support for HttpClient (#6295)

* fix compilation error in eclipse

by updating package declarations in moved files
(eclipse validates that package and folder names match)

* permit specifying the full angular version

simplifying the templates by moving trivial case splits to the model

* remove dead code

this method is never called ...

* support HttpClient in addition to Http, clean up generated code

Fixes #6080

* added new sample, and regenerated existing samples

* updated samples

this time with the freshly build generator ...

* improve formatting

* updated samples

* fix compilation error in generated code

the overload for blobs does not have a type parameter

* added the first test for the utils package

* fix extra trainling commas in function argument lists

* regenerated samples
This commit is contained in:
Adrian Moos
2017-09-30 11:19:39 +02:00
committed by wing328
parent e3c61cd1e4
commit da37f40202
49 changed files with 1916 additions and 2156 deletions

View File

@@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
@@ -14,6 +15,7 @@ import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenParameter;
import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.SupportingFile;
import io.swagger.codegen.utils.SemVer;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.BooleanProperty;
@@ -35,7 +37,6 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
protected String npmName = null;
protected String npmVersion = "1.0.0";
protected String npmRepository = null;
protected String ngVersion = "4";
public TypeScriptAngularClientCodegen() {
super();
@@ -55,7 +56,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
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(WITH_INTERFACES, "Setting this property to true will generate interfaces next to the default class implementations.", BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(NG_VERSION, "The version of Angular (2 or 4). Default is '4'"));
this.cliOptions.add(new CliOption(NG_VERSION, "The version of Angular. Default is '4.3'"));
}
@Override
@@ -100,21 +101,18 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
}
// determine NG version
SemVer ngVersion;
if (additionalProperties.containsKey(NG_VERSION)) {
if ("2".equals(additionalProperties.get(NG_VERSION).toString())) {
additionalProperties.put("isNg2x", true);
setNgVersion("2");
} else if ("4".equals(additionalProperties.get(NG_VERSION).toString())) {
additionalProperties.put("isNg4x", true);
setNgVersion("4");
} else {
throw new IllegalArgumentException("Invalid ngVersion, which must be either '2' or '4'");
}
ngVersion = new SemVer(additionalProperties.get(NG_VERSION).toString());
} else {
// default to 4
additionalProperties.put("isNg4x", true);
setNgVersion("4");
ngVersion = new SemVer("4.3.0");
LOGGER.info("generating code for Angular {} ...", ngVersion);
LOGGER.info(" (you can select the angular version by setting the additionalProperty ngVersion)");
}
additionalProperties.put(NG_VERSION, ngVersion);
additionalProperties.put("injectionToken", ngVersion.atLeast("4.0.0") ? "InjectionToken" : "OpaqueToken");
additionalProperties.put("injectionTokenTyped", ngVersion.atLeast("4.0.0"));
additionalProperties.put("useHttpClient", ngVersion.atLeast("4.3.0"));
}
private void addNpmPackageGeneration() {
@@ -217,36 +215,40 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
List<CodegenOperation> ops = (List<CodegenOperation>) objs.get("operation");
for (CodegenOperation op : ops) {
// Convert httpMethod to Angular's RequestMethod enum
// https://angular.io/docs/ts/latest/api/http/index/RequestMethod-enum.html
switch (op.httpMethod) {
case "GET":
op.httpMethod = "RequestMethod.Get";
break;
case "POST":
op.httpMethod = "RequestMethod.Post";
break;
case "PUT":
op.httpMethod = "RequestMethod.Put";
break;
case "DELETE":
op.httpMethod = "RequestMethod.Delete";
break;
case "OPTIONS":
op.httpMethod = "RequestMethod.Options";
break;
case "HEAD":
op.httpMethod = "RequestMethod.Head";
break;
case "PATCH":
op.httpMethod = "RequestMethod.Patch";
break;
default:
throw new RuntimeException("Unknown HTTP Method " + op.httpMethod + " not allowed");
if ((boolean) additionalProperties.get("useHttpClient")) {
op.httpMethod = op.httpMethod.toLowerCase(Locale.ENGLISH);
} else {
// Convert httpMethod to Angular's RequestMethod enum
// https://angular.io/docs/ts/latest/api/http/index/RequestMethod-enum.html
switch (op.httpMethod) {
case "GET":
op.httpMethod = "RequestMethod.Get";
break;
case "POST":
op.httpMethod = "RequestMethod.Post";
break;
case "PUT":
op.httpMethod = "RequestMethod.Put";
break;
case "DELETE":
op.httpMethod = "RequestMethod.Delete";
break;
case "OPTIONS":
op.httpMethod = "RequestMethod.Options";
break;
case "HEAD":
op.httpMethod = "RequestMethod.Head";
break;
case "PATCH":
op.httpMethod = "RequestMethod.Patch";
break;
default:
throw new RuntimeException("Unknown HTTP Method " + op.httpMethod + " not allowed");
}
}
// Convert path to TypeScript template string
op.path = op.path.replaceAll("\\{(.*?)\\}", "\\$\\{$1\\}");
// Convert path to TypeScript template string, applying URI encoding
op.path = op.path.replaceAll("\\{(.*?)\\}", "\\$\\{encodeURIComponent($1)\\}");
}
// Add additional filename information for model imports in the services
@@ -316,14 +318,6 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
return modelPackage() + "/" + toModelFilename(name);
}
public String getNgVersion() {
return ngVersion;
}
public void setNgVersion(String ngVersion) {
this.ngVersion = ngVersion;
}
public String getNpmName() {
return npmName;
}

View File

@@ -0,0 +1,33 @@
package io.swagger.codegen.utils;
public class SemVer implements Comparable<SemVer> {
public final int major;
public final int minor;
public final int revision;
public SemVer(String versionString) {
String[] tokens = versionString.split("\\.");
major = Integer.parseInt(tokens[0]);
minor = tokens.length < 2 ? 0 : Integer.parseInt(tokens[1]);
revision = tokens.length < 3 ? 0 : Integer.parseInt(tokens[2]);
}
@Override
public int compareTo(SemVer o) {
int cmp = major - o.major;
if (cmp != 0) return cmp;
cmp = minor - o.minor;
if (cmp != 0) return cmp;
return revision - o.revision;
}
public boolean atLeast(String other) {
return compareTo(new SemVer(other)) >= 0;
}
@Override
public String toString() {
return major + "." + minor + "." + revision;
}
}