Skip to content

[TS][Angular] Updating typescript-angular to export api classes #4589

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 2 commits into from
Jan 21, 2017
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 @@ -3,6 +3,8 @@
import java.io.File;

import io.swagger.codegen.SupportingFile;
import io.swagger.codegen.CodegenParameter;
import io.swagger.models.properties.Property;

public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCodegen {

Expand All @@ -19,7 +21,10 @@ public String getHelp() {
@Override
public void processOpts() {
super.processOpts();
supportingFiles.add(new SupportingFile("api.d.mustache", apiPackage().replace('.', File.separatorChar), "api.d.ts"));
supportingFiles.add(new SupportingFile("models.mustache", modelPackage().replace('.', File.separatorChar), "models.ts"));
supportingFiles.add(new SupportingFile("apis.mustache", apiPackage().replace('.', File.separatorChar), "api.ts"));
supportingFiles.add(new SupportingFile("index.mustache", getIndexDirectory(), "index.ts"));
supportingFiles.add(new SupportingFile("api.module.mustache", getIndexDirectory(), "api.module.ts"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));

Expand All @@ -29,9 +34,56 @@ public TypeScriptAngularClientCodegen() {
super();
outputFolder = "generated-code/typescript-angular";
modelTemplateFiles.put("model.mustache", ".ts");
apiTemplateFiles.put("api.mustache", ".ts");
apiTemplateFiles.put("api.mustache", ".ts");
embeddedTemplateDir = templateDir = "typescript-angular";
apiPackage = "API.Client";
modelPackage = "API.Client";
apiPackage = "api";
modelPackage = "model";
}

@Override
public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p);
if(isLanguagePrimitive(swaggerType) || isLanguageGenericType(swaggerType)) {
return swaggerType;
}
return addModelPrefix(swaggerType);
}

@Override
public void postProcessParameter(CodegenParameter parameter) {
super.postProcessParameter(parameter);
parameter.dataType = addModelPrefix(parameter.dataType);
}

private String getIndexDirectory() {
String indexPackage = modelPackage.substring(0, Math.max(0, modelPackage.lastIndexOf('.')));
return indexPackage.replace('.', File.separatorChar);
}

private String addModelPrefix(String swaggerType) {
String type = null;
if (typeMapping.containsKey(swaggerType)) {
type = typeMapping.get(swaggerType);
} else {
type = swaggerType;
}

if (!isLanguagePrimitive(type) && !isLanguageGenericType(type)) {
type = "models." + swaggerType;
}
return type;
}

private boolean isLanguagePrimitive(String type) {
return languageSpecificPrimitives.contains(type);
}

private boolean isLanguageGenericType(String type) {
for (String genericType: languageGenericTypes) {
if (type.startsWith(genericType + "<")) {
return true;
}
}
return false;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as api from './api/api';
import * as angular from 'angular';

{{#apiInfo}}
const apiModule = angular.module('api', [])
{{#apis}}
{{#operations}}
.service('{{classname}}', api.{{classname}})
{{/operations}}
{{/apis}}

export default apiModule;
{{/apiInfo}}
Original file line number Diff line number Diff line change
@@ -1,89 +1,85 @@
{{>licenseInfo}}
/// <reference path="api.d.ts" />
import * as models from '../model/models';

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

{{#operations}}
namespace {{package}} {
'use strict';

{{#description}}
/**
* {{&description}}
*/
/**
* {{&description}}
*/
{{/description}}
export class {{classname}} {
protected basePath = '{{basePath}}';
public defaultHeaders : any = {};
export class {{classname}} {
protected basePath = '{{basePath}}';
public defaultHeaders : any = {};

static $inject: string[] = ['$http', '$httpParamSerializer', 'basePath'];
static $inject: string[] = ['$http', '$httpParamSerializer', 'basePath'];

constructor(protected $http: ng.IHttpService, protected $httpParamSerializer?: (d: any) => any, basePath?: string) {
if (basePath !== undefined) {
this.basePath = basePath;
}
constructor(protected $http: ng.IHttpService, protected $httpParamSerializer?: (d: any) => any, basePath?: string) {
if (basePath !== undefined) {
this.basePath = basePath;
}
}

{{#operation}}
/**
* {{summary}}
* {{notes}}
{{#allParams}}* @param {{paramName}} {{description}}
{{/allParams}}*/
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any ) : ng.IHttpPromise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
const localVarPath = this.basePath + '{{path}}'{{#pathParams}}
.replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};
/**
* {{summary}}
* {{notes}}
{{#allParams}}* @param {{paramName}} {{description}}
{{/allParams}}*/
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any ) : ng.IHttpPromise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
const localVarPath = this.basePath + '{{path}}'{{#pathParams}}
.replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};

let queryParameters: any = {};
let headerParams: any = (<any>Object).assign({}, this.defaultHeaders);
let queryParameters: any = {};
let headerParams: any = (<any>Object).assign({}, this.defaultHeaders);
{{#hasFormParams}}
let formParams: any = {};
let formParams: any = {};

{{/hasFormParams}}
{{#allParams}}
{{#required}}
// verify required parameter '{{paramName}}' is not null or undefined
if ({{paramName}} === null || {{paramName}} === undefined) {
throw new Error('Required parameter {{paramName}} was null or undefined when calling {{nickname}}.');
}
// verify required parameter '{{paramName}}' is not null or undefined
if ({{paramName}} === null || {{paramName}} === undefined) {
throw new Error('Required parameter {{paramName}} was null or undefined when calling {{nickname}}.');
}
{{/required}}
{{/allParams}}
{{#queryParams}}
if ({{paramName}} !== undefined) {
queryParameters['{{baseName}}'] = {{paramName}};
}
if ({{paramName}} !== undefined) {
queryParameters['{{baseName}}'] = {{paramName}};
}

{{/queryParams}}
{{#headerParams}}
headerParams['{{baseName}}'] = {{paramName}};
headerParams['{{baseName}}'] = {{paramName}};

{{/headerParams}}
{{#hasFormParams}}
headerParams['Content-Type'] = 'application/x-www-form-urlencoded';
headerParams['Content-Type'] = 'application/x-www-form-urlencoded';

{{/hasFormParams}}
{{#formParams}}
formParams['{{baseName}}'] = {{paramName}};
formParams['{{baseName}}'] = {{paramName}};

{{/formParams}}
let httpRequestParams: ng.IRequestConfig = {
method: '{{httpMethod}}',
url: localVarPath,
{{#bodyParam}}data: {{paramName}},
{{/bodyParam}}
{{#hasFormParams}}data: this.$httpParamSerializer(formParams),
{{/hasFormParams}}
params: queryParameters,
headers: headerParams
};

if (extraHttpRequestParams) {
httpRequestParams = (<any>Object).assign(httpRequestParams, extraHttpRequestParams);
}
let httpRequestParams: ng.IRequestConfig = {
method: '{{httpMethod}}',
url: localVarPath,
{{#bodyParam}}data: {{paramName}},
{{/bodyParam}}
{{#hasFormParams}}data: this.$httpParamSerializer(formParams),
{{/hasFormParams}}
params: queryParameters,
headers: headerParams
};

return this.$http(httpRequestParams);
if (extraHttpRequestParams) {
httpRequestParams = (<any>Object).assign(httpRequestParams, extraHttpRequestParams);
}
{{/operation}}

return this.$http(httpRequestParams);
}
{{/operation}}
}
{{/operations}}
{{/operations}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{#apiInfo}}
{{#apis}}
{{#operations}}
export * from './{{ classname }}';
import { {{ classname }} } from './{{ classname }}';
{{/operations}}
{{/apis}}
export const APIS = [ {{#apis}}{{#operations}}{{ classname }}, {{/operations}}{{/apis}}];
{{/apiInfo}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './api/api';
export * from './model/models';
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
{{>licenseInfo}}
/// <reference path="api.d.ts" />

namespace {{package}} {
'use strict';
import * as models from './models';

{{#models}}
{{#model}}
{{#description}}
/**
* {{{description}}}
*/
/**
* {{{description}}}
*/
{{/description}}
export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
{{#vars}}
{{#description}}
/**
* {{{description}}}
*/
/**
* {{{description}}}
*/
{{/description}}
"{{name}}"{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};

"{{name}}"{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};
{{/vars}}
}
}

{{#hasEnums}}
export namespace {{classname}} {
export namespace {{classname}} {
{{#vars}}
{{#isEnum}}
export enum {{enumName}} {
{{#allowableValues}}
{{#enumVars}}
{{{name}}} = <any> {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
}
export enum {{enumName}} {
{{#allowableValues}}
{{#enumVars}}
{{{name}}} = <any> {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
}
{{/isEnum}}
{{/vars}}
}
}
{{/hasEnums}}
{{/model}}
{{/models}}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{#models}}
{{#model}}
export * from './{{{ classname }}}';
{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ public void complexPropertyTest() {

final CodegenProperty property1 = cm.vars.get(0);
Assert.assertEquals(property1.baseName, "children");
Assert.assertEquals(property1.datatype, "Children");
Assert.assertEquals(property1.datatype, "models.Children");
Assert.assertEquals(property1.name, "children");
Assert.assertEquals(property1.defaultValue, "null");
Assert.assertEquals(property1.baseType, "Children");
Assert.assertEquals(property1.baseType, "models.Children");
Assert.assertFalse(property1.required);
Assert.assertTrue(property1.isNotContainer);
}
Expand All @@ -142,8 +142,8 @@ public void complexListPropertyTest() {

final CodegenProperty property1 = cm.vars.get(0);
Assert.assertEquals(property1.baseName, "children");
Assert.assertEquals(property1.complexType, "Children");
Assert.assertEquals(property1.datatype, "Array<Children>");
Assert.assertEquals(property1.complexType, "models.Children");
Assert.assertEquals(property1.datatype, "Array<models.Children>");
Assert.assertEquals(property1.name, "children");
Assert.assertEquals(property1.baseType, "Array");
Assert.assertFalse(property1.required);
Expand Down Expand Up @@ -177,6 +177,6 @@ public void mapModelTest() {
Assert.assertEquals(cm.description, "a map model");
Assert.assertEquals(cm.vars.size(), 0);
Assert.assertEquals(cm.imports.size(), 1);
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("models.Children")).size(), 1);
}
}

This file was deleted.

Loading