Skip to content

Commit 1f0ecb5

Browse files
damienpontifexwing328
authored andcommitted
Updating typescript-angular to export api classes (#4589)
* Updating typescript-angular to export api classes * Fixing tsconfig for typescript-angular test case
1 parent 22688f5 commit 1f0ecb5

File tree

31 files changed

+883
-950
lines changed

31 files changed

+883
-950
lines changed

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

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.File;
44

55
import io.swagger.codegen.SupportingFile;
6+
import io.swagger.codegen.CodegenParameter;
7+
import io.swagger.models.properties.Property;
68

79
public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCodegen {
810

@@ -19,7 +21,10 @@ public String getHelp() {
1921
@Override
2022
public void processOpts() {
2123
super.processOpts();
22-
supportingFiles.add(new SupportingFile("api.d.mustache", apiPackage().replace('.', File.separatorChar), "api.d.ts"));
24+
supportingFiles.add(new SupportingFile("models.mustache", modelPackage().replace('.', File.separatorChar), "models.ts"));
25+
supportingFiles.add(new SupportingFile("apis.mustache", apiPackage().replace('.', File.separatorChar), "api.ts"));
26+
supportingFiles.add(new SupportingFile("index.mustache", getIndexDirectory(), "index.ts"));
27+
supportingFiles.add(new SupportingFile("api.module.mustache", getIndexDirectory(), "api.module.ts"));
2328
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
2429
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));
2530

@@ -29,9 +34,56 @@ public TypeScriptAngularClientCodegen() {
2934
super();
3035
outputFolder = "generated-code/typescript-angular";
3136
modelTemplateFiles.put("model.mustache", ".ts");
32-
apiTemplateFiles.put("api.mustache", ".ts");
37+
apiTemplateFiles.put("api.mustache", ".ts");
3338
embeddedTemplateDir = templateDir = "typescript-angular";
34-
apiPackage = "API.Client";
35-
modelPackage = "API.Client";
39+
apiPackage = "api";
40+
modelPackage = "model";
3641
}
42+
43+
@Override
44+
public String getSwaggerType(Property p) {
45+
String swaggerType = super.getSwaggerType(p);
46+
if(isLanguagePrimitive(swaggerType) || isLanguageGenericType(swaggerType)) {
47+
return swaggerType;
48+
}
49+
return addModelPrefix(swaggerType);
50+
}
51+
52+
@Override
53+
public void postProcessParameter(CodegenParameter parameter) {
54+
super.postProcessParameter(parameter);
55+
parameter.dataType = addModelPrefix(parameter.dataType);
56+
}
57+
58+
private String getIndexDirectory() {
59+
String indexPackage = modelPackage.substring(0, Math.max(0, modelPackage.lastIndexOf('.')));
60+
return indexPackage.replace('.', File.separatorChar);
61+
}
62+
63+
private String addModelPrefix(String swaggerType) {
64+
String type = null;
65+
if (typeMapping.containsKey(swaggerType)) {
66+
type = typeMapping.get(swaggerType);
67+
} else {
68+
type = swaggerType;
69+
}
70+
71+
if (!isLanguagePrimitive(type) && !isLanguageGenericType(type)) {
72+
type = "models." + swaggerType;
73+
}
74+
return type;
75+
}
76+
77+
private boolean isLanguagePrimitive(String type) {
78+
return languageSpecificPrimitives.contains(type);
79+
}
80+
81+
private boolean isLanguageGenericType(String type) {
82+
for (String genericType: languageGenericTypes) {
83+
if (type.startsWith(genericType + "<")) {
84+
return true;
85+
}
86+
}
87+
return false;
88+
}
3789
}

modules/swagger-codegen/src/main/resources/typescript-angular/api.d.mustache

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as api from './api/api';
2+
import * as angular from 'angular';
3+
4+
{{#apiInfo}}
5+
const apiModule = angular.module('api', [])
6+
{{#apis}}
7+
{{#operations}}
8+
.service('{{classname}}', api.{{classname}})
9+
{{/operations}}
10+
{{/apis}}
11+
12+
export default apiModule;
13+
{{/apiInfo}}
Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,85 @@
11
{{>licenseInfo}}
2-
/// <reference path="api.d.ts" />
2+
import * as models from '../model/models';
33

44
/* tslint:disable:no-unused-variable member-ordering */
55

66
{{#operations}}
7-
namespace {{package}} {
8-
'use strict';
9-
107
{{#description}}
11-
/**
12-
* {{&description}}
13-
*/
8+
/**
9+
* {{&description}}
10+
*/
1411
{{/description}}
15-
export class {{classname}} {
16-
protected basePath = '{{basePath}}';
17-
public defaultHeaders : any = {};
12+
export class {{classname}} {
13+
protected basePath = '{{basePath}}';
14+
public defaultHeaders : any = {};
1815

19-
static $inject: string[] = ['$http', '$httpParamSerializer', 'basePath'];
16+
static $inject: string[] = ['$http', '$httpParamSerializer', 'basePath'];
2017

21-
constructor(protected $http: ng.IHttpService, protected $httpParamSerializer?: (d: any) => any, basePath?: string) {
22-
if (basePath !== undefined) {
23-
this.basePath = basePath;
24-
}
18+
constructor(protected $http: ng.IHttpService, protected $httpParamSerializer?: (d: any) => any, basePath?: string) {
19+
if (basePath !== undefined) {
20+
this.basePath = basePath;
2521
}
22+
}
2623

2724
{{#operation}}
28-
/**
29-
* {{summary}}
30-
* {{notes}}
31-
{{#allParams}}* @param {{paramName}} {{description}}
32-
{{/allParams}}*/
33-
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any ) : ng.IHttpPromise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
34-
const localVarPath = this.basePath + '{{path}}'{{#pathParams}}
35-
.replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};
25+
/**
26+
* {{summary}}
27+
* {{notes}}
28+
{{#allParams}}* @param {{paramName}} {{description}}
29+
{{/allParams}}*/
30+
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any ) : ng.IHttpPromise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
31+
const localVarPath = this.basePath + '{{path}}'{{#pathParams}}
32+
.replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};
3633

37-
let queryParameters: any = {};
38-
let headerParams: any = (<any>Object).assign({}, this.defaultHeaders);
34+
let queryParameters: any = {};
35+
let headerParams: any = (<any>Object).assign({}, this.defaultHeaders);
3936
{{#hasFormParams}}
40-
let formParams: any = {};
37+
let formParams: any = {};
4138

4239
{{/hasFormParams}}
4340
{{#allParams}}
4441
{{#required}}
45-
// verify required parameter '{{paramName}}' is not null or undefined
46-
if ({{paramName}} === null || {{paramName}} === undefined) {
47-
throw new Error('Required parameter {{paramName}} was null or undefined when calling {{nickname}}.');
48-
}
42+
// verify required parameter '{{paramName}}' is not null or undefined
43+
if ({{paramName}} === null || {{paramName}} === undefined) {
44+
throw new Error('Required parameter {{paramName}} was null or undefined when calling {{nickname}}.');
45+
}
4946
{{/required}}
5047
{{/allParams}}
5148
{{#queryParams}}
52-
if ({{paramName}} !== undefined) {
53-
queryParameters['{{baseName}}'] = {{paramName}};
54-
}
49+
if ({{paramName}} !== undefined) {
50+
queryParameters['{{baseName}}'] = {{paramName}};
51+
}
5552

5653
{{/queryParams}}
5754
{{#headerParams}}
58-
headerParams['{{baseName}}'] = {{paramName}};
55+
headerParams['{{baseName}}'] = {{paramName}};
5956

6057
{{/headerParams}}
6158
{{#hasFormParams}}
62-
headerParams['Content-Type'] = 'application/x-www-form-urlencoded';
59+
headerParams['Content-Type'] = 'application/x-www-form-urlencoded';
6360

6461
{{/hasFormParams}}
6562
{{#formParams}}
66-
formParams['{{baseName}}'] = {{paramName}};
63+
formParams['{{baseName}}'] = {{paramName}};
6764

6865
{{/formParams}}
69-
let httpRequestParams: ng.IRequestConfig = {
70-
method: '{{httpMethod}}',
71-
url: localVarPath,
72-
{{#bodyParam}}data: {{paramName}},
73-
{{/bodyParam}}
74-
{{#hasFormParams}}data: this.$httpParamSerializer(formParams),
75-
{{/hasFormParams}}
76-
params: queryParameters,
77-
headers: headerParams
78-
};
79-
80-
if (extraHttpRequestParams) {
81-
httpRequestParams = (<any>Object).assign(httpRequestParams, extraHttpRequestParams);
82-
}
66+
let httpRequestParams: ng.IRequestConfig = {
67+
method: '{{httpMethod}}',
68+
url: localVarPath,
69+
{{#bodyParam}}data: {{paramName}},
70+
{{/bodyParam}}
71+
{{#hasFormParams}}data: this.$httpParamSerializer(formParams),
72+
{{/hasFormParams}}
73+
params: queryParameters,
74+
headers: headerParams
75+
};
8376

84-
return this.$http(httpRequestParams);
77+
if (extraHttpRequestParams) {
78+
httpRequestParams = (<any>Object).assign(httpRequestParams, extraHttpRequestParams);
8579
}
86-
{{/operation}}
80+
81+
return this.$http(httpRequestParams);
8782
}
83+
{{/operation}}
8884
}
89-
{{/operations}}
85+
{{/operations}}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{#apiInfo}}
2+
{{#apis}}
3+
{{#operations}}
4+
export * from './{{ classname }}';
5+
import { {{ classname }} } from './{{ classname }}';
6+
{{/operations}}
7+
{{/apis}}
8+
export const APIS = [ {{#apis}}{{#operations}}{{ classname }}, {{/operations}}{{/apis}}];
9+
{{/apiInfo}}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './api/api';
2+
export * from './model/models';
Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,38 @@
11
{{>licenseInfo}}
2-
/// <reference path="api.d.ts" />
3-
4-
namespace {{package}} {
5-
'use strict';
2+
import * as models from './models';
63

74
{{#models}}
85
{{#model}}
96
{{#description}}
10-
/**
11-
* {{{description}}}
12-
*/
7+
/**
8+
* {{{description}}}
9+
*/
1310
{{/description}}
14-
export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
11+
export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
1512
{{#vars}}
1613
{{#description}}
17-
/**
18-
* {{{description}}}
19-
*/
14+
/**
15+
* {{{description}}}
16+
*/
2017
{{/description}}
21-
"{{name}}"{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};
22-
18+
"{{name}}"{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};
2319
{{/vars}}
24-
}
20+
}
2521

2622
{{#hasEnums}}
27-
export namespace {{classname}} {
23+
export namespace {{classname}} {
2824
{{#vars}}
2925
{{#isEnum}}
30-
export enum {{enumName}} {
31-
{{#allowableValues}}
32-
{{#enumVars}}
33-
{{{name}}} = <any> {{{value}}}{{^-last}},{{/-last}}
34-
{{/enumVars}}
35-
{{/allowableValues}}
36-
}
26+
export enum {{enumName}} {
27+
{{#allowableValues}}
28+
{{#enumVars}}
29+
{{{name}}} = <any> {{{value}}}{{^-last}},{{/-last}}
30+
{{/enumVars}}
31+
{{/allowableValues}}
32+
}
3733
{{/isEnum}}
3834
{{/vars}}
39-
}
35+
}
4036
{{/hasEnums}}
4137
{{/model}}
4238
{{/models}}
43-
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{#models}}
2+
{{#model}}
3+
export * from './{{{ classname }}}';
4+
{{/model}}
5+
{{/models}}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ public void complexPropertyTest() {
118118

119119
final CodegenProperty property1 = cm.vars.get(0);
120120
Assert.assertEquals(property1.baseName, "children");
121-
Assert.assertEquals(property1.datatype, "Children");
121+
Assert.assertEquals(property1.datatype, "models.Children");
122122
Assert.assertEquals(property1.name, "children");
123123
Assert.assertEquals(property1.defaultValue, "null");
124-
Assert.assertEquals(property1.baseType, "Children");
124+
Assert.assertEquals(property1.baseType, "models.Children");
125125
Assert.assertFalse(property1.required);
126126
Assert.assertTrue(property1.isNotContainer);
127127
}
@@ -142,8 +142,8 @@ public void complexListPropertyTest() {
142142

143143
final CodegenProperty property1 = cm.vars.get(0);
144144
Assert.assertEquals(property1.baseName, "children");
145-
Assert.assertEquals(property1.complexType, "Children");
146-
Assert.assertEquals(property1.datatype, "Array<Children>");
145+
Assert.assertEquals(property1.complexType, "models.Children");
146+
Assert.assertEquals(property1.datatype, "Array<models.Children>");
147147
Assert.assertEquals(property1.name, "children");
148148
Assert.assertEquals(property1.baseType, "Array");
149149
Assert.assertFalse(property1.required);
@@ -177,6 +177,6 @@ public void mapModelTest() {
177177
Assert.assertEquals(cm.description, "a map model");
178178
Assert.assertEquals(cm.vars.size(), 0);
179179
Assert.assertEquals(cm.imports.size(), 1);
180-
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
180+
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("models.Children")).size(), 1);
181181
}
182182
}

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

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)