Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions bin/typescript-angular-v4.3-petstore-with-npm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh

SCRIPT="$0"

while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=`dirname "$SCRIPT"`/..
APP_DIR=`cd "${APP_DIR}"; pwd`
fi

executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"

if [ ! -f "$executable" ]
then
mvn clean package
fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
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"

java $JAVA_OPTS -jar $executable $ags
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

Expand All @@ -14,6 +15,7 @@
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;
Expand All @@ -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();
Expand All @@ -55,7 +56,7 @@ public TypeScriptAngularClientCodegen() {
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
Expand Down Expand Up @@ -100,21 +101,18 @@ public void processOpts() {
}

// 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() {
Expand Down Expand Up @@ -217,36 +215,40 @@ public Map<String, Object> postProcessOperations(Map<String, Object> operations)

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
Expand Down Expand Up @@ -316,14 +318,6 @@ public String toModelImport(String name) {
return modelPackage() + "/" + toModelFilename(name);
}

public String getNgVersion() {
return ngVersion;
}

public void setNgVersion(String ngVersion) {
this.ngVersion = ngVersion;
}

public String getNpmName() {
return npmName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.swagger.codegen.utils;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if you can write unit test for this file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also vote for that :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. It is the first unit test in this package.


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;
}
}
Loading