Skip to content

[TypeScript] add prefix, suffix to TS model #2272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@ public AbstractTypeScriptClientCodegen() {
supportsInheritance = true;
setReservedWordsLowerCase(Arrays.asList(
// local variable names used in API methods (endpoints)
"path", "queryParameters", "headerParams", "formParams", "useFormData", "deferred",
"varLocalPath", "queryParameters", "headerParams", "formParams", "useFormData", "varLocalDeferred",
"requestOptions",
// Typescript reserved words
"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"));

languageSpecificPrimitives = new HashSet<String>(Arrays.asList(
"string",
"String",
"boolean",
"Boolean",
"Double",
"Integer",
"Long",
"Float",
"Object"));
"Object",
"Array",
"Date",
"number",
"any"
));
instantiationTypes.put("array", "Array");

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

@Override
public String toModelName(String name) {
// model name cannot use reserved keyword, e.g. return
if (isReservedWord(name))
throw new RuntimeException(name
+ " (reserved word) cannot be used as a model name");
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.

if (!StringUtils.isEmpty(modelNamePrefix)) {
name = modelNamePrefix + "_" + name;
}

if (!StringUtils.isEmpty(modelNameSuffix)) {
name = name + "_" + modelNameSuffix;
}

// model name cannot use reserved keyword, e.g. return
if (isReservedWord(name)) {
String modelName = camelize("object_" + name);
LOGGER.warn(name + " (reserved word) cannot be used as model name. Renamed to " + modelName);
return modelName;
}

// camelize the model name
// phone_number => PhoneNumber
Expand Down Expand Up @@ -158,7 +176,7 @@ public String getSwaggerType(Property p) {
return type;
} else
type = swaggerType;
return type;
return toModelName(type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace {{package}} {
{{#allParams}}* @param {{paramName}} {{description}}
{{/allParams}}*/
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any ) : ng.IHttpPromise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
const path = this.basePath + '{{path}}'{{#pathParams}}
const localVarPath = this.basePath + '{{path}}'{{#pathParams}}
.replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};

let queryParameters: any = {};
Expand Down Expand Up @@ -76,7 +76,7 @@ namespace {{package}} {
{{/formParams}}
let httpRequestParams: any = {
method: '{{httpMethod}}',
url: path,
url: localVarPath,
json: {{#hasFormParams}}false{{/hasFormParams}}{{^hasFormParams}}true{{/hasFormParams}},
{{#bodyParam}}data: {{paramName}},
{{/bodyParam}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class {{classname}} {
{{#allParams}}* @param {{paramName}} {{description}}
{{/allParams}}*/
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) : Promise<{ response: http.ClientResponse; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }> {
const path = this.basePath + '{{path}}'{{#pathParams}}
const localVarPath = this.basePath + '{{path}}'{{#pathParams}}
.replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};
let queryParameters: any = {};
let headerParams: any = this.extendObj({}, this.defaultHeaders);
Expand Down Expand Up @@ -212,13 +212,13 @@ export class {{classname}} {
{{/isFile}}

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

let requestOptions: request.Options = {
method: '{{httpMethod}}',
qs: queryParameters,
headers: headerParams,
uri: path,
uri: localVarPath,
json: true,
{{#bodyParam}}
body: {{paramName}},
Expand All @@ -241,17 +241,17 @@ export class {{classname}} {

request(requestOptions, (error, response, body) => {
if (error) {
deferred.reject(error);
localVarDeferred.reject(error);
} else {
if (response.statusCode >= 200 && response.statusCode <= 299) {
deferred.resolve({ response: response, body: body });
localVarDeferred.resolve({ response: response, body: body });
} else {
deferred.reject({ response: response, body: body });
localVarDeferred.reject({ response: response, body: body });
}
}
});

return deferred.promise;
return localVarDeferred.promise;
}
{{/operation}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void simpleModelTest() {

final CodegenProperty property3 = cm.vars.get(2);
Assert.assertEquals(property3.baseName, "createdAt");
Assert.assertEquals(property3.complexType, "Date");
Assert.assertEquals(property3.complexType, null);
Assert.assertEquals(property3.datatype, "Date");
Assert.assertEquals(property3.name, "createdAt");
Assert.assertEquals(property3.defaultValue, "null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void simpleModelTest() {

final CodegenProperty property3 = cm.vars.get(2);
Assert.assertEquals(property3.baseName, "createdAt");
Assert.assertEquals(property3.complexType, "Date");
Assert.assertEquals(property3.complexType, null);
Assert.assertEquals(property3.datatype, "Date");
Assert.assertEquals(property3.name, "createdAt");
Assert.assertEquals(property3.defaultValue, "null");
Expand Down
48 changes: 24 additions & 24 deletions samples/client/petstore/typescript-angular/API/Client/PetApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ namespace API.Client {
* @param body Pet object that needs to be added to the store
*/
public updatePet (body?: Pet, extraHttpRequestParams?: any ) : ng.IHttpPromise<{}> {
const path = this.basePath + '/pet';
const localVarPath = this.basePath + '/pet';

let queryParameters: any = {};
let headerParams: any = this.extendObj({}, this.defaultHeaders);
let httpRequestParams: any = {
method: 'PUT',
url: path,
url: localVarPath,
json: true,
data: body,

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

let queryParameters: any = {};
let headerParams: any = this.extendObj({}, this.defaultHeaders);
let httpRequestParams: any = {
method: 'POST',
url: path,
url: localVarPath,
json: true,
data: body,

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

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

let httpRequestParams: any = {
method: 'GET',
url: path,
url: localVarPath,
json: true,


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

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

let httpRequestParams: any = {
method: 'GET',
url: path,
url: localVarPath,
json: true,


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

let queryParameters: any = {};
Expand All @@ -157,7 +157,7 @@ namespace API.Client {
}
let httpRequestParams: any = {
method: 'GET',
url: path,
url: localVarPath,
json: true,


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

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

let httpRequestParams: any = {
method: 'POST',
url: path,
url: localVarPath,
json: false,

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

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

let httpRequestParams: any = {
method: 'DELETE',
url: path,
url: localVarPath,
json: true,


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

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

let httpRequestParams: any = {
method: 'POST',
url: path,
url: localVarPath,
json: false,

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

let queryParameters: any = {};
let headerParams: any = this.extendObj({}, this.defaultHeaders);
// verify required parameter 'petId' is set
if (!petId) {
throw new Error('Missing required parameter petId when calling getPetByIdWithByteArray');
throw new Error('Missing required parameter petId when calling petPetIdtestingByteArraytrueGet');
}
let httpRequestParams: any = {
method: 'GET',
url: path,
url: localVarPath,
json: true,


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

let queryParameters: any = {};
let headerParams: any = this.extendObj({}, this.defaultHeaders);
let httpRequestParams: any = {
method: 'POST',
url: path,
url: localVarPath,
json: true,
data: body,

Expand Down
Loading