Skip to content

Commit

Permalink
typescript-fetch: fix basic type errors
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 committed Jul 18, 2019
1 parent 26b8d1f commit 749d44b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 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}}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class BaseAPI {
// only add the querystring to the URL if there are query parameters.
// this is done to avoid urls ending with a "?" character which buggy webservers
// do not handle correctly sometimes.
url += '?' + querystring(context.query);
url += '?' + this.configuration.queryParamsStringify(context.query);
}
const body = (context.body instanceof FormData || isBlob(context.body))
? context.body
Expand Down Expand Up @@ -127,6 +127,7 @@ export interface ConfigurationParameters {
basePath?: string; // override base path
fetchApi?: FetchAPI; // override for fetch implementation
middleware?: Middleware[]; // middleware to apply before/after fetch requests
queryParamsStringify?: (params: HTTPQuery) => string; // stringify function for query strings
username?: string; // parameter for basic security
password?: string; // parameter for basic security
apiKey?: string | ((name: string) => string); // parameter for apiKey security
Expand All @@ -148,6 +149,10 @@ export class Configuration {
return this.configuration.middleware || [];
}

get queryParamsStringify(): (params: HTTPQuery) => string {
return this.configuration.queryParamsStringify || querystring;
}

get username(): string | undefined {
return this.configuration.username;
}
Expand Down

0 comments on commit 749d44b

Please sign in to comment.