Skip to content

Commit d9e93ce

Browse files
djairhogeuensmacjohnny
authored andcommitted
feature: Typescript-angular support single request param (#4479)
* adds support for useSingleRequestParameter for typescript angular generator * updates samples * removes tab * updates documentation * removes tabs * Revert "updates samples" This reverts commit 8c5bfd9. * updates samples * adds configuration for sample generation * fixes script * sets executable flag * fix docs
1 parent fe0b3ce commit d9e93ce

81 files changed

Lines changed: 2234 additions & 186 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bin/typescript-angular-petstore-all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
./bin/typescript-angular-v7-petstore-provided-in-root-with-npm.sh
1616
./bin/typescript-angular-v8-petstore-provided-in-root-with-npm.sh
1717
./bin/typescript-angular-v8-petstore-provided-in-root-with-prefixed-module-name.sh
18+
./bin/typescript-angular-v8-petstore-single-request-parameter.sh
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
echo "# START SCRIPT: $SCRIPT"
5+
6+
while [ -h "$SCRIPT" ] ; do
7+
ls=`ls -ld "$SCRIPT"`
8+
link=`expr "$ls" : '.*-> \(.*\)$'`
9+
if expr "$link" : '/.*' > /dev/null; then
10+
SCRIPT="$link"
11+
else
12+
SCRIPT=`dirname "$SCRIPT"`/"$link"
13+
fi
14+
done
15+
16+
if [ ! -d "${APP_DIR}" ]; then
17+
APP_DIR=`dirname "$SCRIPT"`/..
18+
APP_DIR=`cd "${APP_DIR}"; pwd`
19+
fi
20+
21+
executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"
22+
23+
if [ ! -f "$executable" ]
24+
then
25+
mvn -B clean package
26+
fi
27+
28+
# if you've executed sbt assembly previously it will use that instead.
29+
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
30+
ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g typescript-angular -c bin/typescript-angular-v8-petstore-provided-in-root-with-npm.json -o samples/client/petstore/typescript-angular-v8-provided-in-root/builds/single-request-parameter --additional-properties ngVersion=8.0.0,useSingleRequestParameter=true $@"
31+
32+
java $JAVA_OPTS -jar $executable $ags

bin/windows/typescript-angular-petstore-all.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ call .\bin\windows\typescript-angular-v7-not-provided-in-root.bat
1313
call .\bin\windows\typescript-angular-v7-not-provided-in-root-with-npm.bat
1414
call .\bin\windows\typescript-angular-v8-provided-in-root-with-npm.bat
1515
call .\bin\windows\typescript-angular-v8-petstore-provided-in-root-with-prefixed-module-name.bat
16+
call .\bin\windows\typescript-angular-v8-single-request-parameter.bat
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar
2+
3+
If Not Exist %executable% (
4+
mvn clean package
5+
)
6+
7+
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g typescript-angular -c bin/typescript-angular-v8-petstore-provided-in-root-with-npm.json -o samples\client\petstore\typescript-angular-v8-provided-in-root\builds\single-request-parameter --additional-properties ngVersion=8.0.0,useSingleRequestParameter=true
8+
9+
10+
java %JAVA_OPTS% -jar %executable% %ags%

docs/generators/typescript-angular.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ sidebar_label: typescript-angular
1616
|snapshot|When setting this property to true, the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
1717
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
1818
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
19+
|useSingleRequestParameter|Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.| |false|
1920
|taggedUnions|Use discriminators to create tagged unions instead of extending interfaces.| |false|
2021
|providedInRoot|Use this property to provide Injectables in root (it is only valid in angular version greater or equal to 6.0.0).| |false|
2122
|ngVersion|The version of Angular.| |8.0.0|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
4444

4545
public static final String NPM_REPOSITORY = "npmRepository";
4646
public static final String WITH_INTERFACES = "withInterfaces";
47+
public static final String USE_SINGLE_REQUEST_PARAMETER = "useSingleRequestParameter";
4748
public static final String TAGGED_UNIONS = "taggedUnions";
4849
public static final String NG_VERSION = "ngVersion";
4950
public static final String PROVIDED_IN_ROOT = "providedInRoot";
@@ -58,6 +59,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
5859

5960
protected String ngVersion = "8.0.0";
6061
protected String npmRepository = null;
62+
private boolean useSingleRequestParameter = false;
6163
protected String serviceSuffix = "Service";
6264
protected String serviceFileSuffix = ".service";
6365
protected String modelSuffix = "";
@@ -86,6 +88,9 @@ public TypeScriptAngularClientCodegen() {
8688
this.cliOptions.add(CliOption.newBoolean(WITH_INTERFACES,
8789
"Setting this property to true will generate interfaces next to the default class implementations.",
8890
false));
91+
this.cliOptions.add(CliOption.newBoolean(USE_SINGLE_REQUEST_PARAMETER,
92+
"Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.",
93+
false));
8994
this.cliOptions.add(CliOption.newBoolean(TAGGED_UNIONS,
9095
"Use discriminators to create tagged unions instead of extending interfaces.",
9196
this.taggedUnions));
@@ -163,6 +168,11 @@ public void processOpts() {
163168
apiTemplateFiles.put("apiInterface.mustache", "Interface.ts");
164169
}
165170
}
171+
172+
if (additionalProperties.containsKey(USE_SINGLE_REQUEST_PARAMETER)) {
173+
this.setUseSingleRequestParameter(convertPropertyToBoolean(USE_SINGLE_REQUEST_PARAMETER));
174+
}
175+
writePropertyBack(USE_SINGLE_REQUEST_PARAMETER, getUseSingleRequestParameter());
166176

167177
if (additionalProperties.containsKey(TAGGED_UNIONS)) {
168178
taggedUnions = Boolean.parseBoolean(additionalProperties.get(TAGGED_UNIONS).toString());
@@ -576,6 +586,14 @@ public void setNpmRepository(String npmRepository) {
576586
this.npmRepository = npmRepository;
577587
}
578588

589+
private boolean getUseSingleRequestParameter() {
590+
return useSingleRequestParameter;
591+
}
592+
593+
private void setUseSingleRequestParameter(boolean useSingleRequestParameter) {
594+
this.useSingleRequestParameter = useSingleRequestParameter;
595+
}
596+
579597
private String getApiFilenameFromClassname(String classname) {
580598
String name = classname.substring(0, classname.length() - serviceSuffix.length());
581599
return toApiFilename(name);

modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,29 @@ import { {{classname}} } from '../{{filename}}';
3030
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
3131
import { Configuration } from '../configuration';
3232
{{#withInterfaces}}
33-
import { {{classname}}Interface } from './{{classFilename}}Interface';
33+
import {
34+
{{classname}}Interface{{#useSingleRequestParameter}}{{#operations}}{{#operation}}{{#allParams.0}},
35+
{{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}RequestParams{{/allParams.0}}{{/operation}}{{/operations}}{{/useSingleRequestParameter}}
36+
} from './{{classFilename}}Interface';
3437
{{/withInterfaces}}
3538

3639
{{#operations}}
3740

41+
{{^withInterfaces}}
42+
{{#useSingleRequestParameter}}
43+
{{#operation}}
44+
{{#allParams.0}}
45+
export interface {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}RequestParams {
46+
{{#allParams}}
47+
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{#isNullable}} | null{{/isNullable}};
48+
{{/allParams}}
49+
}
50+
51+
{{/allParams.0}}
52+
{{/operation}}
53+
{{/useSingleRequestParameter}}
54+
{{/withInterfaces}}
55+
3856
{{#description}}
3957
/**
4058
* {{&description}}
@@ -108,11 +126,14 @@ export class {{classname}} {
108126
{{#summary}}
109127
* @summary {{&summary}}
110128
{{/summary}}
111-
{{#allParams}}* @param {{paramName}} {{description}}
112-
{{/allParams}}*/
129+
{{^useSingleRequestParameter}}{{#allParams}}
130+
* @param {{paramName}} {{description}}{{/allParams}}{{/useSingleRequestParameter}}
131+
{{#useSingleRequestParameter}}{{#allParams.0}}
132+
* @param requestParameters
133+
{{/allParams.0}}{{/useSingleRequestParameter}}*/
113134
{{! if you change this method signature, also change the version below }}
114-
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{^useHttpClient}}{{#hasParams}}, {{/hasParams}}extraHttpRequestParams?: RequestOptionsArgs{{/useHttpClient}}): Observable<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
115-
return this.{{nickname}}WithHttpInfo({{#allParams}}{{paramName}}, {{/allParams}}extraHttpRequestParams)
135+
public {{nickname}}({{^useSingleRequestParameter}}{{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{/useSingleRequestParameter}}{{#useSingleRequestParameter}}{{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}RequestParams{{/allParams.0}}{{/useSingleRequestParameter}}{{^useHttpClient}}{{#hasParams}}, {{/hasParams}}extraHttpRequestParams?: RequestOptionsArgs{{/useHttpClient}}): Observable<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
136+
return this.{{nickname}}WithHttpInfo({{^useSingleRequestParameter}}{{#allParams}}{{paramName}}, {{/allParams}}{{/useSingleRequestParameter}}{{#useSingleRequestParameter}}{{#allParams.0}}requestParameters, {{/allParams.0}}{{/useSingleRequestParameter}}extraHttpRequestParams)
116137
.map((response: Response) => {
117138
if (response.status === 204) {
118139
return undefined;
@@ -138,20 +159,34 @@ export class {{classname}} {
138159
{{#notes}}
139160
* {{notes}}
140161
{{/notes}}
141-
{{#allParams}}* @param {{paramName}} {{description}}
142-
{{/allParams}}{{#useHttpClient}}* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
143-
* @param reportProgress flag to report request and response progress.{{/useHttpClient}}
162+
{{^useSingleRequestParameter}}
163+
{{#allParams}}
164+
* @param {{paramName}} {{description}}
165+
{{/allParams}}
166+
{{/useSingleRequestParameter}}
167+
{{#useSingleRequestParameter}}
168+
{{#allParams.0}}
169+
* @param requestParameters
170+
{{/allParams.0}}
171+
{{/useSingleRequestParameter}}
172+
{{#useHttpClient}}
173+
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
174+
* @param reportProgress flag to report request and response progress.
175+
{{/useHttpClient}}
144176
*/
145177
{{#useHttpClient}}
146-
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}observe?: 'body', reportProgress?: boolean): Observable<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>;
147-
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
148-
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
149-
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}observe: any = 'body', reportProgress: boolean = false ): Observable<any> {
178+
public {{nickname}}({{^useSingleRequestParameter}}{{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}{{/useSingleRequestParameter}}{{#useSingleRequestParameter}}{{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}RequestParams, {{/allParams.0}}{{/useSingleRequestParameter}}observe?: 'body', reportProgress?: boolean): Observable<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>;
179+
public {{nickname}}({{^useSingleRequestParameter}}{{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}{{/useSingleRequestParameter}}{{#useSingleRequestParameter}}{{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}RequestParams, {{/allParams.0}}{{/useSingleRequestParameter}}observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
180+
public {{nickname}}({{^useSingleRequestParameter}}{{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}{{/useSingleRequestParameter}}{{#useSingleRequestParameter}}{{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}RequestParams, {{/allParams.0}}{{/useSingleRequestParameter}}observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
181+
public {{nickname}}({{^useSingleRequestParameter}}{{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}{{/useSingleRequestParameter}}{{#useSingleRequestParameter}}{{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}RequestParams, {{/allParams.0}}{{/useSingleRequestParameter}}observe: any = 'body', reportProgress: boolean = false ): Observable<any> {
150182
{{/useHttpClient}}
151183
{{^useHttpClient}}
152-
public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: RequestOptionsArgs): Observable<Response> {
184+
public {{nickname}}WithHttpInfo({{^useSingleRequestParameter}}{{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}{{/useSingleRequestParameter}}{{#useSingleRequestParameter}}{{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}RequestParams, {{/allParams.0}}{{/useSingleRequestParameter}}extraHttpRequestParams?: RequestOptionsArgs): Observable<Response> {
153185
{{/useHttpClient}}
154186
{{#allParams}}
187+
{{#useSingleRequestParameter}}
188+
const {{paramName}} = requestParameters.{{paramName}};
189+
{{/useSingleRequestParameter}}
155190
{{#required}}
156191
if ({{paramName}} === null || {{paramName}} === undefined) {
157192
throw new Error('Required parameter {{paramName}} was null or undefined when calling {{nickname}}.');

modules/openapi-generator/src/main/resources/typescript-angular/apiInterface.mustache

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ import { Configuration } from '../configurat
2222

2323
{{#operations}}
2424

25+
{{#useSingleRequestParameter}}
26+
{{#operation}}
27+
{{#allParams.0}}
28+
export interface {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}RequestParams {
29+
{{#allParams}}
30+
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{#isNullable}} | null{{/isNullable}};
31+
{{/allParams}}
32+
}
33+
34+
{{/allParams.0}}
35+
{{/operation}}
36+
{{/useSingleRequestParameter}}
37+
2538
{{#description}}
2639
/**
2740
* {{&description}}
@@ -34,11 +47,13 @@ export interface {{classname}}Interface {
3447

3548
{{#operation}}
3649
/**
37-
* {{summary}}
38-
* {{notes}}
39-
{{#allParams}}* @param {{paramName}} {{description}}
40-
{{/allParams}}*/
41-
{{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any): Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}>;
50+
* {{summary}}
51+
* {{notes}}
52+
{{^useSingleRequestParameter}}
53+
{{#allParams}}* @param {{paramName}} {{description}}
54+
{{/allParams}}{{/useSingleRequestParameter}}{{#useSingleRequestParameter}}{{#allParams.0}}* @param requestParameters
55+
{{/allParams.0}}{{/useSingleRequestParameter}}*/
56+
{{nickname}}({{^useSingleRequestParameter}}{{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}{{/useSingleRequestParameter}}{{#useSingleRequestParameter}}{{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}RequestParams, {{/allParams.0}}{{/useSingleRequestParameter}}extraHttpRequestParams?: any): Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}>;
4257

4358
{{/operation}}
4459
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptAngularClientOptionsProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public Map<String, String> createOptions() {
5959
.put(TypeScriptAngularClientCodegen.NPM_VERSION, NMP_VERSION)
6060
.put(TypeScriptAngularClientCodegen.SNAPSHOT, Boolean.FALSE.toString())
6161
.put(TypeScriptAngularClientCodegen.WITH_INTERFACES, Boolean.FALSE.toString())
62+
.put(TypeScriptAngularClientCodegen.USE_SINGLE_REQUEST_PARAMETER, Boolean.FALSE.toString())
6263
.put(TypeScriptAngularClientCodegen.PROVIDED_IN_ROOT, Boolean.FALSE.toString())
6364
.put(TypeScriptAngularClientCodegen.TAGGED_UNIONS, Boolean.FALSE.toString())
6465
.put(TypeScriptAngularClientCodegen.NPM_REPOSITORY, NPM_REPOSITORY)

0 commit comments

Comments
 (0)