Skip to content

Commit 158cdfe

Browse files
jgilesmacjohnny
authored andcommitted
[typescript-fetch] fix #1998: prefix req ifaces. (#3740)
Prefix generated request interface names with API classname rather than using namespaces, because namespaces are not well-supported by some tooling (in particular, Babel).
1 parent 1fc03c8 commit 158cdfe

34 files changed

+126
-173
lines changed

CI/samples.ci/client/petstore/typescript-fetch/builds/namespace-parameter-interfaces/pom.xml renamed to CI/samples.ci/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<project>
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>org.openapitools</groupId>
4-
<artifactId>TypeScriptFetchBuildNamespaceParameterInterfacesPestoreClientTests</artifactId>
4+
<artifactId>TypeScriptFetchBuildPrefixParameterInterfacesPestoreClientTests</artifactId>
55
<packaging>pom</packaging>
66
<version>1.0-SNAPSHOT</version>
77
<name>TS Fetch Petstore Client (with namespacing for parameter interfaces)</name>

bin/openapi3/typescript-fetch-petstore-all.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
./bin/openapi3/typescript-fetch-petstore-with-npm-version.sh
55
./bin/openapi3/typescript-fetch-petstore-interfaces.sh
66
./bin/openapi3/typescript-fetch-petstore.sh
7-
./bin/openapi3/typescript-fetch-petstore-namespace-parameter-interfaces.sh
7+
./bin/openapi3/typescript-fetch-petstore-prefix-parameter-interfaces.sh

bin/openapi3/typescript-fetch-petstore-namespace-parameter-interfaces.sh renamed to bin/openapi3/typescript-fetch-petstore-prefix-parameter-interfaces.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ fi
2727

2828
# if you've executed sbt assembly previously it will use that instead.
2929
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
30-
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g typescript-fetch -o samples/client/petstore/typescript-fetch/builds/namespace-parameter-interfaces --additional-properties namespaceParameterInterfaces=true $@"
30+
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -g typescript-fetch -o samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces --additional-properties prefixParameterInterfaces=true $@"
3131

3232
java $JAVA_OPTS -jar $executable $ags

bin/typescript-fetch-petstore-all.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
./bin/typescript-fetch-petstore-interfaces.sh
66
./bin/typescript-fetch-petstore.sh
77
./bin/typescript-fetch-petstore-multiple-parameters.sh
8-
./bin/typescript-fetch-petstore-namespace-parameter-interfaces.sh
8+
./bin/typescript-fetch-petstore-prefix-parameter-interfaces.sh

bin/typescript-fetch-petstore-namespace-parameter-interfaces.json renamed to bin/typescript-fetch-petstore-prefix-parameter-interfaces.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
"npmVersion": "1.0.0",
44
"npmRepository" : "https://skimdb.npmjs.com/registry",
55
"snapshot" : false,
6-
"namespaceParameterInterfaces": true
6+
"prefixParameterInterfaces": true
77
}

bin/typescript-fetch-petstore-namespace-parameter-interfaces.sh renamed to bin/typescript-fetch-petstore-prefix-parameter-interfaces.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ fi
2727

2828
# if you've executed sbt assembly previously it will use that instead.
2929
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-fetch -c bin/typescript-fetch-petstore-namespace-parameter-interfaces.json -o samples/client/petstore/typescript-fetch/builds/namespace-parameter-interfaces $@"
30+
ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g typescript-fetch -c bin/typescript-fetch-petstore-prefix-parameter-interfaces.json -o samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces $@"
3131

3232
java $JAVA_OPTS -jar $executable $ags
3333

34-
cp CI/samples.ci/client/petstore/typescript-fetch/builds/namespace-parameter-interfaces/pom.xml samples/client/petstore/typescript-fetch/builds/namespace-parameter-interfaces/pom.xml
34+
cp CI/samples.ci/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/pom.xml samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/pom.xml

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ call bin\windows\typescript-fetch-petstore-target-es6.bat
55
call bin\windows\typescript-fetch-petstore-with-npm-version.bat
66
call bin\windows\typescript-fetch-petstore-interfaces.bat
77
call bin\windows\typescript-fetch-petstore-multiple-parameters.bat
8-
call bin\windows\typescript-fetch-petstore-namespace-parameter-interfaces.bat
8+
call bin\windows\typescript-fetch-petstore-prefix-parameter-interfaces.bat

bin/windows/typescript-fetch-petstore-namespace-parameter-interfaces.bat renamed to bin/windows/typescript-fetch-petstore-prefix-parameter-interfaces.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ If Not Exist %executable% (
77
)
88

99
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
10-
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g typescript-fetch -o samples\client\petstore\typescript-fetch\builds\namespace-parameter-interfaces --additional-properties namespaceParameterInterfaces=true
10+
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g typescript-fetch -o samples\client\petstore\typescript-fetch\builds\prefix-parameter-interfaces --additional-properties prefixParameterInterfaces=true
1111

1212
java %JAVA_OPTS% -jar %executable% %ags%

docs/generators/typescript-fetch.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ sidebar_label: typescript-fetch
1919
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
2020
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
2121
|useSingleRequestParameter|Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.| |true|
22-
|namespaceParameterInterfaces|Setting this property to true will generate parameter interface declarations within a dedicated namespace to avoid name conflicts.| |false|
22+
|prefixParameterInterfaces|Setting this property to true will generate parameter interface declarations prefixed with API class name to avoid name conflicts.| |false|

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

+13-24
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
3333
public static final String NPM_REPOSITORY = "npmRepository";
3434
public static final String WITH_INTERFACES = "withInterfaces";
3535
public static final String USE_SINGLE_REQUEST_PARAMETER = "useSingleRequestParameter";
36-
public static final String NAMESPACE_PARAMETER_INTERFACES = "namespaceParameterInterfaces";
36+
public static final String PREFIX_PARAMETER_INTERFACES = "prefixParameterInterfaces";
3737

3838
protected String npmRepository = null;
3939
private boolean useSingleRequestParameter = true;
40-
private boolean namespaceParameterInterfaces = false;
40+
private boolean prefixParameterInterfaces = false;
4141
protected boolean addedApiIndex = false;
4242
protected boolean addedModelIndex = false;
4343

@@ -61,7 +61,7 @@ public TypeScriptFetchClientCodegen() {
6161
this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json"));
6262
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()));
6363
this.cliOptions.add(new CliOption(USE_SINGLE_REQUEST_PARAMETER, "Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.TRUE.toString()));
64-
this.cliOptions.add(new CliOption(NAMESPACE_PARAMETER_INTERFACES, "Setting this property to true will generate parameter interface declarations within a dedicated namespace to avoid name conflicts.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
64+
this.cliOptions.add(new CliOption(PREFIX_PARAMETER_INTERFACES, "Setting this property to true will generate parameter interface declarations prefixed with API class name to avoid name conflicts.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
6565
}
6666

6767
@Override
@@ -97,10 +97,10 @@ public void processOpts() {
9797
}
9898
writePropertyBack(USE_SINGLE_REQUEST_PARAMETER, getUseSingleRequestParameter());
9999

100-
if (additionalProperties.containsKey(NAMESPACE_PARAMETER_INTERFACES)) {
101-
this.setNamespaceParameterInterfaces(convertPropertyToBoolean(NAMESPACE_PARAMETER_INTERFACES));
100+
if (additionalProperties.containsKey(PREFIX_PARAMETER_INTERFACES)) {
101+
this.setPrefixParameterInterfaces(convertPropertyToBoolean(PREFIX_PARAMETER_INTERFACES));
102102
}
103-
writePropertyBack(NAMESPACE_PARAMETER_INTERFACES, getNamespaceParameterInterfaces());
103+
writePropertyBack(PREFIX_PARAMETER_INTERFACES, getPrefixParameterInterfaces());
104104

105105
if (additionalProperties.containsKey(NPM_NAME)) {
106106
addNpmPackageGeneration();
@@ -216,7 +216,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
216216
this.addOperationModelImportInfomation(operations);
217217
this.updateOperationParameterEnumInformation(operations);
218218
this.addOperationObjectResponseInformation(operations);
219-
this.addOperationNamespaceParameterInterfacesInformation(operations);
219+
this.addOperationPrefixParameterInterfacesInformation(operations);
220220
return operations;
221221
}
222222

@@ -263,20 +263,9 @@ private void addOperationObjectResponseInformation(Map<String, Object> operation
263263
}
264264
}
265265

266-
private void addOperationNamespaceParameterInterfacesInformation(Map<String, Object> operations) {
266+
private void addOperationPrefixParameterInterfacesInformation(Map<String, Object> operations) {
267267
Map<String, Object> _operations = (Map<String, Object>) operations.get("operations");
268-
List<CodegenOperation> operationList = (List<CodegenOperation>) _operations.get("operation");
269-
if (!operationList.isEmpty() && getNamespaceParameterInterfaces()) {
270-
operations.put("namespaceParameterInterfaces", true);
271-
operations.put("paramIfaceIndent", " ");
272-
operations.put("paramIfaceSuffix", "");
273-
operations.put("paramIfaceNsPrefix", operationList.get(0).baseName + "Requests.");
274-
} else {
275-
operations.put("namespaceParameterInterfaces", false);
276-
operations.put("paramIfaceIndent", "");
277-
operations.put("paramIfaceSuffix", "Request");
278-
operations.put("paramIfaceNsPrefix", "");
279-
}
268+
operations.put("prefixParameterInterfaces", getPrefixParameterInterfaces());
280269
}
281270

282271
private void addExtraReservedWords() {
@@ -315,11 +304,11 @@ private void setUseSingleRequestParameter(boolean useSingleRequestParameter) {
315304
this.useSingleRequestParameter = useSingleRequestParameter;
316305
}
317306

318-
private boolean getNamespaceParameterInterfaces() {
319-
return namespaceParameterInterfaces;
307+
private boolean getPrefixParameterInterfaces() {
308+
return prefixParameterInterfaces;
320309
}
321310

322-
private void setNamespaceParameterInterfaces(boolean namespaceParameterInterfaces) {
323-
this.namespaceParameterInterfaces = namespaceParameterInterfaces;
311+
private void setPrefixParameterInterfaces(boolean prefixParameterInterfaces) {
312+
this.prefixParameterInterfaces = prefixParameterInterfaces;
324313
}
325314
}

modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache

+5-20
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,16 @@ import {
1313
{{/imports.0}}
1414

1515
{{#operations}}
16-
{{#namespaceParameterInterfaces}}
17-
{{#operation.0}}
18-
/**
19-
* Generated request parameter interface declarations (in namespace to avoid collisions).
20-
*/
21-
export namespace {{baseName}}Requests {
22-
23-
{{/operation.0}}
24-
{{/namespaceParameterInterfaces}}
2516
{{#operation}}
2617
{{#allParams.0}}
27-
{{paramIfaceIndent}}export interface {{operationIdCamelCase}}{{paramIfaceSuffix}} {
18+
export interface {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request {
2819
{{#allParams}}
29-
{{paramIfaceIndent}} {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}};
20+
{{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}};
3021
{{/allParams}}
31-
{{paramIfaceIndent}}}
22+
}
3223

3324
{{/allParams.0}}
3425
{{/operation}}
35-
{{#namespaceParameterInterfaces}}
36-
{{#operation.0}}
37-
}
38-
39-
{{/operation.0}}
40-
{{/namespaceParameterInterfaces}}
4126
{{/operations}}
4227
{{#operations}}
4328
/**
@@ -54,7 +39,7 @@ export class {{classname}} extends runtime.BaseAPI {
5439
* {{&summary}}
5540
{{/summary}}
5641
*/
57-
async {{nickname}}Raw({{#allParams.0}}requestParameters: {{paramIfaceNsPrefix}}{{operationIdCamelCase}}{{paramIfaceSuffix}}{{/allParams.0}}): Promise<runtime.ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}>> {
42+
async {{nickname}}Raw({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}): Promise<runtime.ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}>> {
5843
{{#allParams}}
5944
{{#required}}
6045
if (requestParameters.{{paramName}} === null || requestParameters.{{paramName}} === undefined) {
@@ -264,7 +249,7 @@ export class {{classname}} extends runtime.BaseAPI {
264249
}
265250
{{/useSingleRequestParameter}}
266251
{{#useSingleRequestParameter}}
267-
async {{nickname}}({{#allParams.0}}requestParameters: {{paramIfaceNsPrefix}}{{operationIdCamelCase}}{{paramIfaceSuffix}}{{/allParams.0}}): Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {
252+
async {{nickname}}({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}): Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {
268253
{{#returnType}}
269254
const response = await this.{{nickname}}Raw({{#allParams.0}}requestParameters{{/allParams.0}});
270255
return await response.value();

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Map<String, String> createOptions() {
5252
.put(TypeScriptFetchClientCodegen.SNAPSHOT, Boolean.FALSE.toString())
5353
.put(TypeScriptFetchClientCodegen.WITH_INTERFACES, Boolean.FALSE.toString())
5454
.put(TypeScriptFetchClientCodegen.USE_SINGLE_REQUEST_PARAMETER, Boolean.FALSE.toString())
55-
.put(TypeScriptFetchClientCodegen.NAMESPACE_PARAMETER_INTERFACES, Boolean.FALSE.toString())
55+
.put(TypeScriptFetchClientCodegen.PREFIX_PARAMETER_INTERFACES, Boolean.FALSE.toString())
5656
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
5757
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
5858
.build();

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -844,15 +844,15 @@
844844
</modules>
845845
</profile>
846846
<profile>
847-
<id>typescript-fetch-client-builds-namespace-parameter-interfaces</id>
847+
<id>typescript-fetch-client-builds-prefix-parameter-interfaces</id>
848848
<activation>
849849
<property>
850850
<name>env</name>
851851
<value>java</value>
852852
</property>
853853
</activation>
854854
<modules>
855-
<module>samples/client/petstore/typescript-fetch/builds/namespace-parameter-interfaces</module>
855+
<module>samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces</module>
856856
</modules>
857857
</profile>
858858
<profile>

samples/client/petstore/typescript-fetch/builds/namespace-parameter-interfaces/pom.xml renamed to samples/client/petstore/typescript-fetch/builds/prefix-parameter-interfaces/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<project>
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>org.openapitools</groupId>
4-
<artifactId>TypeScriptFetchBuildNamespaceParameterInterfacesPestoreClientTests</artifactId>
4+
<artifactId>TypeScriptFetchBuildPrefixParameterInterfacesPestoreClientTests</artifactId>
55
<packaging>pom</packaging>
66
<version>1.0-SNAPSHOT</version>
77
<name>TS Fetch Petstore Client (with namespacing for parameter interfaces)</name>

0 commit comments

Comments
 (0)