diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java index 5521d72e7b36..f66340956104 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java @@ -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(); @@ -83,8 +86,6 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege 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")); @@ -127,8 +128,9 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege @Override public Map postProcessModels(Map objs) { - // process enum in models List models = (List) postProcessModelsEnum(objs).get("models"); + + // process enum in models for (Object _mo : models) { Map mo = (Map) _mo; CodegenModel cm = (CodegenModel) mo.get("model"); @@ -190,8 +192,21 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege @Override public Map postProcessOperationsWithModels(Map operations, List 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; } @@ -224,6 +239,20 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege operations.put("hasEnums", hasEnum); } + private void addOperationObjectResponseInformation(Map 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 _operations = (Map) operations.get("operations"); + List operationList = (List) _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"); diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/index.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/index.mustache index 848ecfa4d100..e548c9df6b2c 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/index.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/index.mustache @@ -1,3 +1,9 @@ export * from './runtime'; +{{#apiInfo}} +{{#apis.0}} export * from './apis'; +{{/apis.0}} +{{/apiInfo}} +{{#models.0}} export * from './models'; +{{/models.0}} \ No newline at end of file