Skip to content

PR-1334 Improved angular 2 - typescript generation #5695

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

Closed
Closed
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 @@ -277,7 +277,7 @@ public String toEnumValue(String value, String datatype) {
if ("number".equals(datatype)) {
return value;
} else {
return "\'" + escapeText(value) + "\'";
return "\"" + escapeText(value) + "\"";
}
}

Expand Down Expand Up @@ -345,7 +345,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
for (CodegenProperty var : cm.vars) {
// name enum with model name, e.g. StatuEnum => Pet.StatusEnum
if (Boolean.TRUE.equals(var.isEnum)) {
var.datatypeWithEnum = var.datatypeWithEnum.replace(var.enumName, cm.classname + "." + var.enumName);
var.datatypeWithEnum = var.datatypeWithEnum.replace(var.enumName, cm.classname + "Enums." + var.enumName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
protected String npmName = null;
protected String npmVersion = "1.0.0";
protected String npmRepository = null;
protected String injectionToken = "InjectionToken<string>";
protected String injectionToken = "InjectionToken";

public TypeScriptAngular2ClientCodegen() {
super();
Expand All @@ -41,7 +41,8 @@ public TypeScriptAngular2ClientCodegen() {
embeddedTemplateDir = templateDir = "typescript-angular2";
modelTemplateFiles.put("model.mustache", ".ts");
apiTemplateFiles.put("api.mustache", ".ts");
typeMapping.put("Date","Date");
typeMapping.put("Date", "Date");
typeMapping.put("Array", "Array");
apiPackage = "api";
modelPackage = "model";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ export class {{classname}} {
.map((response: Response) => {
if (response.status === 204) {
return undefined;
} else {
}
try {
return response.json() || {};
}catch(e){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a space between } and catch.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure thing.

return {};
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface {{classname}} {{#parent}}extends models.{{{parent}}} {{/parent}}{
export class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
{{#additionalPropertiesType}}
[key: string]: {{{additionalPropertiesType}}}{{#hasVars}} | any{{/hasVars}};

Expand All @@ -13,7 +13,7 @@ export interface {{classname}} {{#parent}}extends models.{{{parent}}} {{/parent}

{{/vars}}
}{{#hasEnums}}
export namespace {{classname}} {
export namespace {{classname}}Enums {
{{#vars}}
{{#isEnum}}
export enum {{enumName}} {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { {{{injectionToken}}} } from '@angular/core';

export const BASE_PATH = new {{{injectionToken}}}('basePath');
export const BASE_PATH = new {{{injectionToken}}}<String>('basePath');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on this change? Shouldn't it be string like before?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see: https://angular.io/docs/ts/latest/api/core/index/InjectionToken-class.html
I changed it because my app just wouldn't compile otherwise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's weird. Can you post a minimum working example which doesn't compile?
string is the TypeScript string type and String is the default JavaScript string type, which is why I am wondering if the underlying issue is something else.
See e.g. here

export const COLLECTION_FORMATS = {
'csv': ',',
'tsv': ' ',
'ssv': ' ',
'pipes': '|'
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ public void enumArrayMdoelTest() {

HashMap<String, String> fish= new HashMap<String, String>();
fish.put("name", "Fish");
fish.put("value", "'fish'");
fish.put("value", "\"fish\"");
HashMap<String, String> crab= new HashMap<String, String>();
crab.put("name", "Crab");
crab.put("value", "'crab'");
crab.put("value", "\"crab\"");
Assert.assertEquals(prope.allowableValues.get("enumVars"), Arrays.asList(fish, crab));

// assert inner items
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.2.3-SNAPSHOT
98 changes: 48 additions & 50 deletions samples/client/petstore/typescript-angular2/default/api/PetApi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Swagger Petstore
* This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.
* This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters
*
* OpenAPI spec version: 1.0.0
* Contact: apiteam@swagger.io
* Contact: apiteam@wordnik.com
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was probably not intended. Could you please revert it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest master is using petstore.yaml instead of petstore.json (which still references wordnik.com) so rebasing the PR on the latest master should avoid the above change.

*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
Expand Down Expand Up @@ -45,7 +45,7 @@ export class PetApi {
*
* @param body Pet object that needs to be added to the store
*/
public addPet(body: models.Pet, extraHttpRequestParams?: any): Observable<{}> {
public addPet(body?: models.Pet, extraHttpRequestParams?: any): Observable<{}> {
return this.addPetWithHttpInfo(body, extraHttpRequestParams)
.map((response: Response) => {
if (response.status === 204) {
Expand Down Expand Up @@ -78,7 +78,7 @@ export class PetApi {
* Multiple status values can be provided with comma separated strings
* @param status Status values that need to be considered for filter
*/
public findPetsByStatus(status: Array<string>, extraHttpRequestParams?: any): Observable<Array<models.Pet>> {
public findPetsByStatus(status?: Array<string>, extraHttpRequestParams?: any): Observable<Array<models.Pet>> {
return this.findPetsByStatusWithHttpInfo(status, extraHttpRequestParams)
.map((response: Response) => {
if (response.status === 204) {
Expand All @@ -94,7 +94,7 @@ export class PetApi {
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
* @param tags Tags to filter by
*/
public findPetsByTags(tags: Array<string>, extraHttpRequestParams?: any): Observable<Array<models.Pet>> {
public findPetsByTags(tags?: Array<string>, extraHttpRequestParams?: any): Observable<Array<models.Pet>> {
return this.findPetsByTagsWithHttpInfo(tags, extraHttpRequestParams)
.map((response: Response) => {
if (response.status === 204) {
Expand All @@ -107,8 +107,8 @@ export class PetApi {

/**
* Find pet by ID
* Returns a single pet
* @param petId ID of pet to return
* 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 getPetById(petId: number, extraHttpRequestParams?: any): Observable<models.Pet> {
return this.getPetByIdWithHttpInfo(petId, extraHttpRequestParams)
Expand All @@ -126,7 +126,7 @@ export class PetApi {
*
* @param body Pet object that needs to be added to the store
*/
public updatePet(body: models.Pet, extraHttpRequestParams?: any): Observable<{}> {
public updatePet(body?: models.Pet, extraHttpRequestParams?: any): Observable<{}> {
return this.updatePetWithHttpInfo(body, extraHttpRequestParams)
.map((response: Response) => {
if (response.status === 204) {
Expand All @@ -144,7 +144,7 @@ export class PetApi {
* @param name Updated name of the pet
* @param status Updated status of the pet
*/
public updatePetWithForm(petId: number, name?: string, status?: string, extraHttpRequestParams?: any): Observable<{}> {
public updatePetWithForm(petId: string, name?: string, status?: string, extraHttpRequestParams?: any): Observable<{}> {
return this.updatePetWithFormWithHttpInfo(petId, name, status, extraHttpRequestParams)
.map((response: Response) => {
if (response.status === 204) {
Expand All @@ -162,7 +162,7 @@ export class PetApi {
* @param additionalMetadata Additional data to pass to server
* @param file file to upload
*/
public uploadFile(petId: number, additionalMetadata?: string, file?: any, extraHttpRequestParams?: any): Observable<models.ApiResponse> {
public uploadFile(petId: number, additionalMetadata?: string, file?: any, extraHttpRequestParams?: any): Observable<{}> {
return this.uploadFileWithHttpInfo(petId, additionalMetadata, file, extraHttpRequestParams)
.map((response: Response) => {
if (response.status === 204) {
Expand All @@ -179,15 +179,11 @@ export class PetApi {
*
* @param body Pet object that needs to be added to the store
*/
public addPetWithHttpInfo(body: models.Pet, extraHttpRequestParams?: any): Observable<Response> {
public addPetWithHttpInfo(body?: models.Pet, extraHttpRequestParams?: any): Observable<Response> {
const path = this.basePath + '/pet';

let queryParameters = new URLSearchParams();
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
// verify required parameter 'body' is not null or undefined
if (body === null || body === undefined) {
throw new Error('Required parameter body was null or undefined when calling addPet.');
}
// to determine the Content-Type header
let consumes: string[] = [
'application/json',
Expand All @@ -196,8 +192,8 @@ export class PetApi {

// to determine the Accept header
let produces: string[] = [
'application/xml',
'application/json'
'application/json',
'application/xml'
];

// authentication (petstore_auth) required
Expand Down Expand Up @@ -250,8 +246,8 @@ export class PetApi {

// to determine the Accept header
let produces: string[] = [
'application/xml',
'application/json'
'application/json',
'application/xml'
];

// authentication (petstore_auth) required
Expand Down Expand Up @@ -282,17 +278,15 @@ export class PetApi {
* Multiple status values can be provided with comma separated strings
* @param status Status values that need to be considered for filter
*/
public findPetsByStatusWithHttpInfo(status: Array<string>, extraHttpRequestParams?: any): Observable<Response> {
public findPetsByStatusWithHttpInfo(status?: Array<string>, extraHttpRequestParams?: any): Observable<Response> {
const path = this.basePath + '/pet/findByStatus';

let queryParameters = new URLSearchParams();
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
// verify required parameter 'status' is not null or undefined
if (status === null || status === undefined) {
throw new Error('Required parameter status was null or undefined when calling findPetsByStatus.');
}
if (status) {
queryParameters.set('status', status.join(COLLECTION_FORMATS['csv']));
status.forEach((element) => {
queryParameters.append('status', <any>element);
})
}

// to determine the Content-Type header
Expand All @@ -301,8 +295,8 @@ export class PetApi {

// to determine the Accept header
let produces: string[] = [
'application/xml',
'application/json'
'application/json',
'application/xml'
];

// authentication (petstore_auth) required
Expand Down Expand Up @@ -333,17 +327,15 @@ export class PetApi {
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
* @param tags Tags to filter by
*/
public findPetsByTagsWithHttpInfo(tags: Array<string>, extraHttpRequestParams?: any): Observable<Response> {
public findPetsByTagsWithHttpInfo(tags?: Array<string>, extraHttpRequestParams?: any): Observable<Response> {
const path = this.basePath + '/pet/findByTags';

let queryParameters = new URLSearchParams();
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
// verify required parameter 'tags' is not null or undefined
if (tags === null || tags === undefined) {
throw new Error('Required parameter tags was null or undefined when calling findPetsByTags.');
}
if (tags) {
queryParameters.set('tags', tags.join(COLLECTION_FORMATS['csv']));
tags.forEach((element) => {
queryParameters.append('tags', <any>element);
})
}

// to determine the Content-Type header
Expand All @@ -352,8 +344,8 @@ export class PetApi {

// to determine the Accept header
let produces: string[] = [
'application/xml',
'application/json'
'application/json',
'application/xml'
];

// authentication (petstore_auth) required
Expand Down Expand Up @@ -381,8 +373,8 @@ export class PetApi {

/**
* Find pet by ID
* Returns a single pet
* @param petId ID of pet to return
* 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 getPetByIdWithHttpInfo(petId: number, extraHttpRequestParams?: any): Observable<Response> {
const path = this.basePath + '/pet/${petId}'
Expand All @@ -400,15 +392,24 @@ export class PetApi {

// to determine the Accept header
let produces: string[] = [
'application/xml',
'application/json'
'application/json',
'application/xml'
];

// authentication (api_key) required
if (this.configuration.apiKey) {
headers.set('api_key', this.configuration.apiKey);
}

// authentication (petstore_auth) required
// oauth required
if (this.configuration.accessToken) {
let accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers.set('Authorization', 'Bearer ' + accessToken);
}

let requestOptions: RequestOptionsArgs = new RequestOptions({
method: RequestMethod.Get,
headers: headers,
Expand All @@ -428,15 +429,11 @@ export class PetApi {
*
* @param body Pet object that needs to be added to the store
*/
public updatePetWithHttpInfo(body: models.Pet, extraHttpRequestParams?: any): Observable<Response> {
public updatePetWithHttpInfo(body?: models.Pet, extraHttpRequestParams?: any): Observable<Response> {
const path = this.basePath + '/pet';

let queryParameters = new URLSearchParams();
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
// verify required parameter 'body' is not null or undefined
if (body === null || body === undefined) {
throw new Error('Required parameter body was null or undefined when calling updatePet.');
}
// to determine the Content-Type header
let consumes: string[] = [
'application/json',
Expand All @@ -445,8 +442,8 @@ export class PetApi {

// to determine the Accept header
let produces: string[] = [
'application/xml',
'application/json'
'application/json',
'application/xml'
];

// authentication (petstore_auth) required
Expand Down Expand Up @@ -482,7 +479,7 @@ export class PetApi {
* @param name Updated name of the pet
* @param status Updated status of the pet
*/
public updatePetWithFormWithHttpInfo(petId: number, name?: string, status?: string, extraHttpRequestParams?: any): Observable<Response> {
public updatePetWithFormWithHttpInfo(petId: string, name?: string, status?: string, extraHttpRequestParams?: any): Observable<Response> {
const path = this.basePath + '/pet/${petId}'
.replace('${' + 'petId' + '}', String(petId));

Expand All @@ -501,8 +498,8 @@ export class PetApi {

// to determine the Accept header
let produces: string[] = [
'application/xml',
'application/json'
'application/json',
'application/xml'
];

// authentication (petstore_auth) required
Expand Down Expand Up @@ -565,7 +562,8 @@ export class PetApi {

// to determine the Accept header
let produces: string[] = [
'application/json'
'application/json',
'application/xml'
];

// authentication (petstore_auth) required
Expand Down
Loading