From b7810bd0df73cd23a5928d8592d592f8b4647468 Mon Sep 17 00:00:00 2001 From: narkaTee Date: Wed, 14 Nov 2018 22:59:12 +0100 Subject: [PATCH 1/2] Add rxjs6 support --- .../typescript/TypeScriptAngularClientCodegen.java | 1 + .../handlebars/typescript-angular/api.service.mustache | 5 +++++ .../handlebars/typescript-angular/apiInterface.mustache | 5 +++++ .../handlebars/typescript-angular/package.mustache | 6 +++--- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/swagger/codegen/v3/generators/typescript/TypeScriptAngularClientCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/typescript/TypeScriptAngularClientCodegen.java index 6758630c2f..bccb9c5970 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/typescript/TypeScriptAngularClientCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/typescript/TypeScriptAngularClientCodegen.java @@ -127,6 +127,7 @@ public void processOpts() { additionalProperties.put("injectionToken", ngVersion.atLeast("4.0.0") ? "InjectionToken" : "OpaqueToken"); additionalProperties.put("injectionTokenTyped", ngVersion.atLeast("4.0.0")); additionalProperties.put("useHttpClient", ngVersion.atLeast("4.3.0")); + additionalProperties.put("useRxJS6", ngVersion.atLeast("6.0.0")); if (!ngVersion.atLeast("4.3.0")) { supportingFiles.add(new SupportingFile("rxjs-operators.mustache", getIndexDirectory(), "rxjs-operators.ts")); } diff --git a/src/main/resources/handlebars/typescript-angular/api.service.mustache b/src/main/resources/handlebars/typescript-angular/api.service.mustache index 0a600b403b..1f0d217a38 100644 --- a/src/main/resources/handlebars/typescript-angular/api.service.mustache +++ b/src/main/resources/handlebars/typescript-angular/api.service.mustache @@ -15,7 +15,12 @@ import { Response, ResponseContentType } from '@angular/http import { CustomQueryEncoderHelper } from '../encoder'; {{/useHttpClient}} +{{^useRxJS6}} import { Observable } from 'rxjs/Observable'; +{{/useRxJS6}} +{{#useRxJS6}} +import { Observable } from 'rxjs'; +{{/useRxJS6}} {{^useHttpClient}} import '../rxjs-operators'; {{/useHttpClient}} diff --git a/src/main/resources/handlebars/typescript-angular/apiInterface.mustache b/src/main/resources/handlebars/typescript-angular/apiInterface.mustache index 89956f5917..c15d4c2f9f 100644 --- a/src/main/resources/handlebars/typescript-angular/apiInterface.mustache +++ b/src/main/resources/handlebars/typescript-angular/apiInterface.mustache @@ -7,7 +7,12 @@ import { HttpHeaders } from '@angular/comm import { Headers } from '@angular/http'; {{/useHttpClient}} +{{^useRxJS6}} import { Observable } from 'rxjs/Observable'; +{{/useRxJS6}} +{{#useRxJS6}} +import { Observable } from 'rxjs'; +{{/useRxJS6}} {{#imports}} diff --git a/src/main/resources/handlebars/typescript-angular/package.mustache b/src/main/resources/handlebars/typescript-angular/package.mustache index afc69fb714..cf608ba4a8 100644 --- a/src/main/resources/handlebars/typescript-angular/package.mustache +++ b/src/main/resources/handlebars/typescript-angular/package.mustache @@ -14,7 +14,7 @@ {{/useNgPackagr}} {{^useNgPackagr}} "main": "dist/index.js", - "module": "dist/index.js", + "module": "dist/index.js", "typings": "dist/index.d.ts", "scripts": { "build": "ngc", @@ -28,7 +28,7 @@ "@angular/compiler": "^{{ngVersion}}", "core-js": "^2.4.0", "reflect-metadata": "^0.1.3", - "rxjs": "^5.4.0", + "rxjs": "{{#useRxJS6}}^6.1.0{{/useRxJS6}}{{^useRxJS6}}^5.4.0{{/useRxJS6}}", "zone.js": "^0.7.6" }, "devDependencies": { @@ -40,7 +40,7 @@ "@angular/platform-browser": "^{{ngVersion}}",{{#useNgPackagr}} "ng-packagr": {{#useOldNgPackagr}}"^1.6.0"{{/useOldNgPackagr}}{{^useOldNgPackagr}}"^2.4.1"{{/useOldNgPackagr}},{{/useNgPackagr}} "reflect-metadata": "^0.1.3", - "rxjs": "^5.4.0", + "rxjs": "{{#useRxJS6}}^6.1.0{{/useRxJS6}}{{^useRxJS6}}^5.4.0{{/useRxJS6}}", "zone.js": "^0.7.6", "typescript": "^2.1.5" }{{#npmRepository}},{{/npmRepository}} From fd0ebbff66aa4e4322e98a28a9ff3b7ec08a3108 Mon Sep 17 00:00:00 2001 From: narkaTee Date: Wed, 14 Nov 2018 23:02:07 +0100 Subject: [PATCH 2/2] Add providedInRoot support --- .../typescript/TypeScriptAngularClientCodegen.java | 7 +++++++ .../handlebars/typescript-angular/api.service.mustache | 7 +++++++ .../options/TypeScriptAngularClientOptionsProvider.java | 2 ++ 3 files changed, 16 insertions(+) diff --git a/src/main/java/io/swagger/codegen/v3/generators/typescript/TypeScriptAngularClientCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/typescript/TypeScriptAngularClientCodegen.java index bccb9c5970..423e7d41dd 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/typescript/TypeScriptAngularClientCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/typescript/TypeScriptAngularClientCodegen.java @@ -38,6 +38,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode public static final String SNAPSHOT = "snapshot"; public static final String WITH_INTERFACES = "withInterfaces"; public static final String NG_VERSION = "ngVersion"; + public static final String PROVIDED_IN_ROOT ="providedInRoot"; protected String npmName = null; protected String npmVersion = "1.0.0"; @@ -52,6 +53,7 @@ public TypeScriptAngularClientCodegen() { this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json")); this.cliOptions.add(new CliOption(SNAPSHOT, "When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString())); this.cliOptions.add(new CliOption(WITH_INTERFACES, "Setting this property to true will generate interfaces next to the default class implementations.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString())); + this.cliOptions.add(new CliOption(PROVIDED_IN_ROOT, "Use this property to provide Injectables in root (it is only valid in angular version greater or equal to 6.0.0).", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString())); this.cliOptions.add(new CliOption(NG_VERSION, "The version of Angular. Default is '4.3'")); } @@ -123,6 +125,11 @@ public void processOpts() { LOGGER.info("generating code for Angular {} ...", ngVersion); LOGGER.info(" (you can select the angular version by setting the additionalProperty ngVersion)"); } + + if (additionalProperties.containsKey(PROVIDED_IN_ROOT) && !ngVersion.atLeast("6.0.0")) { + additionalProperties.put(PROVIDED_IN_ROOT,false); + } + additionalProperties.put(NG_VERSION, ngVersion); additionalProperties.put("injectionToken", ngVersion.atLeast("4.0.0") ? "InjectionToken" : "OpaqueToken"); additionalProperties.put("injectionTokenTyped", ngVersion.atLeast("4.0.0")); diff --git a/src/main/resources/handlebars/typescript-angular/api.service.mustache b/src/main/resources/handlebars/typescript-angular/api.service.mustache index 1f0d217a38..e2dd0a1c8d 100644 --- a/src/main/resources/handlebars/typescript-angular/api.service.mustache +++ b/src/main/resources/handlebars/typescript-angular/api.service.mustache @@ -42,7 +42,14 @@ import { {{classname}}Interface } from './{{classFile * {{&description}} */ {{/description}} +{{^providedInRoot}} @Injectable() +{{/providedInRoot}} +{{#providedInRoot}} +@Injectable({ + providedIn: 'root', +}) +{{/providedInRoot}} {{#withInterfaces}} export class {{classname}} implements {{classname}}Interface { {{/withInterfaces}} diff --git a/src/test/java/io/swagger/codegen/v3/generators/options/TypeScriptAngularClientOptionsProvider.java b/src/test/java/io/swagger/codegen/v3/generators/options/TypeScriptAngularClientOptionsProvider.java index 50bf189aca..ba3e89f8ae 100644 --- a/src/test/java/io/swagger/codegen/v3/generators/options/TypeScriptAngularClientOptionsProvider.java +++ b/src/test/java/io/swagger/codegen/v3/generators/options/TypeScriptAngularClientOptionsProvider.java @@ -15,6 +15,7 @@ public class TypeScriptAngularClientOptionsProvider implements OptionsProvider { private static final String NMP_VERSION = "1.1.2"; private static final String NPM_REPOSITORY = "https://registry.npmjs.org"; public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false"; + private static final String PROVIDED_IN_ROOT = "true"; public static final String NG_VERSION = "2"; @@ -35,6 +36,7 @@ public Map createOptions() { .put(TypeScriptAngularClientCodegen.SNAPSHOT, Boolean.FALSE.toString()) .put(TypeScriptAngularClientCodegen.WITH_INTERFACES, Boolean.FALSE.toString()) .put(TypeScriptAngularClientCodegen.NPM_REPOSITORY, NPM_REPOSITORY) + .put(TypeScriptAngularClientCodegen.PROVIDED_IN_ROOT, PROVIDED_IN_ROOT) .put(TypeScriptAngularClientCodegen.NG_VERSION, NG_VERSION) .put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE) .build();