Skip to content
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

[typescript-fetch] namespacing the generated request object types #3695

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.openapitools</groupId>
<artifactId>TypeScriptFetchBuildNamespaceParameterInterfacesPestoreClientTests</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>TS Fetch Petstore Client (with namespacing for parameter interfaces)</name>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>npm-install</id>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>npm-build</id>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>run</argument>
<argument>build</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>npm-test</id>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>test</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ Here are some companies/projects (alphabetical order) using OpenAPI Generator in
- [Klarna](https://www.klarna.com/)
- [Metaswitch](https://www.metaswitch.com/)
- [Myworkout](https://myworkout.com)
- [Paxos](https://www.paxos.com)
- [Prometheus/Alertmanager](https://github.com/prometheus/alertmanager)
- [Raiffeisen Schweiz Genossenschaft](https://www.raiffeisen.ch)
- [RepreZen API Studio](https://www.reprezen.com/swagger-openapi-code-generation-api-first-microservices-enterprise-development)
Expand Down
1 change: 1 addition & 0 deletions bin/openapi3/typescript-fetch-petstore-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
./bin/openapi3/typescript-fetch-petstore-with-npm-version.sh
./bin/openapi3/typescript-fetch-petstore-interfaces.sh
./bin/openapi3/typescript-fetch-petstore.sh
./bin/openapi3/typescript-fetch-petstore-namespace-parameter-interfaces.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

SCRIPT="$0"
echo "# START SCRIPT: $SCRIPT"

while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=`dirname "$SCRIPT"`/..
APP_DIR=`cd "${APP_DIR}"; pwd`
fi

executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"

if [ ! -f "$executable" ]
then
mvn clean package
fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
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 $@"

java $JAVA_OPTS -jar $executable $ags
3 changes: 2 additions & 1 deletion bin/typescript-fetch-petstore-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
./bin/typescript-fetch-petstore-with-npm-version.sh
./bin/typescript-fetch-petstore-interfaces.sh
./bin/typescript-fetch-petstore.sh
./bin/typescript-fetch-petstore-multiple-parameters.sh
./bin/typescript-fetch-petstore-multiple-parameters.sh
./bin/typescript-fetch-petstore-namespace-parameter-interfaces.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"npmName": "@openapitools/typescript-fetch-petstore",
"npmVersion": "1.0.0",
"npmRepository" : "https://skimdb.npmjs.com/registry",
"snapshot" : false,
"namespaceParameterInterfaces": true
}
34 changes: 34 additions & 0 deletions bin/typescript-fetch-petstore-namespace-parameter-interfaces.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

SCRIPT="$0"
echo "# START SCRIPT: $SCRIPT"

while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=`dirname "$SCRIPT"`/..
APP_DIR=`cd "${APP_DIR}"; pwd`
fi

executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"

if [ ! -f "$executable" ]
then
mvn -B clean package
fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
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 $@"

java $JAVA_OPTS -jar $executable $ags

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
1 change: 1 addition & 0 deletions bin/windows/typescript-fetch-petstore-all.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ call bin\windows\typescript-fetch-petstore-target-es6.bat
call bin\windows\typescript-fetch-petstore-with-npm-version.bat
call bin\windows\typescript-fetch-petstore-interfaces.bat
call bin\windows\typescript-fetch-petstore-multiple-parameters.bat
call bin\windows\typescript-fetch-petstore-namespace-parameter-interfaces.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@ECHO OFF

set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar

If Not Exist %executable% (
mvn clean package
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
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

java %JAVA_OPTS% -jar %executable% %ags%
1 change: 1 addition & 0 deletions docs/generators/typescript-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ sidebar_label: typescript-fetch
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
|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|
|namespaceParameterInterfaces|Setting this property to true will generate parameter interface declarations within a dedicated namespace to avoid name conflicts.| |false|
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
public static final String NPM_REPOSITORY = "npmRepository";
public static final String WITH_INTERFACES = "withInterfaces";
public static final String USE_SINGLE_REQUEST_PARAMETER = "useSingleRequestParameter";
public static final String NAMESPACE_PARAMETER_INTERFACES = "namespaceParameterInterfaces";

protected String npmRepository = null;
private boolean useSingleRequestParameter = true;
private boolean namespaceParameterInterfaces = false;
protected boolean addedApiIndex = false;
protected boolean addedModelIndex = false;

Expand All @@ -59,6 +61,7 @@ public TypeScriptFetchClientCodegen() {
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(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(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()));
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()));
}

@Override
Expand Down Expand Up @@ -94,6 +97,11 @@ public void processOpts() {
}
writePropertyBack(USE_SINGLE_REQUEST_PARAMETER, getUseSingleRequestParameter());

if (additionalProperties.containsKey(NAMESPACE_PARAMETER_INTERFACES)) {
this.setNamespaceParameterInterfaces(convertPropertyToBoolean(NAMESPACE_PARAMETER_INTERFACES));
}
writePropertyBack(NAMESPACE_PARAMETER_INTERFACES, getNamespaceParameterInterfaces());

if (additionalProperties.containsKey(NPM_NAME)) {
addNpmPackageGeneration();
}
Expand Down Expand Up @@ -208,6 +216,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
this.addOperationModelImportInfomation(operations);
this.updateOperationParameterEnumInformation(operations);
this.addOperationObjectResponseInformation(operations);
this.addOperationNamespaceParameterInterfacesInformation(operations);
return operations;
}

Expand Down Expand Up @@ -254,6 +263,22 @@ private void addOperationObjectResponseInformation(Map<String, Object> operation
}
}

private void addOperationNamespaceParameterInterfacesInformation(Map<String, Object> operations) {
Map<String, Object> _operations = (Map<String, Object>) operations.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) _operations.get("operation");
if (!operationList.isEmpty() && getNamespaceParameterInterfaces()) {
operations.put("namespaceParameterInterfaces", true);
operations.put("paramIfaceIndent", " ");
operations.put("paramIfaceSuffix", "");
operations.put("paramIfaceNsPrefix", operationList.get(0).baseName + "Requests.");
Copy link
Member

Choose a reason for hiding this comment

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

maybe this has to be sanitized, but we will see later

} else {
operations.put("namespaceParameterInterfaces", false);
operations.put("paramIfaceIndent", "");
operations.put("paramIfaceSuffix", "Request");
operations.put("paramIfaceNsPrefix", "");
}
}

private void addExtraReservedWords() {
this.reservedWords.add("BASE_PATH");
this.reservedWords.add("BaseAPI");
Expand Down Expand Up @@ -289,4 +314,12 @@ private boolean getUseSingleRequestParameter() {
private void setUseSingleRequestParameter(boolean useSingleRequestParameter) {
this.useSingleRequestParameter = useSingleRequestParameter;
}

private boolean getNamespaceParameterInterfaces() {
return namespaceParameterInterfaces;
}

private void setNamespaceParameterInterfaces(boolean namespaceParameterInterfaces) {
this.namespaceParameterInterfaces = namespaceParameterInterfaces;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,31 @@ import {
{{/imports.0}}

{{#operations}}
{{#namespaceParameterInterfaces}}
{{#operation.0}}
/**
* Generated request parameter interface declarations (in namespace to avoid collisions).
*/
export namespace {{baseName}}Requests {

{{/operation.0}}
{{/namespaceParameterInterfaces}}
{{#operation}}
{{#allParams.0}}
export interface {{operationIdCamelCase}}Request {
{{#allParams}}
{{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}};
{{/allParams}}
}
{{paramIfaceIndent}}export interface {{operationIdCamelCase}}{{paramIfaceSuffix}} {
{{#allParams}}
{{paramIfaceIndent}} {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}};
{{/allParams}}
{{paramIfaceIndent}}}

{{/allParams.0}}
{{/operation}}
{{#namespaceParameterInterfaces}}
{{#operation.0}}
}

{{/operation.0}}
{{/namespaceParameterInterfaces}}
{{/operations}}
{{#operations}}
/**
Expand All @@ -39,7 +54,7 @@ export class {{classname}} extends runtime.BaseAPI {
* {{&summary}}
{{/summary}}
*/
async {{nickname}}Raw({{#allParams.0}}requestParameters: {{operationIdCamelCase}}Request{{/allParams.0}}): Promise<runtime.ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}>> {
async {{nickname}}Raw({{#allParams.0}}requestParameters: {{paramIfaceNsPrefix}}{{operationIdCamelCase}}{{paramIfaceSuffix}}{{/allParams.0}}): Promise<runtime.ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}>> {
{{#allParams}}
{{#required}}
if (requestParameters.{{paramName}} === null || requestParameters.{{paramName}} === undefined) {
Expand Down Expand Up @@ -249,7 +264,7 @@ export class {{classname}} extends runtime.BaseAPI {
}
{{/useSingleRequestParameter}}
{{#useSingleRequestParameter}}
async {{nickname}}({{#allParams.0}}requestParameters: {{operationIdCamelCase}}Request{{/allParams.0}}): Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {
async {{nickname}}({{#allParams.0}}requestParameters: {{paramIfaceNsPrefix}}{{operationIdCamelCase}}{{paramIfaceSuffix}}{{/allParams.0}}): Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {
{{#returnType}}
const response = await this.{{nickname}}Raw({{#allParams.0}}requestParameters{{/allParams.0}});
return await response.value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public Map<String, String> createOptions() {
.put(TypeScriptFetchClientCodegen.SNAPSHOT, Boolean.FALSE.toString())
.put(TypeScriptFetchClientCodegen.WITH_INTERFACES, Boolean.FALSE.toString())
.put(TypeScriptFetchClientCodegen.USE_SINGLE_REQUEST_PARAMETER, Boolean.FALSE.toString())
.put(TypeScriptFetchClientCodegen.NAMESPACE_PARAMETER_INTERFACES, Boolean.FALSE.toString())
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
.build();
Expand Down
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,18 @@
<module>samples/client/petstore/typescript-fetch/builds/with-npm-version</module>
</modules>
</profile>
<profile>
<id>typescript-fetch-client-builds-namespace-parameter-interfaces</id>
<activation>
<property>
<name>env</name>
<value>java</value>
</property>
</activation>
<modules>
<module>samples/client/petstore/typescript-fetch/builds/namespace-parameter-interfaces</module>
</modules>
</profile>
<profile>
<id>typescript-angularjs-client</id>
<activation>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
wwwroot/*.js
node_modules
typings
dist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4.1.1-SNAPSHOT
Loading