Skip to content

Commit

Permalink
Added support for string enum parameters and inline model enums. Note…
Browse files Browse the repository at this point in the history
… that TypeScript 2.4 is required to support string initializers in generated enums.
  • Loading branch information
lotjomik committed Jan 26, 2018
1 parent 3cf1923 commit ad82a23
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ node_modules
.vscode
.idea
bin
lib
obj
out
output
temp
lib
npm-debug.log
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "angular4-swagger-client-generator",
"version": "0.1.12",
"description": "Angular4 REST API client generator from Swagger JSON with camel case settigs",
"version": "0.1.13",
"description": "Angular 4/5 REST API client generator from Swagger JSON with camel case settings",
"homepage": "https://github.com/lotjomik/angular4-swagger-client-generator",
"main": "index.js",
"bin": {
Expand Down Expand Up @@ -44,6 +44,6 @@
"optimist": "^0.6.1",
"request": "^2.81.0",
"tslint": "^5.7.0",
"typescript": "^2.1.5"
"typescript": "^2.4.0"
}
}
5 changes: 2 additions & 3 deletions src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,15 @@ var Generator = (function () {
typescriptType: null
};

if (property.isArray)
if (property.isArray) {
if (_.has(propin.items, '$ref')) {
property.type = that.camelCase(propin.items['$ref'].replace('#/definitions/', ''));
} else if (_.has(propin.items, 'type')) {
property.type = that.camelCase(propin.items['type']);
} else {
property.type = propin.type;
}

}
else {
property.type = _.has(propin, '$ref') ? that.camelCase(propin['$ref'].replace('#/definitions/', '')) : propin.type;
}
Expand All @@ -306,7 +306,6 @@ var Generator = (function () {
property.typescriptType = property.type;
}


if (property.isRef) {
definition.refs.push(property);

Expand Down
8 changes: 8 additions & 0 deletions templates/angular2-model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
import { {{.}} } from './{{#lower}}{{.}}{{/lower}}.model';
{{/imports}}

{{#enums}}
export enum {{name}} {
{{#values}}
{{#upper}}{{value}}{{/upper}} = '{{value}}'{{^isLast}},{{/isLast}}
{{/values}}
}

{{/enums}}
export interface {{&name}} {
{{#properties}}
{{#isArray}}
Expand Down
16 changes: 12 additions & 4 deletions templates/angular2-service.mustache
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { Inject, Injectable, Optional } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { Observable } from 'rxjs';

import {
{{#definitions}} {{name}}{{^last}},
{{/last}}{{/definitions}}
{{#definitions}} {{name}}{{^isLast}},
{{/isLast}}{{/definitions}}

} from './models';

{{#enums}}
export enum {{name}} {
{{#values}}
{{#upper}}{{value}}{{/upper}} = '{{value}}'{{^isLast}},{{/isLast}}
{{/values}}
}

{{/enums}}
/**
* Created with angular4-swagger-client-generator.
*/
Expand All @@ -33,7 +41,7 @@ export class ApiClientService {
{{/parameters}}
* @return Full HTTP response as Observable
*/
public {{&methodName}}({{#parameters}}{{&camelCaseName}}: {{typescriptType}}{{^last}}, {{/last}}{{/parameters}}): Observable<HttpResponse<{{&response}}>> {
public {{&methodName}}({{#parameters}}{{&camelCaseName}}: {{typescriptType}}{{^isLast}}, {{/isLast}}{{/parameters}}): Observable<HttpResponse<{{&response}}>> {
let uri = `{{&backTickPath}}`;
let headers = new HttpHeaders();
let params = new HttpParams();
Expand Down

1 comment on commit ad82a23

@locosmac
Copy link

Choose a reason for hiding this comment

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

This commit results in invalid output, if the same enum is used in multiple data types. As an example, try generating the api for https://www.maptology.com/api/maptology.json. The OS enum will be generated for Device and DeviceHandle and the result will not compile due to a duplicate definition of OS. I think it is necessary to either "merge" the enums (which seems complicated) or to give them a local name, e.g. "DeviceHandle_OS" or something.

Please sign in to comment.