Skip to content

Commit f550553

Browse files
karismannwing328
authored andcommitted
[Typescript] gets package npm version from API specification (#2920)
* [TypeScript] gets package npm version from API specification * forgot flowtyped
1 parent 049d24e commit f550553

24 files changed

+134
-380
lines changed

docs/generators/javascript-flowtyped.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ sidebar_label: javascript-flowtyped
1313
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
1414
|modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase|
1515
|supportsES6|Generate code that conforms to ES6.| |false|
16-
|npmName|The name under which you want to publish generated npm package| |null|
17-
|npmVersion|The version of your npm package| |null|
16+
|npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null|
17+
|npmVersion|The version of your npm package. If not provided, using the version from the OpenAPI specification file.| |1.0.0|
18+
|snapshot|When setting this property to true, the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
1819
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
19-
|snapshot|When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|

docs/generators/typescript-angular.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ sidebar_label: typescript-angular
1313
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
1414
|modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase|
1515
|supportsES6|Generate code that conforms to ES6.| |false|
16-
|npmName|The name under which you want to publish generated npm package. Required to generate a full angular package| |null|
16+
|npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null|
1717
|npmVersion|The version of your npm package. If not provided, using the version from the OpenAPI specification file.| |1.0.0|
18+
|snapshot|When setting this property to true, the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
1819
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
19-
|snapshot|When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
2020
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
2121
|taggedUnions|Use discriminators to create tagged unions instead of extending interfaces.| |false|
2222
|providedInRoot|Use this property to provide Injectables in root (it is only valid in angular version greater or equal to 6.0.0).| |false|

docs/generators/typescript-aurelia.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ sidebar_label: typescript-aurelia
1313
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
1414
|modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase|
1515
|supportsES6|Generate code that conforms to ES6.| |false|
16-
|npmName|The name under which you want to publish generated npm package| |null|
17-
|npmVersion|The version of your npm package| |null|
16+
|npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null|
17+
|npmVersion|The version of your npm package. If not provided, using the version from the OpenAPI specification file.| |1.0.0|
18+
|snapshot|When setting this property to true, the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|

docs/generators/typescript-axios.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ sidebar_label: typescript-axios
1313
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
1414
|modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase|
1515
|supportsES6|Generate code that conforms to ES6.| |false|
16-
|npmName|The name under which you want to publish generated npm package| |null|
17-
|npmVersion|The version of your npm package| |null|
16+
|npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null|
17+
|npmVersion|The version of your npm package. If not provided, using the version from the OpenAPI specification file.| |1.0.0|
18+
|snapshot|When setting this property to true, the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
1819
|npmRepository|Use this property to set an url of your private npmRepo in the package.json| |null|
19-
|snapshot|When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
2020
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
2121
|withSeparateModelsAndApi|Put the model and api in separate folders and in separate classes| |false|
2222
|withoutPrefixEnums|Don't prefix enum names with class names| |false|

docs/generators/typescript-fetch.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ sidebar_label: typescript-fetch
1313
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
1414
|modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase|
1515
|supportsES6|Generate code that conforms to ES6.| |false|
16-
|npmName|The name under which you want to publish generated npm package| |null|
17-
|npmVersion|The version of your npm package| |null|
16+
|npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null|
17+
|npmVersion|The version of your npm package. If not provided, using the version from the OpenAPI specification file.| |1.0.0|
18+
|snapshot|When setting this property to true, the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
1819
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
19-
|snapshot|When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
2020
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|

docs/generators/typescript-inversify.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ sidebar_label: typescript-inversify
1313
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
1414
|modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase|
1515
|supportsES6|Generate code that conforms to ES6.| |false|
16-
|npmName|The name under which you want to publish generated npm package| |null|
17-
|npmVersion|The version of your npm package| |null|
16+
|npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null|
17+
|npmVersion|The version of your npm package. If not provided, using the version from the OpenAPI specification file.| |1.0.0|
18+
|snapshot|When setting this property to true, the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
1819
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
19-
|snapshot|When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
2020
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
2121
|usePromise|Setting this property to use promise instead of observable inside every service.| |false|
2222
|taggedUnions|Use discriminators to create tagged unions instead of extending interfaces.| |false|

docs/generators/typescript-jquery.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ sidebar_label: typescript-jquery
1313
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
1414
|modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase|
1515
|supportsES6|Generate code that conforms to ES6.| |false|
16-
|npmName|The name under which you want to publish generated npm package| |null|
17-
|npmVersion|The version of your npm package| |null|
16+
|npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null|
17+
|npmVersion|The version of your npm package. If not provided, using the version from the OpenAPI specification file.| |1.0.0|
18+
|snapshot|When setting this property to true, the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
1819
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
19-
|snapshot|When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
2020
|jqueryAlreadyImported|When using this in legacy app using mix of typescript and javascript, this will only declare jquery and not import it| |false|

docs/generators/typescript-node.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ sidebar_label: typescript-node
1313
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
1414
|modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase|
1515
|supportsES6|Generate code that conforms to ES6.| |false|
16-
|npmName|The name under which you want to publish generated npm package| |null|
17-
|npmVersion|The version of your npm package| |null|
16+
|npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null|
17+
|npmVersion|The version of your npm package. If not provided, using the version from the OpenAPI specification file.| |1.0.0|
18+
|snapshot|When setting this property to true, the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
1819
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
19-
|snapshot|When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|

docs/generators/typescript-rxjs.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ sidebar_label: typescript-rxjs
1313
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
1414
|modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |camelCase|
1515
|supportsES6|Generate code that conforms to ES6.| |false|
16-
|npmName|The name under which you want to publish generated npm package| |null|
17-
|npmVersion|The version of your npm package| |null|
16+
|npmName|The name under which you want to publish generated npm package. Required to generate a full package| |null|
17+
|npmVersion|The version of your npm package. If not provided, using the version from the OpenAPI specification file.| |1.0.0|
18+
|snapshot|When setting this property to true, the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
1819
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
19-
|snapshot|When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
2020
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4833,10 +4833,19 @@ protected void addOption(String key, String description, String defaultValue) {
48334833
}
48344834

48354835
protected void updateOption(String key, String defaultValue) {
4836+
for (CliOption cliOption : cliOptions) {
4837+
if (cliOption.getOpt().equals(key)) {
4838+
cliOption.setDefault(defaultValue);
4839+
break;
4840+
}
4841+
}
4842+
}
4843+
4844+
protected void removeOption(String key) {
48364845
for(int i = 0; i < cliOptions.size(); i++) {
4837-
if(cliOptions.get(i).getOpt().equals(key)) {
4838-
cliOptions.get(i).setDefault(defaultValue);
4839-
return;
4846+
if (key.equals(cliOptions.get(i).getOpt())) {
4847+
cliOptions.remove(i);
4848+
break;
48404849
}
48414850
}
48424851
}

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

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.openapitools.codegen.languages;
1919

20+
import io.swagger.v3.oas.models.OpenAPI;
2021
import io.swagger.v3.oas.models.media.ArraySchema;
2122
import io.swagger.v3.oas.models.media.Schema;
2223
import io.swagger.v3.oas.models.parameters.Parameter;
@@ -39,14 +40,19 @@
3940
public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen implements CodegenConfig {
4041
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractTypeScriptClientCodegen.class);
4142

42-
protected static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT);
43-
4443
private static final String X_DISCRIMINATOR_TYPE = "x-discriminator-value";
4544
private static final String UNDEFINED_VALUE = "undefined";
45+
public static final String NPM_NAME = "npmName";
46+
public static final String NPM_VERSION = "npmVersion";
47+
public static final String SNAPSHOT = "snapshot";
48+
49+
protected static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT);
4650

4751
protected String modelPropertyNaming = "camelCase";
4852
protected Boolean supportsES6 = false;
4953
protected HashSet<String> languageGenericTypes;
54+
protected String npmName = null;
55+
protected String npmVersion = "1.0.0";
5056

5157
public AbstractTypeScriptClientCodegen() {
5258
super();
@@ -119,8 +125,14 @@ public AbstractTypeScriptClientCodegen() {
119125
typeMapping.put("UUID", "string");
120126
typeMapping.put("Error", "Error");
121127

122-
cliOptions.add(new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.MODEL_PROPERTY_NAMING_DESC).defaultValue("camelCase"));
128+
cliOptions.add(new CliOption(CodegenConstants.MODEL_PROPERTY_NAMING, CodegenConstants.MODEL_PROPERTY_NAMING_DESC).defaultValue(this.modelPropertyNaming));
123129
cliOptions.add(new CliOption(CodegenConstants.SUPPORTS_ES6, CodegenConstants.SUPPORTS_ES6_DESC).defaultValue(String.valueOf(this.getSupportsES6())));
130+
this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package." +
131+
" Required to generate a full package"));
132+
this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package. If not provided, using the version from the OpenAPI specification file.").defaultValue(this.getNpmVersion()));
133+
this.cliOptions.add(CliOption.newBoolean(SNAPSHOT,
134+
"When setting this property to true, the version will be suffixed with -SNAPSHOT." + this.SNAPSHOT_SUFFIX_FORMAT.toPattern(),
135+
false));
124136

125137
}
126138

@@ -144,6 +156,37 @@ else if (!this.isEnablePostProcessFile()) {
144156
setSupportsES6(Boolean.valueOf(additionalProperties.get(CodegenConstants.SUPPORTS_ES6).toString()));
145157
additionalProperties.put("supportsES6", getSupportsES6());
146158
}
159+
160+
if (additionalProperties.containsKey(NPM_NAME)) {
161+
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
162+
}
163+
164+
}
165+
166+
@Override
167+
public void preprocessOpenAPI(OpenAPI openAPI) {
168+
169+
if (additionalProperties.containsKey(NPM_NAME)) {
170+
171+
// If no npmVersion is provided in additional properties, version from API specification is used.
172+
// If none of them is provided then fallbacks to default version
173+
if (additionalProperties.containsKey(NPM_VERSION)) {
174+
this.setNpmVersion(additionalProperties.get(NPM_VERSION).toString());
175+
} else if (openAPI.getInfo() != null && openAPI.getInfo().getVersion() != null) {
176+
this.setNpmVersion(openAPI.getInfo().getVersion());
177+
}
178+
179+
if (additionalProperties.containsKey(SNAPSHOT) && Boolean.valueOf(additionalProperties.get(SNAPSHOT).toString())) {
180+
if (npmVersion.toUpperCase(Locale.ROOT).matches("^.*-SNAPSHOT$")) {
181+
this.setNpmVersion(npmVersion + "." + SNAPSHOT_SUFFIX_FORMAT.format(new Date()));
182+
} else {
183+
this.setNpmVersion(npmVersion + "-SNAPSHOT." + SNAPSHOT_SUFFIX_FORMAT.format(new Date()));
184+
}
185+
}
186+
additionalProperties.put(NPM_VERSION, npmVersion);
187+
188+
}
189+
147190
}
148191

149192
@Override
@@ -197,7 +240,7 @@ public String toParamName(String name) {
197240
public String toVarName(String name) {
198241
name = this.toParamName(name);
199242

200-
// if the proprty name has any breaking characters such as :, ;, . etc.
243+
// if the property name has any breaking characters such as :, ;, . etc.
201244
// then wrap the name within single quotes.
202245
// my:interface:property: string; => 'my:interface:property': string;
203246
if (propertyHasBreakingCharacters(name)) {
@@ -568,6 +611,22 @@ public Boolean getSupportsES6() {
568611
return supportsES6;
569612
}
570613

614+
public String getNpmName() {
615+
return npmName;
616+
}
617+
618+
public void setNpmName(String npmName) {
619+
this.npmName = npmName;
620+
}
621+
622+
public String getNpmVersion() {
623+
return npmVersion;
624+
}
625+
626+
public void setNpmVersion(String npmVersion) {
627+
this.npmVersion = npmVersion;
628+
}
629+
571630
private void setDiscriminatorValue(CodegenModel model, String baseName, String value) {
572631
for (CodegenProperty prop : model.allVars) {
573632
if (prop.baseName.equals(baseName)) {

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,8 @@
3232
import static org.openapitools.codegen.utils.StringUtils.dashize;
3333

3434
public class JavascriptFlowtypedClientCodegen extends AbstractTypeScriptClientCodegen {
35-
public static final String NPM_NAME = "npmName";
36-
public static final String NPM_VERSION = "npmVersion";
3735
public static final String NPM_REPOSITORY = "npmRepository";
38-
public static final String SNAPSHOT = "snapshot";
3936

40-
protected String npmName = null;
41-
protected String npmVersion = "1.0.0";
4237
protected String npmRepository = null;
4338

4439
public JavascriptFlowtypedClientCodegen() {
@@ -105,10 +100,7 @@ public JavascriptFlowtypedClientCodegen() {
105100
outputFolder = "generated-code/javascript-flowtyped";
106101
embeddedTemplateDir = templateDir = "Javascript-Flowtyped";
107102

108-
this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package"));
109-
this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package"));
110103
this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json"));
111-
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()));
112104

113105
}
114106

@@ -130,23 +122,6 @@ public void processOpts() {
130122
}
131123

132124
private void addNpmPackageGeneration() {
133-
if (additionalProperties.containsKey(NPM_NAME)) {
134-
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
135-
}
136-
137-
if (additionalProperties.containsKey(NPM_VERSION)) {
138-
this.setNpmVersion(additionalProperties.get(NPM_VERSION).toString());
139-
}
140-
141-
if (additionalProperties.containsKey(SNAPSHOT) && Boolean.valueOf(additionalProperties.get(SNAPSHOT).toString())) {
142-
if (npmVersion.toUpperCase(Locale.ROOT).matches("^.*-SNAPSHOT$")) {
143-
this.setNpmVersion(npmVersion + "." + SNAPSHOT_SUFFIX_FORMAT.format(new Date()));
144-
}
145-
else {
146-
this.setNpmVersion(npmVersion + "-SNAPSHOT." + SNAPSHOT_SUFFIX_FORMAT.format(new Date()));
147-
}
148-
}
149-
additionalProperties.put(NPM_VERSION, npmVersion);
150125

151126
if (additionalProperties.containsKey(NPM_REPOSITORY)) {
152127
this.setNpmRepository(additionalProperties.get(NPM_REPOSITORY).toString());

0 commit comments

Comments
 (0)