Skip to content

Commit da37f40

Browse files
bedag-moowing328
authored andcommitted
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
1 parent e3c61cd commit da37f40

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1896
-2136
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l typescript-angular -c bin/typescript-petstore-npm.json -o samples/client/petstore/typescript-angular-v4.3/npm --additional-properties ngVersion=4.3"
30+
31+
java $JAVA_OPTS -jar $executable $ags

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

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Date;
77
import java.util.HashMap;
88
import java.util.List;
9+
import java.util.Locale;
910
import java.util.Map;
1011
import java.util.Set;
1112

@@ -14,6 +15,7 @@
1415
import io.swagger.codegen.CodegenParameter;
1516
import io.swagger.codegen.CodegenOperation;
1617
import io.swagger.codegen.SupportingFile;
18+
import io.swagger.codegen.utils.SemVer;
1719
import io.swagger.models.ModelImpl;
1820
import io.swagger.models.properties.ArrayProperty;
1921
import io.swagger.models.properties.BooleanProperty;
@@ -35,7 +37,6 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
3537
protected String npmName = null;
3638
protected String npmVersion = "1.0.0";
3739
protected String npmRepository = null;
38-
protected String ngVersion = "4";
3940

4041
public TypeScriptAngularClientCodegen() {
4142
super();
@@ -55,7 +56,7 @@ public TypeScriptAngularClientCodegen() {
5556
this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json"));
5657
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()));
5758
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()));
58-
this.cliOptions.add(new CliOption(NG_VERSION, "The version of Angular (2 or 4). Default is '4'"));
59+
this.cliOptions.add(new CliOption(NG_VERSION, "The version of Angular. Default is '4.3'"));
5960
}
6061

6162
@Override
@@ -100,21 +101,18 @@ public void processOpts() {
100101
}
101102

102103
// determine NG version
104+
SemVer ngVersion;
103105
if (additionalProperties.containsKey(NG_VERSION)) {
104-
if ("2".equals(additionalProperties.get(NG_VERSION).toString())) {
105-
additionalProperties.put("isNg2x", true);
106-
setNgVersion("2");
107-
} else if ("4".equals(additionalProperties.get(NG_VERSION).toString())) {
108-
additionalProperties.put("isNg4x", true);
109-
setNgVersion("4");
110-
} else {
111-
throw new IllegalArgumentException("Invalid ngVersion, which must be either '2' or '4'");
112-
}
106+
ngVersion = new SemVer(additionalProperties.get(NG_VERSION).toString());
113107
} else {
114-
// default to 4
115-
additionalProperties.put("isNg4x", true);
116-
setNgVersion("4");
108+
ngVersion = new SemVer("4.3.0");
109+
LOGGER.info("generating code for Angular {} ...", ngVersion);
110+
LOGGER.info(" (you can select the angular version by setting the additionalProperty ngVersion)");
117111
}
112+
additionalProperties.put(NG_VERSION, ngVersion);
113+
additionalProperties.put("injectionToken", ngVersion.atLeast("4.0.0") ? "InjectionToken" : "OpaqueToken");
114+
additionalProperties.put("injectionTokenTyped", ngVersion.atLeast("4.0.0"));
115+
additionalProperties.put("useHttpClient", ngVersion.atLeast("4.3.0"));
118116
}
119117

120118
private void addNpmPackageGeneration() {
@@ -217,36 +215,40 @@ public Map<String, Object> postProcessOperations(Map<String, Object> operations)
217215

218216
List<CodegenOperation> ops = (List<CodegenOperation>) objs.get("operation");
219217
for (CodegenOperation op : ops) {
220-
// Convert httpMethod to Angular's RequestMethod enum
221-
// https://angular.io/docs/ts/latest/api/http/index/RequestMethod-enum.html
222-
switch (op.httpMethod) {
223-
case "GET":
224-
op.httpMethod = "RequestMethod.Get";
225-
break;
226-
case "POST":
227-
op.httpMethod = "RequestMethod.Post";
228-
break;
229-
case "PUT":
230-
op.httpMethod = "RequestMethod.Put";
231-
break;
232-
case "DELETE":
233-
op.httpMethod = "RequestMethod.Delete";
234-
break;
235-
case "OPTIONS":
236-
op.httpMethod = "RequestMethod.Options";
237-
break;
238-
case "HEAD":
239-
op.httpMethod = "RequestMethod.Head";
240-
break;
241-
case "PATCH":
242-
op.httpMethod = "RequestMethod.Patch";
243-
break;
244-
default:
245-
throw new RuntimeException("Unknown HTTP Method " + op.httpMethod + " not allowed");
218+
if ((boolean) additionalProperties.get("useHttpClient")) {
219+
op.httpMethod = op.httpMethod.toLowerCase(Locale.ENGLISH);
220+
} else {
221+
// Convert httpMethod to Angular's RequestMethod enum
222+
// https://angular.io/docs/ts/latest/api/http/index/RequestMethod-enum.html
223+
switch (op.httpMethod) {
224+
case "GET":
225+
op.httpMethod = "RequestMethod.Get";
226+
break;
227+
case "POST":
228+
op.httpMethod = "RequestMethod.Post";
229+
break;
230+
case "PUT":
231+
op.httpMethod = "RequestMethod.Put";
232+
break;
233+
case "DELETE":
234+
op.httpMethod = "RequestMethod.Delete";
235+
break;
236+
case "OPTIONS":
237+
op.httpMethod = "RequestMethod.Options";
238+
break;
239+
case "HEAD":
240+
op.httpMethod = "RequestMethod.Head";
241+
break;
242+
case "PATCH":
243+
op.httpMethod = "RequestMethod.Patch";
244+
break;
245+
default:
246+
throw new RuntimeException("Unknown HTTP Method " + op.httpMethod + " not allowed");
247+
}
246248
}
247249

248-
// Convert path to TypeScript template string
249-
op.path = op.path.replaceAll("\\{(.*?)\\}", "\\$\\{$1\\}");
250+
// Convert path to TypeScript template string, applying URI encoding
251+
op.path = op.path.replaceAll("\\{(.*?)\\}", "\\$\\{encodeURIComponent($1)\\}");
250252
}
251253

252254
// Add additional filename information for model imports in the services
@@ -316,14 +318,6 @@ public String toModelImport(String name) {
316318
return modelPackage() + "/" + toModelFilename(name);
317319
}
318320

319-
public String getNgVersion() {
320-
return ngVersion;
321-
}
322-
323-
public void setNgVersion(String ngVersion) {
324-
this.ngVersion = ngVersion;
325-
}
326-
327321
public String getNpmName() {
328322
return npmName;
329323
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.swagger.codegen.utils;
2+
3+
public class SemVer implements Comparable<SemVer> {
4+
5+
public final int major;
6+
public final int minor;
7+
public final int revision;
8+
9+
public SemVer(String versionString) {
10+
String[] tokens = versionString.split("\\.");
11+
major = Integer.parseInt(tokens[0]);
12+
minor = tokens.length < 2 ? 0 : Integer.parseInt(tokens[1]);
13+
revision = tokens.length < 3 ? 0 : Integer.parseInt(tokens[2]);
14+
}
15+
16+
@Override
17+
public int compareTo(SemVer o) {
18+
int cmp = major - o.major;
19+
if (cmp != 0) return cmp;
20+
cmp = minor - o.minor;
21+
if (cmp != 0) return cmp;
22+
return revision - o.revision;
23+
}
24+
25+
public boolean atLeast(String other) {
26+
return compareTo(new SemVer(other)) >= 0;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return major + "." + minor + "." + revision;
32+
}
33+
}

0 commit comments

Comments
 (0)