Skip to content

[Typescript] Support InversifyJS #6489

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 28 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
709c95a
Add config option to enable InversifyJS
bodograumann May 26, 2020
0ae13a7
Add pascal case lambda for mustache
bodograumann May 26, 2020
e3936fc
Generate a class for each auth method
bodograumann May 26, 2020
7df9511
Add service identifiers and service binder helper
bodograumann May 28, 2020
5822900
Split Configuration into interface and factory
bodograumann May 29, 2020
22e2545
Define minimal interface for ServerConfiguration
bodograumann May 29, 2020
e199ca8
Add annotations for inversify when enabled
bodograumann May 29, 2020
0b711b0
Always expose list of server configurations
bodograumann Jun 8, 2020
7c8caf6
Add samples and defalt tests for useInversify
bodograumann Jun 10, 2020
63313bd
Simplify sample generation script
bodograumann Jun 10, 2020
d744995
Fix: Add object_params arg description to help
bodograumann Jun 10, 2020
8568973
Fix: Properly enable inversify with bool property
bodograumann Jun 10, 2020
8919300
Build tests in pom instead of prepublish
bodograumann Jun 10, 2020
2f42dcf
Update dependencies for inversify tests
bodograumann Jun 10, 2020
7748c3c
Test basic api service resolution
bodograumann Jun 10, 2020
8bcabad
Remove Promise and Observable prefix from exports
bodograumann Jun 10, 2020
94cab72
Fix, RxJS: Import Observable in object params api
bodograumann Jun 10, 2020
76ea485
Add ioc service identifier for object param api
bodograumann Jun 10, 2020
abe3aa7
Add hint about unimpeded development
bodograumann Jun 10, 2020
32041a0
Simplify api service binder syntax
bodograumann Jun 11, 2020
e630e4c
Remove default tests for inversify
bodograumann Jun 11, 2020
07b93ec
Add wrapper for easy promise based http libraries
bodograumann Jun 11, 2020
e882b25
Add named injects for remaining auth parameters
bodograumann Jun 11, 2020
81660b7
Directly inject promise services without RxJS
bodograumann Jun 11, 2020
2fcc832
Add tests for api service binder
bodograumann Jun 11, 2020
fbe9705
Add convenience method to bind all api services
bodograumann Jun 11, 2020
f10bab1
Fix: Rename inversify test artifact
bodograumann Jun 12, 2020
41f5e3e
Run bin/utils/copy-to-website.sh
bodograumann Jun 12, 2020
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cache:
- $HOME/samples/openapi3/client/petstore/typescript/tests/default/node_modules
- $HOME/samples/openapi3/client/petstore/typescript/tests/jquery/node_modules
- $HOME/samples/openapi3/client/petstore/typescript/tests/object_params/node_modules
- $HOME/samples/openapi3/client/petstore/typescript/tests/inversify/node_modules
- $HOME/samples/client/petstore/typescript-node/npm/node_modules
- $HOME/samples/client/petstore/typescript-node/npm/typings/
- $HOME/samples/client/petstore/typescript-fetch/tests/default/node_modules
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ To add test cases (optional) covering the change in the code generator, please r

To test the templates, please perform the following:
- Update the Petstore sample by running the shell scripts under `bin` and `bin/openapi3` folder. For example, run `./bin/python-petstore.sh` and `./bin/openapi3/python-petstore.sh` to update the Python PetStore API client under [`samples/client/petstore/python`](https://github.com/openapitools/openapi-generator/tree/master/samples/client/petstore/python) and [`samples/openapi3/client/petstore/python`](https://github.com/openapitools/openapi-generator/tree/master/samples/openapi3/client/petstore/python). For Windows, the batch files can be found under `bin\windows` folder. (If you find that there are new files generated or unexpected changes as a result of the update, that's not unusual as the test cases are added to the OpenAPI spec from time to time. If you've questions or concerns, please open a ticket to start a discussion)
- During development it can be helpful to quickly regenerate the samples without recompiling all of openapi-generator, e.g. when you have only updated the mustache templates. This can be done by passing the `-t` parameter: `./bin/python-petstore.sh -t modules/openapi-generator/src/main/resources/python`.
- Run the tests in the sample folder using maven `mvn integration-test -rf :<artifactId>`, e.g. open a shell in `samples/client/petstore/python`, run `mvn integration-test -rf :PythonPetstoreClientTests`. The artifactId of the project can be found in the pom.xml file. (some languages may not contain unit testing for Petstore and we're looking for contribution from the community to implement those tests)
- Finally, git commit the updated samples files: `git commit -a`
(`git add -A` if added files with new test cases)
Expand Down
23 changes: 14 additions & 9 deletions bin/typescript.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ if [ ! -d "${APP_DIR}" ]; then
fi

executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"
common_args="generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g typescript"
samples="samples/openapi3/client/petstore/typescript/builds"

if [ ! -f "$executable" ]
then
Expand All @@ -27,16 +29,19 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
echo "Creating default (fetch) client!"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g typescript -o samples/openapi3/client/petstore/typescript/builds/default --additional-properties=platform=node,npmName=ts-petstore-client $@"

java $JAVA_OPTS -jar $executable $ags
echo "Creating jquery client!"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g typescript -o samples/openapi3/client/petstore/typescript/builds/jquery --additional-properties=framework=jquery,npmName=ts-petstore-client $@"
printf "\033[32m## Creating default (fetch) client!\033[0m\n"
args="-o $samples/default --additional-properties=platform=node,npmName=ts-petstore-client $@"
java $JAVA_OPTS -jar $executable $common_args $args

java $JAVA_OPTS -jar $executable $ags
printf "\033[32m## Creating jquery client!\033[0m\n"
args="-o $samples/jquery --additional-properties=framework=jquery,npmName=ts-petstore-client $@"
java $JAVA_OPTS -jar $executable $common_args $args

echo "Creating fetch object client!"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g typescript -o samples/openapi3/client/petstore/typescript/builds/object_params --additional-properties=platform=node,npmName=ts-petstore-client,useObjectParameters $@"
printf "\033[32m## Creating fetch object client!\033[0m\n"
args="-o $samples/object_params --additional-properties=platform=node,npmName=ts-petstore-client,useObjectParameters=true $@"
java $JAVA_OPTS -jar $executable $common_args $args

java $JAVA_OPTS -jar $executable $ags
printf "\033[32m## Creating fetch client with InversifyJS support!\033[0m\n"
args="-o $samples/inversify --additional-properties=platform=node,npmName=ts-petstore-client,useInversify=true $@"
java $JAVA_OPTS -jar $executable $common_args $args
15 changes: 9 additions & 6 deletions bin/windows/typescript.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ If Not Exist %executable% (

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M

set ags=generate -i modules\openapi-generator\src\test\resources\3_0\petstore.yaml -g typescript -o samples\openapi3\client\petstore\typescript\builds\default --additional-properties=platform=node,npmName=ts-petstore-client
java %JAVA_OPTS% -jar %executable% %ags%
set args=generate -i modules\openapi-generator\src\test\resources\3_0\petstore.yaml -g typescript -o samples\openapi3\client\petstore\typescript\builds\default --additional-properties=platform=node,npmName=ts-petstore-client
java %JAVA_OPTS% -jar %executable% %args%

ags=generate -i modules\openapi-generator\src\test\resources\3_0\petstore.yaml -g typescript -o samples\openapi3\client\petstore\typescript\builds\jquery --additional-properties=framework=jquery,npmName=ts-petstore-client
java %JAVA_OPTS% -jar %executable% %ags%
args=generate -i modules\openapi-generator\src\test\resources\3_0\petstore.yaml -g typescript -o samples\openapi3\client\petstore\typescript\builds\jquery --additional-properties=framework=jquery,npmName=ts-petstore-client
java %JAVA_OPTS% -jar %executable% %args%

set ags=generate -i modules\openapi-generator\src\test\resources\3_0\petstore.yaml -g typescript -o samples\openapi3\client\petstore\typescript\builds\object_params --additional-properties=platform=node,npmName=ts-petstore-client,useObjectParameters
java %JAVA_OPTS% -jar %executable% %ags%
set args=generate -i modules\openapi-generator\src\test\resources\3_0\petstore.yaml -g typescript -o samples\openapi3\client\petstore\typescript\builds\object_params --additional-properties=platform=node,npmName=ts-petstore-client,useObjectParameters
java %JAVA_OPTS% -jar %executable% %args%

set args=generate -i modules\openapi-generator\src\test\resources\3_0\petstore.yaml -g typescript -o samples\openapi3\client\petstore\typescript\builds\inversify --additional-properties=platform=node,npmName=ts-petstore-client,useInversify
java %JAVA_OPTS% -jar %executable% %args%
1 change: 1 addition & 0 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ To add test cases (optional) covering the change in the code generator, please r

To test the templates, please perform the following:
- Update the Petstore sample by running the shell scripts under `bin` and `bin/openapi3` folder. For example, run `./bin/python-petstore.sh` and `./bin/openapi3/python-petstore.sh` to update the Python PetStore API client under [`samples/client/petstore/python`](https://github.com/openapitools/openapi-generator/tree/master/samples/client/petstore/python) and [`samples/openapi3/client/petstore/python`](https://github.com/openapitools/openapi-generator/tree/master/samples/openapi3/client/petstore/python). For Windows, the batch files can be found under `bin\windows` folder. (If you find that there are new files generated or unexpected changes as a result of the update, that's not unusual as the test cases are added to the OpenAPI spec from time to time. If you've questions or concerns, please open a ticket to start a discussion)
- During development it can be helpful to quickly regenerate the samples without recompiling all of openapi-generator, e.g. when you have only updated the mustache templates. This can be done by passing the `-t` parameter: `./bin/python-petstore.sh -t modules/openapi-generator/src/main/resources/python`.
- Run the tests in the sample folder using maven `mvn integration-test -rf :<artifactId>`, e.g. open a shell in `samples/client/petstore/python`, run `mvn integration-test -rf :PythonPetstoreClientTests`. The artifactId of the project can be found in the pom.xml file. (some languages may not contain unit testing for Petstore and we're looking for contribution from the community to implement those tests)
- Finally, git commit the updated samples files: `git commit -a`
(`git add -A` if added files with new test cases)
Expand Down
3 changes: 2 additions & 1 deletion docs/generators/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ If false: the 'additionalProperties' implementation is compliant with the OAS an
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|supportsES6|Generate code that conforms to ES6.| |false|
|useObjectParameters|useObjectParameters| |false|
|useInversify|Enable this to generate decorators and service identifiers for the InversifyJS inversion of control container.| |false|
|useObjectParameters|Use aggregate parameter objects as function arguments for api operations instead of passing each parameter as a separate function argument.| |false|
|useRxJS|Enable this to internally use rxjs observables. If disabled, a stub is used instead. This is required for the 'angular' framework.| |false|

## IMPORT MAPPING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
.put("lowercase", new LowercaseLambda().generator(this))
.put("uppercase", new UppercaseLambda())
.put("titlecase", new TitlecaseLambda())
.put("camelcase", new CamelCaseLambda().generator(this))
.put("camelcase", new CamelCaseLambda(true).generator(this))
.put("pascalcase", new CamelCaseLambda(false).generator(this))
.put("indented", new IndentedLambda())
.put("indented_8", new IndentedLambda(8, " "))
.put("indented_12", new IndentedLambda(12, " "))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class TypeScriptClientCodegen extends DefaultCodegen implements CodegenCo
private static final String FILE_CONTENT_DATA_TYPE_DESC = "Specifies the type to use for the content of a file - i.e. Blob (Browser) / Buffer (node)";
private static final String USE_RXJS_SWITCH = "useRxJS";
private static final String USE_RXJS_SWITCH_DESC = "Enable this to internally use rxjs observables. If disabled, a stub is used instead. This is required for the 'angular' framework.";
private static final String USE_INVERSIFY_SWITCH = "useInversify";
private static final String USE_INVERSIFY_SWITCH_DESC = "Enable this to generate decorators and service identifiers for the InversifyJS inversion of control container.";

private static final String USE_OBJECT_PARAMS_SWITCH = "useObjectParameters";
private static final String USE_OBJECT_PARAMS_DESC = "Use aggregate parameter objects as function arguments for api operations instead of passing each parameter as a separate function argument.";
Expand Down Expand Up @@ -167,7 +169,8 @@ public TypeScriptClientCodegen() {
cliOptions.add(new CliOption(CodegenConstants.SUPPORTS_ES6, CodegenConstants.SUPPORTS_ES6_DESC).defaultValue("false"));
cliOptions.add(new CliOption(TypeScriptClientCodegen.FILE_CONTENT_DATA_TYPE, TypeScriptClientCodegen.FILE_CONTENT_DATA_TYPE_DESC).defaultValue("Buffer"));
cliOptions.add(new CliOption(TypeScriptClientCodegen.USE_RXJS_SWITCH, TypeScriptClientCodegen.USE_RXJS_SWITCH_DESC).defaultValue("false"));
cliOptions.add(new CliOption(TypeScriptClientCodegen.USE_OBJECT_PARAMS_SWITCH, TypeScriptClientCodegen.USE_OBJECT_PARAMS_SWITCH).defaultValue("false"));
cliOptions.add(new CliOption(TypeScriptClientCodegen.USE_OBJECT_PARAMS_SWITCH, TypeScriptClientCodegen.USE_OBJECT_PARAMS_DESC).defaultValue("false"));
cliOptions.add(new CliOption(TypeScriptClientCodegen.USE_INVERSIFY_SWITCH, TypeScriptClientCodegen.USE_INVERSIFY_SWITCH_DESC).defaultValue("false"));

CliOption frameworkOption = new CliOption(TypeScriptClientCodegen.FRAMEWORK_SWITCH, TypeScriptClientCodegen.FRAMEWORK_SWITCH_DESC);
for (String option: TypeScriptClientCodegen.FRAMEWORKS) {
Expand Down Expand Up @@ -208,15 +211,15 @@ public TypeScriptClientCodegen() {
supportingFiles.add(new SupportingFile("types" + File.separator + "ObjectParamAPI.mustache", "types", "ObjectParamAPI.ts"));

// models
this.setModelPackage("");
setModelPackage("");
supportingFiles.add(new SupportingFile("model" + File.separator + "ObjectSerializer.mustache", "models", "ObjectSerializer.ts"));
modelTemplateFiles.put("model" + File.separator + "model.mustache", ".ts");

// api
this.setApiPackage("");
setApiPackage("");
supportingFiles.add(new SupportingFile("api" + File.separator + "middleware.mustache", "", "middleware.ts"));
this.supportingFiles.add(new SupportingFile("api" + File.separator + "baseapi.mustache", "apis", "baseapi.ts"));
this.apiTemplateFiles.put("api" + File.separator + "api.mustache", ".ts");
supportingFiles.add(new SupportingFile("api" + File.separator + "baseapi.mustache", "apis", "baseapi.ts"));
apiTemplateFiles.put("api" + File.separator + "api.mustache", ".ts");
}

public String getNpmName() {
Expand Down Expand Up @@ -791,7 +794,18 @@ public void processOpts() {

final boolean useRxJS = convertPropertyToBooleanAndWriteBack(USE_RXJS_SWITCH);
if (!useRxJS) {
supportingFiles.add(new SupportingFile("rxjsStub.mustache", "rxjsStub.ts"));
supportingFiles.add(new SupportingFile("rxjsStub.mustache", "rxjsStub.ts"));
}

final boolean useInversify = convertPropertyToBooleanAndWriteBack(USE_INVERSIFY_SWITCH);
if (useInversify) {
supportingFiles.add(new SupportingFile("services" + File.separator + "index.mustache", "services", "index.ts"));
supportingFiles.add(new SupportingFile("services" + File.separator + "configuration.mustache", "services", "configuration.ts"));
supportingFiles.add(new SupportingFile("services" + File.separator + "PromiseAPI.mustache", "services", "PromiseAPI.ts"));
supportingFiles.add(new SupportingFile("services" + File.separator + "ObservableAPI.mustache", "services", "ObservableAPI.ts"));
supportingFiles.add(new SupportingFile("services" + File.separator + "ObjectParamAPI.mustache", "services", "ObjectParamAPI.ts"));
supportingFiles.add(new SupportingFile("services" + File.separator + "http.mustache", "services", "http.ts"));
apiTemplateFiles.put("services" + File.separator + "api.mustache", ".service.ts");
}

// NPM Settings
Expand All @@ -806,10 +820,6 @@ public void processOpts() {
if (additionalProperties.containsKey(NPM_REPOSITORY)) {
setNpmRepository(additionalProperties.get(NPM_REPOSITORY).toString());
}




}

private String getHttpLibForFramework(String object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@
public class CamelCaseLambda implements Mustache.Lambda {
private CodegenConfig generator = null;
private Boolean escapeParam = false;
private Boolean lowercaseFirstLetter = true;

public CamelCaseLambda() {

public CamelCaseLambda(boolean lowercaseFirstLetter) {
this.lowercaseFirstLetter = lowercaseFirstLetter;
}

public CamelCaseLambda() {}

public CamelCaseLambda generator(final CodegenConfig generator) {
this.generator = generator;
return this;
Expand All @@ -59,7 +62,7 @@ public CamelCaseLambda escapeAsParamName(final Boolean escape) {

@Override
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
String text = camelize(fragment.execute(), true);
String text = camelize(fragment.execute(), lowercaseFirstLetter);
if (generator != null) {
text = generator.sanitizeName(text);
if (generator.reservedWords().contains(text)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import * as FormData from "form-data";
import {ObjectSerializer} from '../models/ObjectSerializer';
import {ApiException} from './exception';
import {isCodeInRange} from '../util';
{{#useInversify}}
import { injectable } from "inversify";
{{/useInversify}}

{{#imports}}
import { {{classname}} } from '..{{filename}}';
Expand All @@ -19,6 +22,9 @@ import { {{classname}} } from '..{{filename}}';
/**
* {{#description}}{{{description}}}{{/description}}{{^description}}no description{{/description}}
*/
{{#useInversify}}
@injectable()
{{/useInversify}}
export class {{classname}}RequestFactory extends BaseAPIRequestFactory {

{{#operation}}
Expand Down Expand Up @@ -142,6 +148,9 @@ export class {{classname}}RequestFactory extends BaseAPIRequestFactory {

{{#operations}}

{{#useInversify}}
@injectable()
{{/useInversify}}
export class {{classname}}ResponseProcessor {

{{#operation}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Configuration } from '../configuration'
{{#useInversify}}
import { injectable, inject } from "inversify";
import { AbstractConfiguration } from "../services/configuration";
{{/useInversify}}

/**
*
Expand All @@ -17,9 +21,12 @@ export const COLLECTION_FORMATS = {
* @export
* @class BaseAPI
*/
{{#useInversify}}
@injectable()
{{/useInversify}}
export class BaseAPIRequestFactory {

constructor(protected configuration: Configuration) {
constructor({{#useInversify}}@inject(AbstractConfiguration) {{/useInversify}}protected configuration: Configuration) {
}
};

Expand Down
Loading