Skip to content

Commit 724753f

Browse files
committed
Merge pull request #2272 from wing328/typescript_prefix_suffic
[TypeScript] add prefix, suffix to TS model
2 parents 9f3c34d + 74d91f4 commit 724753f

File tree

9 files changed

+400
-224
lines changed

9 files changed

+400
-224
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractTypeScriptClientCodegen.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,26 @@ public AbstractTypeScriptClientCodegen() {
1717
supportsInheritance = true;
1818
setReservedWordsLowerCase(Arrays.asList(
1919
// local variable names used in API methods (endpoints)
20-
"path", "queryParameters", "headerParams", "formParams", "useFormData", "deferred",
20+
"varLocalPath", "queryParameters", "headerParams", "formParams", "useFormData", "varLocalDeferred",
2121
"requestOptions",
2222
// Typescript reserved words
2323
"abstract", "await", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do", "double", "else", "enum", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "let", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "transient", "true", "try", "typeof", "var", "void", "volatile", "while", "with", "yield"));
2424

2525
languageSpecificPrimitives = new HashSet<String>(Arrays.asList(
26+
"string",
2627
"String",
2728
"boolean",
2829
"Boolean",
2930
"Double",
3031
"Integer",
3132
"Long",
3233
"Float",
33-
"Object"));
34+
"Object",
35+
"Array",
36+
"Date",
37+
"number",
38+
"any"
39+
));
3440
instantiationTypes.put("array", "Array");
3541

3642
typeMapping = new HashMap<String, String>();
@@ -116,10 +122,22 @@ public String toVarName(String name) {
116122

117123
@Override
118124
public String toModelName(String name) {
119-
// model name cannot use reserved keyword, e.g. return
120-
if (isReservedWord(name))
121-
throw new RuntimeException(name
122-
+ " (reserved word) cannot be used as a model name");
125+
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
126+
127+
if (!StringUtils.isEmpty(modelNamePrefix)) {
128+
name = modelNamePrefix + "_" + name;
129+
}
130+
131+
if (!StringUtils.isEmpty(modelNameSuffix)) {
132+
name = name + "_" + modelNameSuffix;
133+
}
134+
135+
// model name cannot use reserved keyword, e.g. return
136+
if (isReservedWord(name)) {
137+
String modelName = camelize("object_" + name);
138+
LOGGER.warn(name + " (reserved word) cannot be used as model name. Renamed to " + modelName);
139+
return modelName;
140+
}
123141

124142
// camelize the model name
125143
// phone_number => PhoneNumber
@@ -158,7 +176,7 @@ public String getSwaggerType(Property p) {
158176
return type;
159177
} else
160178
type = swaggerType;
161-
return type;
179+
return toModelName(type);
162180
}
163181

164182
@Override

modules/swagger-codegen/src/main/resources/TypeScript-Angular/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace {{package}} {
3939
{{#allParams}}* @param {{paramName}} {{description}}
4040
{{/allParams}}*/
4141
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any ) : ng.IHttpPromise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
42-
const path = this.basePath + '{{path}}'{{#pathParams}}
42+
const localVarPath = this.basePath + '{{path}}'{{#pathParams}}
4343
.replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};
4444

4545
let queryParameters: any = {};
@@ -76,7 +76,7 @@ namespace {{package}} {
7676
{{/formParams}}
7777
let httpRequestParams: any = {
7878
method: '{{httpMethod}}',
79-
url: path,
79+
url: localVarPath,
8080
json: {{#hasFormParams}}false{{/hasFormParams}}{{^hasFormParams}}true{{/hasFormParams}},
8181
{{#bodyParam}}data: {{paramName}},
8282
{{/bodyParam}}

modules/swagger-codegen/src/main/resources/TypeScript-node/api.mustache

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export class {{classname}} {
179179
{{#allParams}}* @param {{paramName}} {{description}}
180180
{{/allParams}}*/
181181
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) : Promise<{ response: http.ClientResponse; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }> {
182-
const path = this.basePath + '{{path}}'{{#pathParams}}
182+
const localVarPath = this.basePath + '{{path}}'{{#pathParams}}
183183
.replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};
184184
let queryParameters: any = {};
185185
let headerParams: any = this.extendObj({}, this.defaultHeaders);
@@ -212,13 +212,13 @@ export class {{classname}} {
212212
{{/isFile}}
213213

214214
{{/formParams}}
215-
let deferred = promise.defer<{ response: http.ClientResponse; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }>();
215+
let localVarDeferred = promise.defer<{ response: http.ClientResponse; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }>();
216216

217217
let requestOptions: request.Options = {
218218
method: '{{httpMethod}}',
219219
qs: queryParameters,
220220
headers: headerParams,
221-
uri: path,
221+
uri: localVarPath,
222222
json: true,
223223
{{#bodyParam}}
224224
body: {{paramName}},
@@ -241,17 +241,17 @@ export class {{classname}} {
241241

242242
request(requestOptions, (error, response, body) => {
243243
if (error) {
244-
deferred.reject(error);
244+
localVarDeferred.reject(error);
245245
} else {
246246
if (response.statusCode >= 200 && response.statusCode <= 299) {
247-
deferred.resolve({ response: response, body: body });
247+
localVarDeferred.resolve({ response: response, body: body });
248248
} else {
249-
deferred.reject({ response: response, body: body });
249+
localVarDeferred.reject({ response: response, body: body });
250250
}
251251
}
252252
});
253253

254-
return deferred.promise;
254+
return localVarDeferred.promise;
255255
}
256256
{{/operation}}
257257
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptangular/TypeScriptAngularModelTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void simpleModelTest() {
5959

6060
final CodegenProperty property3 = cm.vars.get(2);
6161
Assert.assertEquals(property3.baseName, "createdAt");
62-
Assert.assertEquals(property3.complexType, "Date");
62+
Assert.assertEquals(property3.complexType, null);
6363
Assert.assertEquals(property3.datatype, "Date");
6464
Assert.assertEquals(property3.name, "createdAt");
6565
Assert.assertEquals(property3.defaultValue, "null");

modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptnode/TypeScriptNodeModelTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void simpleModelTest() {
5959

6060
final CodegenProperty property3 = cm.vars.get(2);
6161
Assert.assertEquals(property3.baseName, "createdAt");
62-
Assert.assertEquals(property3.complexType, "Date");
62+
Assert.assertEquals(property3.complexType, null);
6363
Assert.assertEquals(property3.datatype, "Date");
6464
Assert.assertEquals(property3.name, "createdAt");
6565
Assert.assertEquals(property3.defaultValue, "null");

samples/client/petstore/typescript-angular/API/Client/PetApi.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ namespace API.Client {
3232
* @param body Pet object that needs to be added to the store
3333
*/
3434
public updatePet (body?: Pet, extraHttpRequestParams?: any ) : ng.IHttpPromise<{}> {
35-
const path = this.basePath + '/pet';
35+
const localVarPath = this.basePath + '/pet';
3636

3737
let queryParameters: any = {};
3838
let headerParams: any = this.extendObj({}, this.defaultHeaders);
3939
let httpRequestParams: any = {
4040
method: 'PUT',
41-
url: path,
41+
url: localVarPath,
4242
json: true,
4343
data: body,
4444

@@ -59,13 +59,13 @@ namespace API.Client {
5959
* @param body Pet object that needs to be added to the store
6060
*/
6161
public addPet (body?: Pet, extraHttpRequestParams?: any ) : ng.IHttpPromise<{}> {
62-
const path = this.basePath + '/pet';
62+
const localVarPath = this.basePath + '/pet';
6363

6464
let queryParameters: any = {};
6565
let headerParams: any = this.extendObj({}, this.defaultHeaders);
6666
let httpRequestParams: any = {
6767
method: 'POST',
68-
url: path,
68+
url: localVarPath,
6969
json: true,
7070
data: body,
7171

@@ -82,11 +82,11 @@ namespace API.Client {
8282
}
8383
/**
8484
* Finds Pets by status
85-
* Multiple status values can be provided with comma seperated strings
86-
* @param status Status values that need to be considered for filter
85+
* Multiple status values can be provided with comma separated strings
86+
* @param status Status values that need to be considered for query
8787
*/
8888
public findPetsByStatus (status?: Array<string>, extraHttpRequestParams?: any ) : ng.IHttpPromise<Array<Pet>> {
89-
const path = this.basePath + '/pet/findByStatus';
89+
const localVarPath = this.basePath + '/pet/findByStatus';
9090

9191
let queryParameters: any = {};
9292
let headerParams: any = this.extendObj({}, this.defaultHeaders);
@@ -96,7 +96,7 @@ namespace API.Client {
9696

9797
let httpRequestParams: any = {
9898
method: 'GET',
99-
url: path,
99+
url: localVarPath,
100100
json: true,
101101

102102

@@ -116,7 +116,7 @@ namespace API.Client {
116116
* @param tags Tags to filter by
117117
*/
118118
public findPetsByTags (tags?: Array<string>, extraHttpRequestParams?: any ) : ng.IHttpPromise<Array<Pet>> {
119-
const path = this.basePath + '/pet/findByTags';
119+
const localVarPath = this.basePath + '/pet/findByTags';
120120

121121
let queryParameters: any = {};
122122
let headerParams: any = this.extendObj({}, this.defaultHeaders);
@@ -126,7 +126,7 @@ namespace API.Client {
126126

127127
let httpRequestParams: any = {
128128
method: 'GET',
129-
url: path,
129+
url: localVarPath,
130130
json: true,
131131

132132

@@ -146,7 +146,7 @@ namespace API.Client {
146146
* @param petId ID of pet that needs to be fetched
147147
*/
148148
public getPetById (petId: number, extraHttpRequestParams?: any ) : ng.IHttpPromise<Pet> {
149-
const path = this.basePath + '/pet/{petId}'
149+
const localVarPath = this.basePath + '/pet/{petId}'
150150
.replace('{' + 'petId' + '}', String(petId));
151151

152152
let queryParameters: any = {};
@@ -157,7 +157,7 @@ namespace API.Client {
157157
}
158158
let httpRequestParams: any = {
159159
method: 'GET',
160-
url: path,
160+
url: localVarPath,
161161
json: true,
162162

163163

@@ -179,7 +179,7 @@ namespace API.Client {
179179
* @param status Updated status of the pet
180180
*/
181181
public updatePetWithForm (petId: string, name?: string, status?: string, extraHttpRequestParams?: any ) : ng.IHttpPromise<{}> {
182-
const path = this.basePath + '/pet/{petId}'
182+
const localVarPath = this.basePath + '/pet/{petId}'
183183
.replace('{' + 'petId' + '}', String(petId));
184184

185185
let queryParameters: any = {};
@@ -198,7 +198,7 @@ namespace API.Client {
198198

199199
let httpRequestParams: any = {
200200
method: 'POST',
201-
url: path,
201+
url: localVarPath,
202202
json: false,
203203

204204
data: this.$httpParamSerializer(formParams),
@@ -220,7 +220,7 @@ namespace API.Client {
220220
* @param apiKey
221221
*/
222222
public deletePet (petId: number, apiKey?: string, extraHttpRequestParams?: any ) : ng.IHttpPromise<{}> {
223-
const path = this.basePath + '/pet/{petId}'
223+
const localVarPath = this.basePath + '/pet/{petId}'
224224
.replace('{' + 'petId' + '}', String(petId));
225225

226226
let queryParameters: any = {};
@@ -233,7 +233,7 @@ namespace API.Client {
233233

234234
let httpRequestParams: any = {
235235
method: 'DELETE',
236-
url: path,
236+
url: localVarPath,
237237
json: true,
238238

239239

@@ -255,7 +255,7 @@ namespace API.Client {
255255
* @param file file to upload
256256
*/
257257
public uploadFile (petId: number, additionalMetadata?: string, file?: any, extraHttpRequestParams?: any ) : ng.IHttpPromise<{}> {
258-
const path = this.basePath + '/pet/{petId}/uploadImage'
258+
const localVarPath = this.basePath + '/pet/{petId}/uploadImage'
259259
.replace('{' + 'petId' + '}', String(petId));
260260

261261
let queryParameters: any = {};
@@ -274,7 +274,7 @@ namespace API.Client {
274274

275275
let httpRequestParams: any = {
276276
method: 'POST',
277-
url: path,
277+
url: localVarPath,
278278
json: false,
279279

280280
data: this.$httpParamSerializer(formParams),
@@ -294,19 +294,19 @@ namespace API.Client {
294294
* Returns a pet when ID &lt; 10. ID &gt; 10 or nonintegers will simulate API error conditions
295295
* @param petId ID of pet that needs to be fetched
296296
*/
297-
public getPetByIdWithByteArray (petId: number, extraHttpRequestParams?: any ) : ng.IHttpPromise<string> {
298-
const path = this.basePath + '/pet/{petId}?testing_byte_array=true'
297+
public petPetIdtestingByteArraytrueGet (petId: number, extraHttpRequestParams?: any ) : ng.IHttpPromise<string> {
298+
const localVarPath = this.basePath + '/pet/{petId}?testing_byte_array=true'
299299
.replace('{' + 'petId' + '}', String(petId));
300300

301301
let queryParameters: any = {};
302302
let headerParams: any = this.extendObj({}, this.defaultHeaders);
303303
// verify required parameter 'petId' is set
304304
if (!petId) {
305-
throw new Error('Missing required parameter petId when calling getPetByIdWithByteArray');
305+
throw new Error('Missing required parameter petId when calling petPetIdtestingByteArraytrueGet');
306306
}
307307
let httpRequestParams: any = {
308308
method: 'GET',
309-
url: path,
309+
url: localVarPath,
310310
json: true,
311311

312312

@@ -326,13 +326,13 @@ namespace API.Client {
326326
* @param body Pet object in the form of byte array
327327
*/
328328
public addPetUsingByteArray (body?: string, extraHttpRequestParams?: any ) : ng.IHttpPromise<{}> {
329-
const path = this.basePath + '/pet?testing_byte_array=true';
329+
const localVarPath = this.basePath + '/pet?testing_byte_array=true';
330330

331331
let queryParameters: any = {};
332332
let headerParams: any = this.extendObj({}, this.defaultHeaders);
333333
let httpRequestParams: any = {
334334
method: 'POST',
335-
url: path,
335+
url: localVarPath,
336336
json: true,
337337
data: body,
338338

0 commit comments

Comments
 (0)