Skip to content

[Typescript-Fetch] Support additionalproperties, Enum, Auth and more. #6130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 25, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ 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 -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"
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"

java $JAVA_OPTS -jar $executable $ags
4 changes: 4 additions & 0 deletions bin/typescript-fetch-petstore-target-es6.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"npmName": "@swagger/typescript-fetch-petstore",
"npmVersion": "1.0.0",
"npmRepository" : "https://skimdb.npmjs.com/registry",
"snapshot" : false,
"supportsES6": true
}
4 changes: 3 additions & 1 deletion bin/typescript-fetch-petstore-with-npm-version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"npmName": "@swagger/typescript-fetch-petstore",
"npmVersion": "0.0.1"
"npmVersion": "1.0.0",
"npmRepository" : "https://skimdb.npmjs.com/registry",
"snapshot" : false
}
5 changes: 5 additions & 0 deletions bin/windows/typescript-fetch-petstore-all.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@ECHO OFF

call bin\windows\typescript-fetch-petstore.bat
call bin\windows\typescript-fetch-petstore-target-es6.bat
call bin\windows\typescript-fetch-petstore-with-npm-version.bat
12 changes: 12 additions & 0 deletions bin/windows/typescript-fetch-petstore-target-es6.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@ECHO OFF

set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar

If Not Exist %executable% (
mvn clean package
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
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

java %JAVA_OPTS% -jar %executable% %ags%
12 changes: 12 additions & 0 deletions bin/windows/typescript-fetch-petstore-with-npm-version.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@ECHO OFF

set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar

If Not Exist %executable% (
mvn clean package
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
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

java %JAVA_OPTS% -jar %executable% %ags%
6 changes: 5 additions & 1 deletion bin/windows/typescript-fetch.bat → bin/windows/typescript-fetch-petstore.bat
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
@ECHO OFF

set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar

If Not Exist %executable% (
mvn clean package
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -l typescript-fetch -o samples\client\petstore\typescript-fetch

echo
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

java %JAVA_OPTS% -jar %executable% %ags%
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.SupportingFile;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.*;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.text.SimpleDateFormat;
import java.util.*;

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

public static final String NPM_NAME = "npmName";
public static final String NPM_VERSION = "npmVersion";
public static final String NPM_REPOSITORY = "npmRepository";
public static final String SNAPSHOT = "snapshot";

protected String npmName = null;
protected String npmVersion = "1.0.0";
protected String npmRepository = null;

public TypeScriptFetchClientCodegen() {
super();
Expand All @@ -26,31 +31,74 @@ public TypeScriptFetchClientCodegen() {
importMapping.clear();

outputFolder = "generated-code/typescript-fetch";
embeddedTemplateDir = templateDir = "TypeScript-Fetch";
embeddedTemplateDir = templateDir = "typescript-fetch";

this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package"));
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()));
}

@Override
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, ModelImpl swaggerModel) {
codegenModel.additionalPropertiesType = getTypeDeclaration(swaggerModel.getAdditionalProperties());
addImport(codegenModel, codegenModel.additionalPropertiesType);
}

@Override
public void processOpts() {
super.processOpts();
supportingFiles.add(new SupportingFile("index.mustache", "", "index.ts"));
supportingFiles.add(new SupportingFile("api.mustache", "", "api.ts"));
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.ts"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
supportingFiles.add(new SupportingFile("package.json.mustache", "", "package.json"));
supportingFiles.add(new SupportingFile("typings.json.mustache", "", "typings.json"));
supportingFiles.add(new SupportingFile("tsconfig.json.mustache", "", "tsconfig.json"));
supportingFiles.add(new SupportingFile("tslint.json.mustache", "", "tslint.json"));
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.ts"));

if(additionalProperties.containsKey(NPM_NAME)) {
if (additionalProperties.containsKey(NPM_NAME)) {
addNpmPackageGeneration();
}
}

@Override
public String getTypeDeclaration(Property p) {
Property inner;
if(p instanceof ArrayProperty) {
ArrayProperty mp1 = (ArrayProperty)p;
inner = mp1.getItems();
return this.getSwaggerType(p) + "<" + this.getTypeDeclaration(inner) + ">";
} else if(p instanceof MapProperty) {
MapProperty mp = (MapProperty)p;
inner = mp.getAdditionalProperties();
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
} else if(p instanceof FileProperty || p instanceof ObjectProperty) {
return "any";
} else {
return super.getTypeDeclaration(p);
}
}

private void addNpmPackageGeneration() {
if (additionalProperties.containsKey(NPM_NAME)) {
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
}

if (additionalProperties.containsKey(NPM_VERSION)) {
this.setNpmVersion(additionalProperties.get(NPM_VERSION).toString());
}

if (additionalProperties.containsKey(SNAPSHOT) && Boolean.valueOf(additionalProperties.get(SNAPSHOT).toString())) {
this.setNpmVersion(npmVersion + "-SNAPSHOT." + SNAPSHOT_SUFFIX_FORMAT.format(new Date()));
}
additionalProperties.put(NPM_VERSION, npmVersion);

if (additionalProperties.containsKey(NPM_REPOSITORY)) {
this.setNpmRepository(additionalProperties.get(NPM_REPOSITORY).toString());
}

//Files for building our lib
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("package.mustache", "", "package.json"));
supportingFiles.add(new SupportingFile("tsconfig.mustache", "", "tsconfig.json"));
}

@Override
Expand Down Expand Up @@ -79,24 +127,12 @@ public void setNpmVersion(String npmVersion) {
this.npmVersion = npmVersion;
}

@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
// process enum in models
List<Object> models = (List<Object>) postProcessModelsEnum(objs).get("models");
for (Object _mo : models) {
Map<String, Object> mo = (Map<String, Object>) _mo;
CodegenModel cm = (CodegenModel) mo.get("model");
cm.imports = new TreeSet(cm.imports);
for (CodegenProperty var : cm.vars) {
// name enum with model name, e.g. StatuEnum => PetStatusEnum
if (Boolean.TRUE.equals(var.isEnum)) {
var.datatypeWithEnum = var.datatypeWithEnum.replace(var.enumName, cm.classname + var.enumName);
var.enumName = cm.classname + var.enumName;
}
}
}
public String getNpmRepository() {
return npmRepository;
}

return objs;
public void setNpmRepository(String npmRepository) {
this.npmRepository = npmRepository;
}

}

This file was deleted.

Loading