Skip to content

Commit

Permalink
typescript-fetch: fix basic type errors (#3380)
Browse files Browse the repository at this point in the history
* use proper response for simple object return types
* exclude api and/or model support files and imports if none are generated

Signed-off-by: Prateek Malhotra <someone1@gmail.com>
  • Loading branch information
someone1 authored and macjohnny committed Jul 19, 2019
1 parent 944e1c3 commit 84e2f60
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege

protected String npmRepository = null;
private boolean useSingleRequestParameter = true;
protected boolean addedApiIndex = false;
protected boolean addedModelIndex = false;


public TypeScriptFetchClientCodegen() {
super();
Expand Down Expand Up @@ -83,8 +86,6 @@ public void processOpts() {
additionalProperties.put("modelPropertyNaming", getModelPropertyNaming());
supportingFiles.add(new SupportingFile("index.mustache", "", "index.ts"));
supportingFiles.add(new SupportingFile("runtime.mustache", "", "runtime.ts"));
supportingFiles.add(new SupportingFile("apis.index.mustache", apiPackage().replace('.', File.separatorChar), "index.ts"));
supportingFiles.add(new SupportingFile("models.index.mustache", modelPackage().replace('.', File.separatorChar), "index.ts"));
supportingFiles.add(new SupportingFile("tsconfig.mustache", "", "tsconfig.json"));
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));

Expand Down Expand Up @@ -127,8 +128,9 @@ protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Sc

@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
// process enum in models
List<Object> models = (List<Object>) postProcessModelsEnum(objs).get("models");

// process enum in models
for (Object _mo : models) {
Map<String, Object> mo = (Map<String, Object>) _mo;
CodegenModel cm = (CodegenModel) mo.get("model");
Expand Down Expand Up @@ -190,8 +192,21 @@ private void addNpmPackageGeneration() {

@Override
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> operations, List<Object> allModels) {
// Add supporting file only if we plan to generate files in /apis
if (operations.size() > 0 && !addedApiIndex) {
addedApiIndex = true;
supportingFiles.add(new SupportingFile("apis.index.mustache", apiPackage().replace('.', File.separatorChar), "index.ts"));
}

// Add supporting file only if we plan to generate files in /models
if (allModels.size() > 0 && !addedModelIndex) {
addedModelIndex = true;
supportingFiles.add(new SupportingFile("models.index.mustache", modelPackage().replace('.', File.separatorChar), "index.ts"));
}

this.addOperationModelImportInfomation(operations);
this.updateOperationParameterEnumInformation(operations);
this.addOperationObjectResponseInformation(operations);
return operations;
}

Expand Down Expand Up @@ -224,6 +239,20 @@ private void updateOperationParameterEnumInformation(Map<String, Object> operati
operations.put("hasEnums", hasEnum);
}

private void addOperationObjectResponseInformation(Map<String, Object> operations) {
// This method will modify the infomation on the operations' return type.
// The api template uses this infomation to know when to return a text
// response for a given simple response operation.
Map<String, Object> _operations = (Map<String, Object>) operations.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) _operations.get("operation");
for (CodegenOperation op : operationList) {
if(op.returnType == "object") {
op.isMapContainer = true;
op.returnSimpleType = false;
}
}
}

private void addExtraReservedWords() {
this.reservedWords.add("BASE_PATH");
this.reservedWords.add("BaseAPI");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export * from './runtime';
{{#apiInfo}}
{{#apis.0}}
export * from './apis';
{{/apis.0}}
{{/apiInfo}}
{{#models.0}}
export * from './models';
{{/models.0}}

0 comments on commit 84e2f60

Please sign in to comment.