forked from stardogventures/swagger-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from stardogventures/typescript-axios-new-version
Merging updates to typescript-axios codegen and Kotlin codegen
- Loading branch information
Showing
20 changed files
with
351 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
...gger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAxiosClientCodegen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package io.swagger.codegen.languages; | ||
|
||
import io.swagger.codegen.CodegenModel; | ||
import io.swagger.codegen.CodegenOperation; | ||
import io.swagger.codegen.CodegenProperty; | ||
import io.swagger.codegen.SupportingFile; | ||
import io.swagger.models.ModelImpl; | ||
|
||
import java.io.File; | ||
import java.util.*; | ||
|
||
public class TypeScriptAxiosClientCodegen extends AbstractTypeScriptClientCodegen { | ||
public TypeScriptAxiosClientCodegen() { | ||
super(); | ||
|
||
this.outputFolder = "generated-code/typescript-axios"; | ||
embeddedTemplateDir = templateDir = "typescript-axios"; | ||
|
||
modelTemplateFiles.put("model.mustache", ".ts"); | ||
apiTemplateFiles.put("api.mustache", ".ts"); | ||
|
||
modelPackage = "models"; | ||
apiPackage = "resources"; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "typescript-axios"; | ||
} | ||
|
||
@Override | ||
public String getHelp() { | ||
return "Generates a TypeScript client library using Axios for HTTP requests."; | ||
} | ||
|
||
@Override | ||
public void processOpts() { | ||
super.processOpts(); | ||
supportingFiles.add(new SupportingFile("axios.config.mustache", "axios.config.ts")); | ||
} | ||
|
||
@Override | ||
public String toModelFilename(String name) { | ||
return toModelName(name); | ||
} | ||
|
||
@Override | ||
public String toModelImport(String name) { | ||
return modelPackage() + "/" + toModelFilename(name); | ||
} | ||
|
||
@Override | ||
public String toEnumVarName(String name, String datatype) { | ||
if (name.length() == 0) { | ||
return "Empty"; | ||
} | ||
|
||
// for symbol, e.g. $, # | ||
if (getSymbolName(name) != null) { | ||
return camelize(getSymbolName(name)); | ||
} | ||
|
||
// number | ||
if ("number".equals(datatype)) { | ||
String varName = "NUMBER_" + name; | ||
|
||
varName = varName.replaceAll("-", "MINUS_"); | ||
varName = varName.replaceAll("\\+", "PLUS_"); | ||
varName = varName.replaceAll("\\.", "_DOT_"); | ||
return varName; | ||
} | ||
|
||
String enumName = name; | ||
if (enumName.matches("\\d.*")) { // starts with number | ||
return "_" + enumName; | ||
} else { | ||
return enumName; | ||
} | ||
} | ||
|
||
@Override | ||
public String toEnumName(CodegenProperty property) { | ||
String enumName = toModelName(property.name); | ||
|
||
if (enumName.matches("\\d.*")) { // starts with number | ||
return "_" + enumName; | ||
} else { | ||
return enumName; | ||
} | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public Map<String, Object> postProcessModels(Map<String, Object> objs) { | ||
Map<String, Object> result = super.postProcessModels(objs); | ||
|
||
// Add additional filename information for imports | ||
List<Map<String, Object>> models = (List<Map<String, Object>>)postProcessModelsEnum(result).get("models"); | ||
for (Map<String,Object> mo : models) { | ||
CodegenModel cm = (CodegenModel) mo.get("model"); | ||
mo.put("tsImports", toTsImports(cm.imports)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private List<Map<String, String>> toTsImports(Set<String> imports) { | ||
List<Map<String, String>> tsImports = new ArrayList<>(); | ||
for(String im : imports) { | ||
HashMap<String, String> tsImport = new HashMap<>(); | ||
tsImport.put("classname", im); | ||
tsImport.put("filename", toModelFilename(im)); | ||
tsImports.add(tsImport); | ||
} | ||
return tsImports; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> postProcessOperations(Map<String, Object> operations) { | ||
// Add additional filename information for model imports in the services | ||
List<Map<String, Object>> imports = (List<Map<String, Object>>) operations.get("imports"); | ||
for(Map<String, Object> im : imports) { | ||
im.put("filename", im.get("import")); | ||
im.put("classname", getModelnameFromModelFilename(im.get("filename").toString())); | ||
} | ||
|
||
// if there are any form params, we'll need to import stringify | ||
Map<String, Object> opsObj = (Map<String, Object>) operations.get("operations"); | ||
List<CodegenOperation> ops = (List<CodegenOperation>)opsObj.get("operation"); | ||
boolean anyHasFormParams = false; | ||
for (CodegenOperation op : ops) { | ||
if (op.getHasFormParams()) { | ||
anyHasFormParams = true; | ||
} | ||
} | ||
if (anyHasFormParams) { | ||
operations.put("hasFormParams", true); | ||
} | ||
|
||
return operations; | ||
} | ||
|
||
private String getModelnameFromModelFilename(String filename) { | ||
return filename.substring((modelPackage() + "/").length()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 15 additions & 3 deletions
18
...es/swagger-codegen/src/main/resources/kotlin-client/infrastructure/Serializer.kt.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
package {{packageName}}.infrastructure | ||
|
||
import com.squareup.moshi.KotlinJsonAdapterFactory | ||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.Rfc3339DateJsonAdapter | ||
import com.squareup.moshi.* | ||
import java.math.BigDecimal | ||
import java.time.LocalDate | ||
import java.util.* | ||
|
||
object Serializer { | ||
@JvmStatic | ||
val moshi: Moshi = Moshi.Builder() | ||
.add(KotlinJsonAdapterFactory()) | ||
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) | ||
.add(LocalDateAdapter) | ||
.add(BigDecimalAdapter) | ||
.build() | ||
} | ||
|
||
object LocalDateAdapter { | ||
@FromJson fun fromJson(string: String) = LocalDate.parse(string) | ||
@ToJson fun toJson(value: LocalDate) = value.toString() | ||
} | ||
|
||
object BigDecimalAdapter { | ||
@FromJson fun fromJson(string: String) = BigDecimal(string) | ||
@ToJson fun toJson(value: BigDecimal) = value.toString() | ||
} |
Oops, something went wrong.