Skip to content

Commit 3ecc374

Browse files
isman-usohwing328
authored andcommitted
[Typescript-Fetch] Support additionalproperties, Enum, Auth and more. (#6130)
* Support additionalproperties and more. - Upgrade to TypeScript 2 - Use type definition from npm instead of typings, typings is deprecation - Use Enum instead of String Literal Types - Use typescript es6 lib for target es5 - Support additionalproperties - Support JSDoc - Add snapshot and npmRepository option - Update typescript-fetch run script for linux - Create typescript-fetch run script for windows * Update and fix - Fix circle run script - Fix duplicate query parameter * Rename typescript-fetch folder to lowercase * Fix for review and update new line end of file * Fix end of file * rename script to {lang}-petstore-all.sh and fix test * Fix override query string https://stackoverflow.com/a/7517673/1077943
1 parent d4b8658 commit 3ecc374

File tree

72 files changed

+10285
-4929
lines changed

Some content is hidden

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

72 files changed

+10285
-4929
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ fi
2626

2727
# if you've executed sbt assembly previously it will use that instead.
2828
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29-
ags="$@ generate -t modules/swagger-codegen/src/main/resources/TypeScript-Fetch -i modules/swagger-codegen/src/test/resources/2_0/petstore-security-test.yaml -l typescript-fetch -o samples/client/petstore-security-test/typescript-fetch"
29+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/typescript-fetch -i modules/swagger-codegen/src/test/resources/2_0/petstore-security-test.yaml -l typescript-fetch -o samples/client/petstore-security-test/typescript-fetch"
3030

3131
java $JAVA_OPTS -jar $executable $ags
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2+
"npmName": "@swagger/typescript-fetch-petstore",
3+
"npmVersion": "1.0.0",
4+
"npmRepository" : "https://skimdb.npmjs.com/registry",
5+
"snapshot" : false,
26
"supportsES6": true
37
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
22
"npmName": "@swagger/typescript-fetch-petstore",
3-
"npmVersion": "0.0.1"
3+
"npmVersion": "1.0.0",
4+
"npmRepository" : "https://skimdb.npmjs.com/registry",
5+
"snapshot" : false
46
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@ECHO OFF
2+
3+
call bin\windows\typescript-fetch-petstore.bat
4+
call bin\windows\typescript-fetch-petstore-target-es6.bat
5+
call bin\windows\typescript-fetch-petstore-with-npm-version.bat
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@ECHO OFF
2+
3+
set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar
4+
5+
If Not Exist %executable% (
6+
mvn clean package
7+
)
8+
9+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
10+
set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -l typescript-fetch -c bin\typescript-fetch-petstore-target-es6.json -o samples\client\petstore\typescript-fetch\builds\es6-target
11+
12+
java %JAVA_OPTS% -jar %executable% %ags%
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@ECHO OFF
2+
3+
set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar
4+
5+
If Not Exist %executable% (
6+
mvn clean package
7+
)
8+
9+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
10+
set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -l typescript-fetch -c bin\typescript-fetch-petstore-with-npm-version.json -o samples\client\petstore\typescript-fetch\builds\with-npm-version
11+
12+
java %JAVA_OPTS% -jar %executable% %ags%
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
@ECHO OFF
2+
13
set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar
24

35
If Not Exist %executable% (
46
mvn clean package
57
)
68

79
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
8-
set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -l typescript-fetch -o samples\client\petstore\typescript-fetch
10+
11+
echo
12+
set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -l typescript-fetch -o samples\client\petstore\typescript-fetch\builds\default
913

1014
java %JAVA_OPTS% -jar %executable% %ags%

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

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@
44
import io.swagger.codegen.CodegenModel;
55
import io.swagger.codegen.CodegenProperty;
66
import io.swagger.codegen.SupportingFile;
7+
import io.swagger.models.ModelImpl;
8+
import io.swagger.models.properties.*;
79

810
import java.io.File;
9-
import java.util.List;
10-
import java.util.Map;
11-
import java.util.TreeSet;
11+
import java.text.SimpleDateFormat;
12+
import java.util.*;
1213

1314
public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodegen {
15+
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
1416

1517
public static final String NPM_NAME = "npmName";
1618
public static final String NPM_VERSION = "npmVersion";
19+
public static final String NPM_REPOSITORY = "npmRepository";
20+
public static final String SNAPSHOT = "snapshot";
1721

1822
protected String npmName = null;
1923
protected String npmVersion = "1.0.0";
24+
protected String npmRepository = null;
2025

2126
public TypeScriptFetchClientCodegen() {
2227
super();
@@ -26,31 +31,74 @@ public TypeScriptFetchClientCodegen() {
2631
importMapping.clear();
2732

2833
outputFolder = "generated-code/typescript-fetch";
29-
embeddedTemplateDir = templateDir = "TypeScript-Fetch";
34+
embeddedTemplateDir = templateDir = "typescript-fetch";
35+
3036
this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package"));
3137
this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package"));
38+
this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json"));
39+
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()));
40+
}
41+
42+
@Override
43+
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, ModelImpl swaggerModel) {
44+
codegenModel.additionalPropertiesType = getTypeDeclaration(swaggerModel.getAdditionalProperties());
45+
addImport(codegenModel, codegenModel.additionalPropertiesType);
3246
}
3347

3448
@Override
3549
public void processOpts() {
3650
super.processOpts();
51+
supportingFiles.add(new SupportingFile("index.mustache", "", "index.ts"));
3752
supportingFiles.add(new SupportingFile("api.mustache", "", "api.ts"));
53+
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.ts"));
3854
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
39-
supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
40-
supportingFiles.add(new SupportingFile("package.json.mustache", "", "package.json"));
41-
supportingFiles.add(new SupportingFile("typings.json.mustache", "", "typings.json"));
42-
supportingFiles.add(new SupportingFile("tsconfig.json.mustache", "", "tsconfig.json"));
43-
supportingFiles.add(new SupportingFile("tslint.json.mustache", "", "tslint.json"));
4455
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));
45-
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.ts"));
4656

47-
if(additionalProperties.containsKey(NPM_NAME)) {
57+
if (additionalProperties.containsKey(NPM_NAME)) {
58+
addNpmPackageGeneration();
59+
}
60+
}
61+
62+
@Override
63+
public String getTypeDeclaration(Property p) {
64+
Property inner;
65+
if(p instanceof ArrayProperty) {
66+
ArrayProperty mp1 = (ArrayProperty)p;
67+
inner = mp1.getItems();
68+
return this.getSwaggerType(p) + "<" + this.getTypeDeclaration(inner) + ">";
69+
} else if(p instanceof MapProperty) {
70+
MapProperty mp = (MapProperty)p;
71+
inner = mp.getAdditionalProperties();
72+
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
73+
} else if(p instanceof FileProperty || p instanceof ObjectProperty) {
74+
return "any";
75+
} else {
76+
return super.getTypeDeclaration(p);
77+
}
78+
}
79+
80+
private void addNpmPackageGeneration() {
81+
if (additionalProperties.containsKey(NPM_NAME)) {
4882
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
4983
}
5084

5185
if (additionalProperties.containsKey(NPM_VERSION)) {
5286
this.setNpmVersion(additionalProperties.get(NPM_VERSION).toString());
5387
}
88+
89+
if (additionalProperties.containsKey(SNAPSHOT) && Boolean.valueOf(additionalProperties.get(SNAPSHOT).toString())) {
90+
this.setNpmVersion(npmVersion + "-SNAPSHOT." + SNAPSHOT_SUFFIX_FORMAT.format(new Date()));
91+
}
92+
additionalProperties.put(NPM_VERSION, npmVersion);
93+
94+
if (additionalProperties.containsKey(NPM_REPOSITORY)) {
95+
this.setNpmRepository(additionalProperties.get(NPM_REPOSITORY).toString());
96+
}
97+
98+
//Files for building our lib
99+
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
100+
supportingFiles.add(new SupportingFile("package.mustache", "", "package.json"));
101+
supportingFiles.add(new SupportingFile("tsconfig.mustache", "", "tsconfig.json"));
54102
}
55103

56104
@Override
@@ -79,24 +127,12 @@ public void setNpmVersion(String npmVersion) {
79127
this.npmVersion = npmVersion;
80128
}
81129

82-
@Override
83-
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
84-
// process enum in models
85-
List<Object> models = (List<Object>) postProcessModelsEnum(objs).get("models");
86-
for (Object _mo : models) {
87-
Map<String, Object> mo = (Map<String, Object>) _mo;
88-
CodegenModel cm = (CodegenModel) mo.get("model");
89-
cm.imports = new TreeSet(cm.imports);
90-
for (CodegenProperty var : cm.vars) {
91-
// name enum with model name, e.g. StatuEnum => PetStatusEnum
92-
if (Boolean.TRUE.equals(var.isEnum)) {
93-
var.datatypeWithEnum = var.datatypeWithEnum.replace(var.enumName, cm.classname + var.enumName);
94-
var.enumName = cm.classname + var.enumName;
95-
}
96-
}
97-
}
130+
public String getNpmRepository() {
131+
return npmRepository;
132+
}
98133

99-
return objs;
134+
public void setNpmRepository(String npmRepository) {
135+
this.npmRepository = npmRepository;
100136
}
101137

102138
}

modules/swagger-codegen/src/main/resources/TypeScript-Fetch/README.md

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)